shadowwalker / next-pwa

Zero config PWA plugin for Next.js, with workbox 🧰
MIT License
3.86k stars 323 forks source link

_offline page flashes briefly when loading dynamic route with query parameter #423

Open jamesopti opened 1 year ago

jamesopti commented 1 year ago

Summary

I'm trying to debug why the _offline page I have flashes momentarily when loading a cached page without a network connection.

The start Url is / The page I'm testing is /docs/[docId]?foo=bar

Works fine: /docs/[docId] Flashes _offline: /docs/[docId]?foo=bar

Versions

jamesopti commented 1 year ago

UPDATE:

This seems to have solved the issue for us, caching all requests to our /docs/[docId] page in the same key. Would love any input on this approach.

{
    urlPattern: ({ url }) => {
      const isSameOrigin = self.origin === url.origin
      if (!isSameOrigin) return false
      const pathname = url.pathname
      if (pathname.startsWith('/docs/')) return true
      return false
    },
    handler: 'NetworkFirst',
    options: {
      cacheName: 'docs',
      expiration: {
        maxEntries: 32,
        maxAgeSeconds: SEVEN_DAYS,
      },
      networkTimeoutSeconds: 10,
      plugins: [{ cacheKeyWillBeUsed: async () => '/docs' }],
    },
  },