Pwa-module: It's not possible not to have workbox.setConfig in sw.js

Created on 3 Feb 2020  路  3Comments  路  Source: nuxt-community/pwa-module

Version

v3.0.0-beta.19

Reproduction link

https://jsfiddle.net/zcrtmdsx/1/

Steps to reproduce

When using

pwa: {
  workbox: {
            importScripts: ["my-script.js"],
            dev: true,
            config: undefined
        }
}

The compilation crashes because config is undefined:

if (options.config.debug === undefined) {
    // Debug field is by default set to true for localhost domain which is not always ideal
    options.config.debug = options.dev || this.options.dev
  }

And it's the only way to not have it added:

<% if (options.config) {%>
// Set workbox config
workbox.setConfig(<%= JSON.stringify(options.config, null, 2) %>)
<% } %>

What is expected ?

To be able to not have the setConfig in sw.js so we can use workbox in importScripts

What is actually happening?

There is a compilation error:

 FATAL  Cannot read property 'debug' of undefined                                                                     12:50:35

  at ModuleContainer.getOptions (node_modules\@nuxtjs\pwa\lib\workbox\options.js:88:22)
  at nuxt.hook (node_modules\@nuxtjs\pwa\lib\workbox\module.js:9:32)
  at fn (node_modules\hable\lib\hookable.js:50:45)
  at promise.then.previous (node_modules\hable\lib\utils.js:15:67)
  at process._tickCallback (internal/process/next_tick.js:68:7)

If I set a value for config, the setConfig is added and I get the following error because I am using workbox in 'my-script.js':

Config must be set before accessing workbox.* modules
This bug report is available on Nuxt community (#c200)

Most helpful comment

I have this error right now. Has this been fixed yet?

I was having the same problem when trying to "background sync". I resolved it as follows:

nuxt.config.js

pwa: {
    workbox: {
      dev: true,
      offlineStrategy: 'StaleWhileRevalidate',
      runtimeCaching: [
        {
          urlPattern: 'https://cdn.com/.*',
        }
      ],
      workboxExtensions: [ /* importScripts causes failure */
        'static/bg-sw.js'
      ]
    }
  }

All 3 comments

I have this error right now. Has this been fixed yet?

I have this error right now. Has this been fixed yet?

I was having the same problem when trying to "background sync". I resolved it as follows:

nuxt.config.js

pwa: {
    workbox: {
      dev: true,
      offlineStrategy: 'StaleWhileRevalidate',
      runtimeCaching: [
        {
          urlPattern: 'https://cdn.com/.*',
        }
      ],
      workboxExtensions: [ /* importScripts causes failure */
        'static/bg-sw.js'
      ]
    }
  }

@valdeir2000 answer seems best approach. In other scripts, you may need to import workbox again.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Shuunen picture Shuunen  路  8Comments

manniL picture manniL  路  3Comments

aldarund picture aldarund  路  5Comments

memic84 picture memic84  路  5Comments

joberthel picture joberthel  路  11Comments