I’ve been noticing this issue multiple times on some apps that accidentally run service-worker.js file in window scope - for example, some Vue Storefront based apps run it twice - on both window and worker scope:
which works fine but still gives the error what becomes confusing in result.
I’m thinking if Layer0 could just warn that service-worker.js file runs in a wrong scope?
Something like this can be added at the top:
// check if service-worker is running in the right scope
if (typeof importScripts !== 'function') {
console.warn(`You're trying to run service-worker.js file on non-worker scope. Please check your framework build and make sure you're running your service worker file once on WorkerGlobalScope.`);
return;
}
Or even maybe it’s worth to make it as a strict check and throw an error? Basically it already does it now, but it’s unclear for the most developers of what exactly going on, so that way we can make it more clear.
Thanks!