Skip to content

Commit

Permalink
Determines whether PUBLIC_URL is same-origin before registering SW. (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffposnick authored and gaearon committed Jun 26, 2017
1 parent 3e9955d commit a7d31d9
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions packages/react-scripts/template/src/registerServiceWorker.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@

export default function register() {
if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) {
// The URL constructor is available in all browsers that support SW.
const publicUrl = new URL(process.env.PUBLIC_URL, window.location);
if (publicUrl.origin !== window.location.origin) {
// Our service worker won't work if PUBLIC_URL is on a different origin
// from what our page is served on. This might happen if a CDN is used to
// serve assets; see https://github.com/facebookincubator/create-react-app/issues/2374
return;
}

window.addEventListener('load', () => {
const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`;
navigator.serviceWorker
Expand Down

0 comments on commit a7d31d9

Please sign in to comment.