How to add an offline fallback #13
Replies: 1 comment 9 replies
-
@FleetAdmiralJakob The built-in support will take a while, but here's an alternative: import withSerwistInit from "@serwist/next";
const withSerwist = withSerwistInit({
cacheOnFrontEndNav: true,
swSrc: "app/sw.ts",
swDest: "public/sw.js",
additionalPrecacheEntries: ["/fallback-font.ttf"],
}); installSerwist({
precacheEntries: self.__SW_MANIFEST,
skipWaiting: true,
clientsClaim: true,
navigationPreload: true,
runtimeCaching: [
{
urlPattern: /^https:\/\/fonts\.(?:gstatic)\.com\/.*/i,
handler: "CacheFirst",
options: {
cacheName: "google-fonts-webfonts",
expiration: {
maxEntries: 4,
maxAgeSeconds: 365 * 24 * 60 * 60, // 365 days
},
plugins: [
{
async handlerDidError() {
const fallbackResponse = await caches.match("/fallback-font.ttf", { ignoreSearch: true });
if (fallbackResponse !== undefined) {
return fallbackResponse;
}
return Response.error();
},
},
],
},
},
...defaultCache.filter((entry) => !(entry.options?.cacheName === "google-fonts-webfonts")),
],
}); |
Beta Was this translation helpful? Give feedback.
9 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
So, if a page is not cached and you are offline.
Beta Was this translation helpful? Give feedback.
All reactions