Nuxt pwa 未注册 Service Worker

Sou*_*Dev 3 vue.js service-worker nuxt.js workbox

我正在尝试通过 @nuxt/pwa 包在 Nuxt 中运行离线缓存。当我检查 chrome 开发工具/应用程序时,没有看到注册服务工作者。

我不确定我是否遗漏了什么。现在,我只对我的 nuxt.config 文件进行了以下添加

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
            }
          }
        }        
      ]
    }
  }
Run Code Online (Sandbox Code Playgroud)

与此同时,我'@nuxtjs/pwa'在模块下添加了。

我注意到静态文件中正在生成一个 sw.js 文件。但是,没有任何内容被缓存。

在此处输入图片说明

sw.js 如下所示:

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')
Run Code Online (Sandbox Code Playgroud)

Nic*_*nec 7

不建议在开发模式 ( npm run dev)上测试工作箱,
请参阅文档:https : //pwa.nuxtjs.org/modules/workbox.html#dev

您必须使用localhost主机名在生产模式下进行测试。
所以运行你的应用程序npm run build && npm run start并转到http://localhost:3000/

然后您应该在控制台面板中看到以下消息:
[workbox] Welcome to Workbox! --- workbox-core.dev.js:132