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

Add the &display=swap parameter to the end of your Google Fonts URL #429

Open
StudioRATATA opened this issue Apr 25, 2020 · 4 comments
Open

Comments

@StudioRATATA
Copy link

Hi, Can you Add the &display=swap parameter to the end of your Google Fonts URL?
https://web.dev/font-display/?utm_source=lighthouse&utm_medium=unknown#google-fonts

This adds to the Google Lighthouse score

@prdt3e
Copy link

prdt3e commented Apr 26, 2020

#403

@prdt3e
Copy link

prdt3e commented Apr 26, 2020

In head, if the font is cached:

<script>sessionStorage.fontsLoaded&&document.documentElement.classList.add("wf-active");</script>

And load webfontloader async with font-display: swap:

    if (!sessionStorage.fontsLoaded) {
        require(['webfontloader'], function(WebFont) {
            WebFont.load({
                google: {
                  families: [
                      'Roboto:400,700&display=swap'
                  ]
                },
                active: () => {
                    sessionStorage.fontsLoaded = true
                },
            });
        });
    }

Google Lighthouse score Mobil: 100
Google Lighthouse score Desktop: 100

Der gesamte Text bleibt während der Webfont-Ladevorgänge sichtbar.
All text remains visible during webfont loads

@prdt3e
Copy link

prdt3e commented Apr 26, 2020

You can now optimize the whole thing with Preconnect / dns-prefetch.
This takes the loading time of the connection establishment to the target out of the consideration. In a test case, the Google programmer Ilya Grigorik saved about 0.5 seconds of loading time by adding Preconnect when loading the Google fonts and wrote a very informative article on this topic.

<link rel="dns-prefetch" href="//fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>

Does anyone have more experience or tips here?

@olemarius
Copy link

May I add that in order for display=swap to work, you need to add it only to the last font in the array.

you can achieve this by doing

const fonts = [ ... ] // what you'd normally pass to families: []
fonts.map((x, i) => x + variants + (i === fonts.length - 1 ? '&display=swap' : ''));

Adding on each item in the array passed to families will generate a request with multiple display=swap params.

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

3 participants