preact-cli icon indicating copy to clipboard operation
preact-cli copied to clipboard

Service Worker loaded from CDN

Open florianmutter opened this issue 6 years ago • 10 comments

Do you want to request a feature or report a bug? Report a Bug

What is the current behaviour? sw.js is loaded from a CDN if a CDN is set in the webpack publicPath. This is forbidden: Failed to register a ServiceWorker: The origin of the provided scriptURL ('https://yourcdn.net') does not match the current origin ('https://yourapp.com').

If the current behaviour is a bug, please provide the steps to reproduce. Add the following snippet to preact.config.js:

config.output.publicPath = '//youcdn.net/';

It seems this was introduced in https://github.com/developit/preact-cli/pull/323

What is the expected behaviour? sw.js must be loaded from the base domain. See https://github.com/w3c/ServiceWorker/issues/940

Please mention other relevant information.

  • Node version: v6.10.3
  • npm version: 3.10.10
  • Operating system: macOS
  • CLI version: 2.2.1
  • Browser: Chrome 67.0.3396.87

florianmutter avatar Jun 21 '18 08:06 florianmutter

Any idea on how to get around this? Maybe using a plug in to replace the compiled bundle from n.p+"sw.js" to "/sw.js"?

fedemar avatar Jul 23 '18 16:07 fedemar

Hmm - this is an odd one. It might be worth us adding an option to define SW_PATH?

navigator.serviceWorker.register((SW_PATH || __webpack_public_path__) + 'sw.js');

developit avatar Aug 23 '18 14:08 developit

can we re-open this ticket? I am encountering the same issue. the suggestion from @developit might work

kencanak avatar Jul 16 '20 02:07 kencanak

can you please try this with the latest rc?

prateekbh avatar Jul 16 '20 06:07 prateekbh

@prateekbh , just tested it with latest rc. still being compiled as n.p+"sw.js"

kencanak avatar Jul 16 '20 06:07 kencanak

is this deployed somewhere?

prateekbh avatar Jul 16 '20 06:07 prateekbh

a ok this makes sense. I can take a look at it

prateekbh avatar Jul 16 '20 06:07 prateekbh

@prateekbh , i can create a PR, if it's ok. not sure if there is a standard guide that i need to follow though

kencanak avatar Jul 16 '20 06:07 kencanak

@prateekbh , does the current npm run test command works? i am getting error when running it. tried on master branch as well

kencanak avatar Jul 16 '20 07:07 kencanak

Your cdn should be in the same tld, ideally your first party origin (if you don't use SSR which makes it harder)

ForsakenHarmony avatar Jul 21 '20 02:07 ForsakenHarmony