Skip to content

Commit a7d31d9

Browse files
jeffposnickgaearon
authored andcommitted
Determines whether PUBLIC_URL is same-origin before registering SW. (#2432)
1 parent 3e9955d commit a7d31d9

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

packages/react-scripts/template/src/registerServiceWorker.js

+9
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,15 @@
1010

1111
export default function register() {
1212
if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) {
13+
// The URL constructor is available in all browsers that support SW.
14+
const publicUrl = new URL(process.env.PUBLIC_URL, window.location);
15+
if (publicUrl.origin !== window.location.origin) {
16+
// Our service worker won't work if PUBLIC_URL is on a different origin
17+
// from what our page is served on. This might happen if a CDN is used to
18+
// serve assets; see https://github.com/facebookincubator/create-react-app/issues/2374
19+
return;
20+
}
21+
1322
window.addEventListener('load', () => {
1423
const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`;
1524
navigator.serviceWorker

0 commit comments

Comments
 (0)