Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Google Fonts CSS API v2 #430

Open
baderj opened this issue May 1, 2020 · 14 comments
Open

Support Google Fonts CSS API v2 #430

baderj opened this issue May 1, 2020 · 14 comments

Comments

@baderj
Copy link

baderj commented May 1, 2020

Google released a new version of their API https://developers.google.com/fonts/docs/css2. On the same guides page, they link to this project as the official web font loader. As far as I can tell, it won't work with the new APIs which means variable fonts from Google fonts are not supported.

It would be great if the project would also support version 2, since the official Google guides link to this repo as the recommended webfont loader.

@phox4ever
Copy link

phox4ever commented May 12, 2020

I ran into the same problem. You can change the API URL it's using by adding:

WebFont.load({
    google: {
      api: 'https://fonts.googleapis.com/css2',
      families: ['Droid Sans', 'Droid Serif']
    }
});

@reginna-chao
Copy link

It's not work for me, if I want to use two fonts with special font weight and display.

Certain URL is: https://fonts.googleapis.com/css2?family=Noto+Sans+TC:wght@400;700;900&family=Poppins:wght@400;700&display=swap

Test1:

WebFont.load({
	google: {
	    api: 'https://fonts.googleapis.com/css2',
	    families: ['Noto+Sans+TC:wght@400;700;900','Poppins:wght@400;700&display=swap']
	}
});

Get URL: https://fonts.googleapis.com/css2?family=Noto+Sans+TC:wght@400;700;900&Poppins:wght@400;700&display=swap

Test2:

WebFont.load({
	google: {
	    api: 'https://fonts.googleapis.com/css2',
	    families: ['Noto+Sans+TC:wght@400;700;900&family=Poppins:wght@400;700&display=swap']
	}
});

Get URL: https://fonts.googleapis.com/css2?family=Noto+Sans+TC:wght@400;700;900&family=Poppins&subset=wght@400;700&display=swap

How to get certain URL?

@nuthinking
Copy link

@ReginnaChao you shouldn't definitely specify the families in the same way you specify the url of the CSS. In theory the library should generate the url under the hood.

@nuthinking
Copy link

I also have issues with v.1 but v.2 doesn't seem to be fully supported. I nudged the team to try to find out more. It sucks there isn't a properly supported alternative.

@akshaybosamiya
Copy link

I ran into the same problem. You can change the API URL it's using by adding:

WebFont.load({
    google: {
      api: 'https://fonts.googleapis.com/css2',
      families: ['Droid Sans', 'Droid Serif']
    }
});

I'm also facing the same issue. It won't work for me. It returns 400 from webfont.js while providing api paramerter with new endpoint for version 2 of google fonts api.

Screenshot from 2020-07-29 17-38-48
Screenshot from 2020-07-29 17-39-21

@theprojectsomething
Copy link

theprojectsomething commented Sep 26, 2020

@phox4ever's solution should work where you are only requesting a single font, however it won't work where you need multiple fonts. Basically, the seperating syntax has changed so where it was family=font1|font2 it's now family=font1&family=font2.

I've created a PR (#437) that resolves the above by letting you specify the API version you are targeting, as well as incorporating other google-only features such as display and effects.

Check out the updated readme to see how the proposed API works. If you're desperate you can hotlink the file directly from github: https://cdn.jsdelivr.net/gh/theprojectsomething/webfontloader@feature/google-fonts-v2/webfontloader.js ... tho I would recommend hosting it yourself (or pinning the exact commit) to be safe.

@nuthinking
Copy link

@theprojectsomething the last merged PR was more than 3 years ago. What are the chances this is gonna land?

@theprojectsomething
Copy link

@nuthinking 😀 the project does look a little bit abandoned .. I figure enough people are using it in production to at least warrant a workaround!

@nuthinking
Copy link

@theprojectsomething your PR is great, maybe you should maintain a fork? 🤔

@lokesh-squareup
Copy link

You can still construct the v2 API URL yourself and pass it to the urls property. It's more manual work but depending on your situation it might still be the best option:

// Example href val: https://fonts.googleapis.com/css2?family=Fira+Sans:ital,wght@0,400;0,500;1,400
const href = `https://fonts.googleapis.com/css2?${families.join('&')}&display=fallback`;

return {
	urls: [href],
	preloads: [{
		as: 'style',
		href,
	}],
};

@somratpro
Copy link

@theprojectsomething

You are a genius, do you know how to resolve this problem? Ensure text remains visible during webfont load

@theprojectsomething
Copy link

@somratpro sorry not sure that's related to this issue. tho my guess is that what you're asking for is likely the default behaviour. If you search FOUT (flash of unstyled text) in the gh issues you'll probably find some good info

@davelab6
Copy link

@nuthinking
Copy link

@davelab6 are you suggesting using the CSS Font Loading API in conjunction with Google Fonts Developer API (to get the URL of the files to load)? Any examples we could look at? I can see on GitHub code referencing Google static font files directly without Google Fonts Dev API, is this safe? Thanks! 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants