• I hope you can help me with fonts. When I test my pages on PageSpeed Insights, there is always some render blocking. This request usually says potential saving of 780 ms, at least on mobile:
    https://fonts.googleapis.com/css?family=Open+Sans:300,300italic,regular,italic,600,600italic,700,700italic,800,800italic&display=swap
    When I go that URL, it’s a list of maybe 80 font-faces, with labels including cyrillic, greek, vietnamese, and extended versions. Each has a link for downloading it. I’m assuming they are not all downloaded, because PageSpeed shows about 2 KB for the download.

    But still, why is it holding things up for 3/4 of a second?

    Aside: generatepress.woff2, even though quite small, was showing as a render blocking element to the tune of 1.4 seconds. By trial and error I realized I wasn’t even using it, and switched to SVG in the Customizer. That really improved my PageSpeed scores.

    The page listed is just an example of one that Google Search Console dings me for for speed.

    Thank you!

    The page I need help with: [log in to see the link]

Viewing 15 replies - 1 through 15 (of 20 total)
  • Hi,

    When I go that URL, it’s a list of maybe 80 font-faces, with labels including cyrillic, greek, vietnamese, and extended versions. Each has a link for downloading it. I’m assuming they are not all downloaded, because PageSpeed shows about 2 KB for the download.

    You can minimise the number of WOFF files by removing the Variants you’re not using in the Customizer > Typography > Body.

    Alternatively, you might want to consider locally hosting your selected fonts from Google and have full control over it.
    https://docs.generatepress.com/article/adding-local-fonts/

    You can also use plugins to host your fonts locally:
    https://wordpress.org/plugins/host-webfonts-local/

    Let us know if this helps.

    Thread Starter Jim

    (@jwmc)

    Thanks for the great information. I will look into locally hosting. But for the first suggestion, I don’t see anything in Appearance > Customize > Typography about removing language variants – only weight and italic. I don’t need greek and such.

    Thanks for the great information. I will look into locally hosting. But for the first suggestion, I don’t see anything in Appearance > Customize > Typography about removing language variants – only weight and italic. I don’t need greek and such.

    Yes it isn’t available in GP or WordPress UI. I’m afraid you’ll need a very specific site optimization & custom code to be able to achieve this as even Google provided embed code includes these language charsets.

    You may have to resort to removing the currently added Google fonts and re-add them yourself by creating a smaller @font-face rule.

    Thread Starter Jim

    (@jwmc)

    Not to beat a dead horse, but I did find that I was only using a few of the weights and styles of the font, so I deleted the rest in the Customizer. That didn’t make any difference to PageSpeed Insights, still render blocking 780 ms.

    Then I followed the steps to self-host the fonts in the link you gave,
    https://docs.generatepress.com/article/adding-local-fonts/
    It’s nice because you can limit languages as well as weights.

    But after all that, PageSpeed Insights still says the fonts are causing about the same delay. Now they are no longer render-blocking, but it says they should be pre-loaded. So I undid all the self-hosting as it didn’t seem to really help.

    Thread Starter Jim

    (@jwmc)

    As an experiment, I tried switching to the System Font Stack for body in the customizer. It makes a huge difference, in PageSpeed Insights getting scores all in the 90s on mobile and high 90s-100 on desktop. I can’t believe downloading a few font files makes such a huge difference. It must require more processing in the client than equivalent file size of an image.

    It doesn’t look quite as nice this way, and I don’t know what it looks like on other platforms, so I would still like to find a fast way to use Google’s Open Sans – self-hosting didn’t even help really.

    Theme Author Tom

    (@edge22)

    System stack is fast because it doesn’t require the browser to download any files in order to display the font, it just happens instantly.

    When you’re serving a custom font (whether it’s local or from Google), the browser needs to download those font files in order to display the font to the user visiting the site. I don’t believe there is any way around that, unfortunately, but I’d be happy to be proven wrong.

    Thread Starter Jim

    (@jwmc)

    Thanks Tom! Last year someone asked about using system stack for mobile and a google font for desktop. You said sure, and asked a question he never replied to, so you never got to the answer.

    I’d like to try that in a way that won’t mess up GeneratePress. Mobile seems to be more impacted by using Google fonts.

    So would I set the customizer to the system stack, and then do something like this in styles.css?

    @media only screen and (min-width: 980px) {
       @font-face {
          font-family: "Open Sans";
          src: url(https://fonts.googleapis.com/css?family=Open+Sans:300,regular,italic,700&display=swap);
          font-weight: normal;
          font-style: normal;
       }
    
       body {
          font-family: "Open Sans";
       }
    }

    I have a pretty strong feeling this isn’t right, but wanted to at least try something.

    Theme Author Tom

    (@edge22)

    I’m not sure if it’s possible, unfortunately. There needs to be a request to the Google servers to download the font file, and that request can’t happen using media queries.

    You could maybe do something like this:

    add_action( 'wp_head', function() {
        if ( wp_is_mobile() ) {
            echo '<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Open+Sans%3A400%2C400i%2C700%2C700i&subset=latin%2Clatin-ext" />';
    
            echo '<style>body {font-family: "Open Sans";}</style>';
        }
    } );

    This won’t work with media queries but should work on actual mobile devices.

    Thread Starter Jim

    (@jwmc)

    OK, sorry to drag this out. I will try something like that, but isn’t it backwards? If on mobile, I want to use the system font stack rather than google fonts.

    No problem.

    You can try adding this css code:

    @media (max-width:768px){
    body {
      font-family: -apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,Oxygen-Sans,Ubuntu,Cantarell,"Helvetica Neue",sans-serif !important;
    }
    }

    This will make ALL fonts to use system font stack on mobile. This may be tricky as you have make sure everything else is set to inherit.

    Let us know how it goes.

    • This reply was modified 5 years, 9 months ago by Elvin. Reason: typo
    Thread Starter Jim

    (@jwmc)

    Thanks @ejcabquina, but that didn’t quite work. It does cause the browser to switch between using system fonts when narrow to Open Sans when wide, but Open Sans google fonts gets downloaded for mobile as well as desktop (PageSpeed Insights shows it as render blocking for both).

    Of course I had to set Customize > Typography > body to Google’s Open Sans, otherwise it wouldn’t be there for desktop. The rest were set to inherit.

    If as Tom says, the font request can’t be conditioned on a media query, maybe there’s no way to download the fonts for one and not the other.

    Hi there,

    Toms code should work.
    But you can change this line:

    if ( wp_is_mobile() ) {

    to

    if ( !wp_is_mobile() ) {

    This changes the condition to NOT mobile – so it will only load those fonts if WP doesn’t detect a mobile device.

    Thread Starter Jim

    (@jwmc)

    That works! And leaving the customizer body section set to system stack. Now I only download google font on desktop. Thank you!

    One minor thing. When I go to that URL:
    https://fonts.googleapis.com/css?family=Open+Sans%3A300%2C400%2C400i%2C700&subset=latin%2Clatin-ext

    I see just the desired family, weights, and styles, but I see all the cyrillic and vietnamese etc. in addition to the latin. I don’t understand the use of the codes for certain characters there (assume it’s because it’s in php), but does the ampersand before the word “subset” also need to be converted, like to %amp or something?

    Again, minor, as I don’t think it really affects speed.

    EDIT: Hmm, even following Google’s example for getting a subset, I get all of them:

    To request the Greek subset of the Roboto Mono font, the URL would be:
    https://fonts.googleapis.com/css?family=Roboto+Mono&subset=greek

    EDIT 2: Apparently it’s more complicated than that, and all is working as it should. Thanks for the awesome help!

    • This reply was modified 5 years, 9 months ago by Jim.
    • This reply was modified 5 years, 9 months ago by Jim.

    Glad we could be of help.

    Thread Starter Jim

    (@jwmc)

    I’m baaaack! I discovered a funny problem with this strategy of using system fonts on mobile and google fonts on desktop.

    I was puzzled that when repeatedly using PageSpeed Insights, the mobile output, on various pages sometimes google fonts were downloaded and sometimes they weren’t.

    I think I found the problem in comments to this nice post:
    https://jamesc.id.au/2013/05/mobile-detection-wordpress-wp_is_mobile/
    Caching on the server – mobile user may be served a cached page intended for desktop (and I assume vice versa).

    Any magical ideas for getting around that? 😉

Viewing 15 replies - 1 through 15 (of 20 total)

The topic ‘Google fonts slowing load time?’ is closed to new replies.