Failed to register a ServiceWorker when PUBLIC_URL is CDN
See original GitHub issueCan you reproduce the problem with latest npm?
Yes.
Description
When set PUBLIC_URL
to a cdn address, service worker registration is blocked. It seems a cross-domain problem:
Error during service worker registration: DOMException: Failed to register a ServiceWorker: The origin of the provided scriptURL (‘https://o0tu0aq0f.qnssl.com’) does not match the current origin (‘https://apporz.com’).
Expected behavior
ServiceWorker should work well when PUBLIC_URL
isn’t local host.
Actual behavior
ServiceWorker doesn’t work.
Environment
npm ls react-scripts
(if you haven’t ejected): react-scripts@1.0.6node -v
: v7.10.0npm -v
: v4.2.0
Then, specify:
- Operating system: macOS
- Browser and version: Chrome 58.0.3029.110 (64-bit)
Reproducible Demo
See console output on my website.
Issue Analytics
- State:
- Created 6 years ago
- Comments:11 (8 by maintainers)
Top Results From Across the Web
Failed to register a ServiceWorker when PUBLIC_URL is ...
I want to load my react web application from cdn using service worker and set PUBLIC_URL to my CDN url at build time....
Read more >Failed To Register A Serviceworker When Public_Url Is ...
After some trial error and success I decided to write this article to demonstrate The first step is updating the serviceWorker.js file within...
Read more >serviceWorker.js in src – GeniusWebApp
1, // This optional code is used to register a service worker. ... 28, // Our service worker won't work if PUBLIC_URL is...
Read more >src/client/registerServiceWorker.ts · skava/@skava/ui
This might happen if a CDN is used to // serve assets; ... Just register service worker registerValidSW(swUrl) } else { // This...
Read more >Source of serviceWorker.js - naamesScrape
This optional code is used to register a service worker. ... Our service worker won't work if PUBLIC_URL is on a different origin....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Why not also use service worker when
PUBLIC_URL
env variable set to a CDN?We can serve
index.html
andservice-worker.js
in our server(the same origin) and put other js/css/images assets into our CDN server. And then using service worker to cache assets in CDN server.@hartono-sulaiman-kaskus, what you describe is expected after https://github.com/facebookincubator/create-react-app/pull/2432 went into effect.
The history behind that PR can be found upthread. There are definitely scenarios in which you can configure everything properly to safely use a service worker while retrieving all of your assets from a CDN, but doing so requires a lot of assumptions (such as: your CDN supports CORS, that the deployment of your
service-worker.js
and CDN happens at exactly the same time, etc.). It’s not safe to make those assumptions in a general-purpose project likecreate-react-app
.(If you want to manually update your service worker configuration, post-
eject
, because you know that the way you’re using a CDN is “safe” from the issues I mentioned above, then thestripPrefixMulti
option allows you to remap the precached URLs.)