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

Offline is only working in development #168

Open
liamb0t opened this issue Oct 17, 2024 · 3 comments
Open

Offline is only working in development #168

liamb0t opened this issue Oct 17, 2024 · 3 comments

Comments

@liamb0t
Copy link

liamb0t commented Oct 17, 2024

When I run my app with network enabled I can see that workbox has cached the items built by Nuxt successfully.

Screenshot 2024-10-17 at 11 56 57

However, if I disable the network in dev console and check the cache, it is empty.

Screenshot 2024-10-17 at 11 57 38

And I also get a network error for the route I'm trying to open. And the page is offline.

Screenshot 2024-10-17 at 11 57 23

In development though if I disable the network, the page loads fine and the items are still in the cache (although the items are cached in the homepage cache, not workbox-precache).

Screenshot 2024-10-17 at 12 11 13

This is my config. It is the only config I've been able to use that actually somewhat works with offline caching.

  pwa: {
    registerType: 'autoUpdate',
    strategies: 'generateSW',
    injectRegister: 'script',
    manifest: false,
    workbox: {
      navigateFallback: '/',
      globIgnores: ['google*.html'],
      globPatterns: ['**/*.{js,css,html,json,svg,png,webmanifest}'],
      maximumFileSizeToCacheInBytes: 5 * 1024 * 1024, // 5 MB
      navigateFallbackDenylist: [
        /^\/.*\\?giscus=.*/,
        /^\/.*\\?api.*/,
        /^\/privacy.*/
      ],
      runtimeCaching: [
        {
          urlPattern: ({ url }) => url.href.startsWith('https://mysite.directus.app/items/'),
          handler: 'CacheFirst',
          options: {
            cacheName: 'directus-items'
          }
        },
        {
          urlPattern: ({ url, sameOrigin }) => sameOrigin && url.pathname.match(/^\/.*(avatar|favicon|privacy|manifest|).*/i),
          handler: 'CacheFirst' as const,
          options: { cacheName: 'homepage' }
        }, // Every article have to be visited before it is cached
        {
          urlPattern: ({ url, sameOrigin }) => sameOrigin && url.pathname.match(/^\/(api|article)\/.*/i),
          handler: 'CacheFirst' as const,
          options: { cacheName: 'articles' }
        } // when this is cached - the frontpage is working offline
      ]
    },
    devOptions: {
      enabled: true,
      type: 'module',
      navigateFallback: '/',
      navigateFallbackAllowlist: [/^\/$/]
    },
    client: {
      installPrompt: true
    }
  },
@userquin
Copy link
Member

dev mode is about checking sw logic will not work when offline (we're caching only the root page): if you enable sw in dev mode will be a pain, use nuxt generate/build and check in your local the offline support

@liamb0t
Copy link
Author

liamb0t commented Oct 18, 2024

dev mode is about checking sw logic will not work when offline (we're caching only the root page): if you enable sw in dev mode will be a pain, use nuxt generate/build and check in your local the offline support

So you suggest I set devOptions enabled: false and then test it?

If I do that and do bun run build then bun run dev, and look in the dev console, there is no sw, even when network is enabled?

@userquin
Copy link
Member

You need to run npx serve ... or node ..., check nuxt hint once generate/build script finish.

You are running the build command and then start the dev server again, you need to run app built for production (static or ssr app).

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

2 participants