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

Google fonts sometimes fail to load - caching of opaque errors #3303

Open
joshkel opened this issue Mar 27, 2024 · 0 comments
Open

Google fonts sometimes fail to load - caching of opaque errors #3303

joshkel opened this issue Mar 27, 2024 · 0 comments

Comments

@joshkel
Copy link
Contributor

joshkel commented Mar 27, 2024

Library Affected: workbox-strategies, workbox-recipes

Browser & Platform: Google Chrome 123.0.6312.58 for Mac

Issue Description:

I've noticed that Google fonts sometimes fail to load for my web app.

We're using a standard <link> tag to request the fonts stylesheet:

    <link
      rel="stylesheet"
      href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700&display=swap"
      crossorigin
    />

and our own local version of the Google Fonts recipe, based off the old https://developers.google.com/web/tools/workbox/guides/common-recipes#google_fonts page:

  registerRoute(
    ({ url }) => url.origin === 'https://fonts.googleapis.com',
    new StaleWhileRevalidate({
      cacheName: 'google-fonts-stylesheets',
    })
  );

  registerRoute(
    ({ url }) => url.origin === 'https://fonts.gstatic.com',
    new CacheFirst({
      cacheName: 'google-fonts-webfonts',
      plugins: [
        new CacheableResponsePlugin({
          statuses: [0, 200],
        }),
        new ExpirationPlugin({
          maxAgeSeconds: 60 * 60 * 24 * 365,
          maxEntries: 30,
        }),
      ],
    })
  );

The cause, as best as I can tell, is that Chrome will randomly fail to load the googleapis.com link when the page is first loaded. (It seems to work fine whenever an already open page is reloaded.) I've been unable to find any details as to why. Sample console error messages:

The FetchEvent for "https://fonts.googleapis.com/css?family=Roboto:300,400,500,700&display=swap" resulted in a network error response: an "opaque" response was used for a request whose type is not non-cors
The FetchEvent for "https://fonts.googleapis.com/css?family=Roboto:300,400,500,700&display=swap" resulted in a network error response: an "opaque" response was used for a request whose type is not non-cors
The FetchEvent for "https://fonts.googleapis.com/css?family=Roboto:300,400,500,700&display=swap" resulted in a network error response: an "opaque" response was used for a request whose type is not non-cors
GET https://fonts.googleapis.com/css?family=Roboto:300,400,500,700&display=swap net::ERR_FAILED

When this happens, Workbox may cache an opaque response, causing the next page load or reload to not have valid fonts. (I believe that had previously happened in the sample console messages - the previous response had failed, resulting in the FetchEvent messages, then the current response failed too.)

If I modify the recipe to not cache opaque responses, I no longer have these issues:

  registerRoute(
    ({ url }) => url.origin === 'https://fonts.googleapis.com',
    new StaleWhileRevalidate({
      cacheName: 'google-fonts-stylesheets',
      plugins: [
        new CacheableResponsePlugin({
          statuses: [200],
        }),
      ],
    })
  );

  registerRoute(
    ({ url }) => url.origin === 'https://fonts.gstatic.com',
    new CacheFirst({
      cacheName: 'google-fonts-webfonts',
      plugins: [
        new CacheableResponsePlugin({
          statuses: [200],
        }),
        new ExpirationPlugin({
          maxAgeSeconds: 60 * 60 * 24 * 365,
          maxEntries: 30,
        }),
      ],
    })
  );

Based on this, I believe that the recipe's guidance to allow caching of opaque Google fonts responses is out of date.

@joshkel joshkel changed the title Google fonts sometimes fail to load Google fonts sometimes fail to load - caching of opaque errors Mar 27, 2024
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

1 participant