Nuxt pwa not registering service worker

Viewed 7459

I am trying to run offline caching in Nuxt via the @nuxt/pwa package. It doesn't seen to register the service worker when I check the chrome devtool/application.

I am not sure if I am missing anything. Right now, I have only made the following additions to my nuxt.config file

pwa: {
    icon: {
      iconSrc: './static/logo.jpeg'
    },
    manifest: {
      lang: 'en',
      short_name: 'P',
      name: 'Project',
      start_url: '/',
      display: 'standalone',
      theme_color: '#00b5ad'
    },
    workbox: {
      /* workbox options */
      dev: false,
      offlineStrategy: 'StaleWhileRevalidate',
      runtimeCaching: [
        {
          urlPattern: 'https://media.giphy.com/media/.*',
          handler: 'cacheFirst',
          method: 'GET',
          strategyOptions: {
            cacheName: 'giphy',
            cacheExpiration: {
              maxEntries: 100,
              maxAgeSeconds: 60 * 60 * 24 * 10
            }
          }
        }        
      ]
    }
  }

Along with this I have added '@nuxtjs/pwa' under modules.

I do notice that a sw.js file is being generated in the static file. But, nothing is being cached.

enter image description here

The sw.js looks as follows:

importScripts('https://cdn.jsdelivr.net/npm/workbox-cdn@4.3.1/workbox/workbox-sw.js')

// --------------------------------------------------
// Configure
// --------------------------------------------------

// Set workbox config
workbox.setConfig({
  "debug": false
})

// Start controlling any existing clients as soon as it activates
workbox.core.clientsClaim()

// Skip over the SW waiting lifecycle stage
workbox.core.skipWaiting()

workbox.precaching.cleanupOutdatedCaches()

// --------------------------------------------------
// Precaches
// --------------------------------------------------

// Precache assets

// --------------------------------------------------
// Runtime Caching
// --------------------------------------------------

// Register route handlers for runtimeCaching
workbox.routing.registerRoute(new RegExp('https://media.giphy.com/media/.*'), new workbox.strategies.CacheFirst ({"cacheName":"giphy","cacheExpiration":{"maxEntries":100,"maxAgeSeconds":864000}}), 'GET')    
workbox.routing.registerRoute(new RegExp('/_nuxt/'), new workbox.strategies.CacheFirst ({}), 'GET')
workbox.routing.registerRoute(new RegExp('/'), new workbox.strategies.StaleWhileRevalidate ({}), 'GET')
1 Answers

It's not recommended to test workbox on dev mode (npm run dev)
see doc: https://pwa.nuxtjs.org/modules/workbox.html#dev

You have to test in production mode with the localhost hostname.
So run your app with npm run build && npm run start and go to http://localhost:3000/

Then you should see this following message in your console panel:
[workbox] Welcome to Workbox! --- workbox-core.dev.js:132

Related