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

importScripts using Workbox + workbox-build can lead to broken SW #1324

Closed
gauntface opened this issue Feb 20, 2018 · 3 comments
Closed

importScripts using Workbox + workbox-build can lead to broken SW #1324

gauntface opened this issue Feb 20, 2018 · 3 comments
Milestone

Comments

@gauntface
Copy link

@beatrizdemiguelperez pointed this out on the Slack channel.

If you add an importScript that uses the workbox API and workbox-build calls workbox.setConfig(), the service worker throws with workbox-sw.js:1 Uncaught Error: Config must be set before accessing workbox.* modules to account for the fact that this is probably not intended.

@jeffposnick the easiest solution is to call setConfig before importing additional scripts.

@gauntface gauntface added this to the V3 Stable milestone Feb 20, 2018
@jeffposnick
Copy link
Contributor

Here's the relevant template that workbox-build uses:

<% if (importScripts) { %>
importScripts(
<%= importScripts.map(JSON.stringify).join(',\\n ') %>
);
<% } %>
<% if (modulePathPrefix) { %>workbox.setConfig({modulePathPrefix: <%= JSON.stringify(modulePathPrefix) %>});<% } %>

The way I'm interpreting this issue is that there needs to be some way of segregating the importScripts values, so that there's a single call to the importScripts('path/to/workbox-sw.js') first, then the call to workbox.setConfig(), and then another importScripts(...), where ... is all of the other individual script files.

This might be doable by searching through the importScripts array and finding the URL that ends with workbox-sw.js, if we could safely assume that that's always going to exist. (I guess it would if importWorkboxFrom is set to 'local' or 'cdn'.)

Or we might want to encourage a model in which you could disable the automatic workbox.setConfig(), and developers would be responsible for calling it themselves inside of the custom script that's being imported, but I think it would be difficult for developers to figure out the correct path to use.

I'm inclined to go for the first approach, unless anyone else has any ideas. How does that sound, @beatrizdemiguelperez & co.?

@gauntface
Copy link
Author

+1 to the seperate of workbox.js and using it that way.

@beatrizdemiguelperez
Copy link

Great. +1 first option. Thanks!

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

3 participants