question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Failed to register a ServiceWorker when PUBLIC_URL is CDN

See original GitHub issue

Can 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

  1. npm ls react-scripts (if you haven’t ejected): react-scripts@1.0.6
  2. node -v: v7.10.0
  3. npm -v: v4.2.0

Then, specify:

  1. Operating system: macOS
  2. Browser and version: Chrome 58.0.3029.110 (64-bit)

Reproducible Demo

See console output on my website.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:11 (8 by maintainers)

github_iconTop GitHub Comments

4reactions
vincentbelcommented, Jul 28, 2017

Why not also use service worker when PUBLIC_URL env variable set to a CDN?

We can serve index.html and service-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.

0reactions
jeffposnickcommented, Sep 18, 2017

@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 like create-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 the stripPrefixMulti option allows you to remap the precached URLs.)

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found