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

Load Google Fonts Asynchronously - not loading all weights with css2 #679

Open
kokers opened this issue Jun 11, 2024 · 23 comments
Open

Load Google Fonts Asynchronously - not loading all weights with css2 #679

kokers opened this issue Jun 11, 2024 · 23 comments

Comments

@kokers
Copy link
Contributor

kokers commented Jun 11, 2024

Looks like google fonts are not fully loaded when the embed code uses the new API css2. Only the default weight (400) is being pulled.

Url is build like this:
https://fonts.googleapis.com/css2?family=Manrope:[email protected]&display=swap

Looks like webfontloader doesnt handle css2 correctly. Might need a library update. Found one very old issue still open typekit/webfontloader#430 Not sure when google made a switch, but they generate embed codes now on google fonts with the css2 links.

Old API with links https://fonts.googleapis.com/css?family=Manrope:300,400,600,700,800 work ok.

Tymotey added a commit to timotei-litespeed/lscache_wp that referenced this issue Jun 17, 2024
@timotei-litespeed
Copy link
Contributor

@kokers Are you able to test some changes to the plugin files?

@kokers
Copy link
Contributor Author

kokers commented Jun 17, 2024

@timotei-litespeed sure, let me pull this commit and test it. Will let you know if that fix it.

@timotei-litespeed
Copy link
Contributor

@kokers quick answer :))
I've been testing the updated that code and seems it works.
image
image

@timotei-litespeed
Copy link
Contributor

Just try on a staging site. That code is in development.

@timotei-litespeed
Copy link
Contributor

Maybe take just the JS files content

@kokers
Copy link
Contributor Author

kokers commented Jun 17, 2024

Sorry it took a bit but needed to make sure. Of course doing this on staging no worries.

It doesnt work. I had originally just a code in wp_head hook that is a full embed code generate by google, swapped to the wp_enqueue_style to make sure, but it doesnt work.

You can see that its not, cause in sources the CSS url includes css instead of css2

This is when Load Google Fonts Async is OFF
image

This is when Load Google Fonts Async is On
image

This is the code for font:

wp_enqueue_style('qc-font-Manrope', 'https://fonts.googleapis.com/css2?family=Manrope:[email protected]&display=swap', [], '1.1');

@timotei-litespeed
Copy link
Contributor

Does the page have multiple google font links?

@timotei-litespeed
Copy link
Contributor

I see the same thing, but the fonts are loaded
image
image
image

@kokers
Copy link
Contributor Author

kokers commented Jun 17, 2024

They are loaded with only single weight. look at the CSS that has been pulled. it's only 400. All other weights are missing.

image

This is correct source that we should see:

image

It's not that font is not loaded. It's that only default (400) weight is, cause css API cant interpret the :[email protected] part. if its loaded with css2 it loads all defined weights in url.

Now it's time to figure out if it's a webfontloader issue, or there is some swap happening in LS cache plugin that changes css2 url to css which causes the issue.

@timotei-litespeed
Copy link
Contributor

That webkit version is oldddd
But the font will load and show(LIMITED!).

I tried to add css2(as in other pull requests) but no luck. I am trying to find support for both V2 and V1.

@timotei-litespeed
Copy link
Contributor

timotei-litespeed commented Jun 27, 2024

New commit: timotei-litespeed@150c214

How I tested:
functions.php

function add_google_fonts() {
	wp_enqueue_style( 'add_google_fonts', 'https://fonts.googleapis.com/css?family=Open+Sans:300,400,800', false );
	wp_enqueue_style( 'add_google_fonts2', 'https://fonts.googleapis.com/css2?family=Manrope:[email protected]&family=Open+Sans:ital,wght@0,300..800;1,300..800&family=Playwrite+ES+Deco:[email protected]&family=Playwrite+US+Trad:[email protected]&family=Poppins:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,100;1,200;1,300;1,400;1,500;1,600;1,700;1,800;1,900&display=swap', [], null );
	wp_enqueue_style( 'add_google_fonts3', 'https://fonts.googleapis.com/css2?family=Bungee+Spice&family=Maname&family=Playwrite+HR+Lijeva:[email protected]&family=Playwrite+HU:[email protected]&display=swap', [], null);
	wp_enqueue_style( 'add_google_fonts4', 'https://fonts.googleapis.com/css2?family=Roboto:ital,wght@0,100;0,300;0,400;0,500;0,700;0,900;1,100;1,300;1,400;1,500;1,700;1,900&display=swap', [], null);
	wp_enqueue_style( 'child-style', get_stylesheet_directory_uri() . '/style.css', array());
}
add_action( 'wp_enqueue_scripts', 'add_google_fonts' );

style.css

.font_1{ font-family: "Open Sans"; font-weight: 800; font-size: 44px; font-style: normal; }
.font_2{ font-family: "Manrope"; font-weight: 300; font-size: 44px; font-style: normal; }
.font_3{ font-family: "Poppins"; font-weight: 400; font-size: 44px; font-style: normal; }
.font_4{ font-family: "Playwrite ES Deco"; font-weight: 400; font-size: 44px; font-style: normal; }
.font_5{ font-family: "Playwrite US Trad"; font-weight: 100; font-size: 44px; font-style: normal; }

@timotei-litespeed
Copy link
Contributor

timotei-litespeed commented Jun 27, 2024

One thing I needed to add is: ....1,900&display=swap', [], null );
change version parameter to value null
Testing with LSC off I saw that only first font was loaded, and the rest were ignored.
Also updated webfont loader

@kokers sorry for late answer

@timotei-litespeed
Copy link
Contributor

I have revisited my work and the correct test will be from: timotei-litespeed@e72f969

Still the wp_enqueue_style paramers 2 and 3 needs to be present.
wp_enqueue_style( 'add_google_fonts2', 'https://fonts.googleapis.com/css2?family=Bungee+Spice&family=Maname&family=Playwrite+HR+Lijeva:[email protected]&family=Playwrite+HU:[email protected]&display=swap', [], null);
Without them the multiple families will remain 1 only

@kokers
Copy link
Contributor Author

kokers commented Jul 12, 2024

Will test on Monday and confirm :)

@kokers
Copy link
Contributor Author

kokers commented Jul 16, 2024

This doesn't work. It pulls the first weight only and not all of them. With the syntax 100...400 it needs to pull 100,200,300,400 so the
https://fonts.googleapis.com/css2?family=Manrope:[email protected]&display=swap
needs to become:
https://fonts.googleapis.com/css?family=Manrope:300,400,500,600,700,800

Essentially current fix is how it worked in the moment I posted the issue, cause font loader was able to get only the first one and ignored the rest of the syntax.

@timotei-litespeed
Copy link
Contributor

Beside that, is there other bugs?

@kokers
Copy link
Contributor Author

kokers commented Jul 16, 2024

No, but the issue I submitted is about that.

Only the default weight (400) is being pulled.

So this solution doesnt change anything apart from adding extra code.

@timotei-litespeed
Copy link
Contributor

But if you have italic you have to also get italic 300, italic 400, italic 500...

Yes?

@kokers
Copy link
Contributor Author

kokers commented Jul 16, 2024

Good catch. yes.

@timotei-litespeed
Copy link
Contributor

This should fix: timotei-litespeed@2bd5a53

@kokers
Copy link
Contributor Author

kokers commented Jul 17, 2024

Nice! Looks like it works now, thank you

@timotei-litespeed
Copy link
Contributor

The solution is limited la what CSS v1 can do, but will do the trick for now.
We will see what exactly will be the final solution and how we will integrate in future releases.

@timotei-litespeed
Copy link
Contributor

And thank you for testing the solution :)

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

2 participants