Skip to content

Commit

Permalink
resolved service worker not registering
Browse files Browse the repository at this point in the history
  • Loading branch information
monsieurBoutte committed Feb 4, 2019
1 parent 23d444e commit 9f5b947
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 69 deletions.
16 changes: 0 additions & 16 deletions config-overrides.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,10 @@ module.exports = function override(config, env) {
const workboxConfig = {
...defaultInjectConfig,
swSrc: path.join(__dirname, 'src', 'customServiceWorker.js'),
swDest: path.join(__dirname, 'public', 'custom-service-worker.js'),
importWorkboxFrom: 'local'
};
config = rewireWorkboxInject(workboxConfig)(config, env);
}

return config;
};

// module.exports = function override(config, env) {
// config.plugins = config.plugins.map(plugin => {
// if (plugin.constructor.name === 'GenerateSW') {
// return new WorkboxWebpackPlugin.InjectManifest({
// swSrc: path.join(__dirname, 'src', 'customServiceWorker.js'),
// swDest: path.join(__dirname, 'public', 'custom-service-worker.js')
// });
// }

// return plugin;
// });

// return config;
// };
42 changes: 0 additions & 42 deletions public/custom-service-worker.js

This file was deleted.

2 changes: 1 addition & 1 deletion src/customServiceWorker.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ workbox.core.setLogLevel(workbox.core.LOG_LEVELS.debug);

workbox.core.setCacheNameDetails({
prefix: 'git-lyrics',
suffix: 'v1'
suffix: 'v2'
});

self.addEventListener('install', event => event.waitUntil(self.skipWaiting()));
Expand Down
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';
import ReactDOM from 'react-dom';
import { StoreProvider } from 'easy-peasy';
import store from './configureStore';
import * as serviceWorker from './serviceWorker';
import registerServiceWorker from './serviceWorker';
import { MuiThemeProvider, createMuiTheme } from '@material-ui/core/styles';
import { Routes } from './routes';
import { theme } from './styles/custom-theme-style';
Expand All @@ -22,4 +22,4 @@ ReactDOM.render(
);

// Learn more about service workers in CRA: http://bit.ly/CRA-PWA
serviceWorker.register();
registerServiceWorker();
16 changes: 8 additions & 8 deletions src/serviceWorker.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,35 +20,35 @@ const isLocalhost = Boolean(
)
);

export function register(config) {
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.href);
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/facebook/create-react-app/issues/2374
// serve assets; see https://github.com/facebookincubator/create-react-app/issues/2374
return;
}

window.addEventListener('load', () => {
const swUrl = `${process.env.PUBLIC_URL}/customServiceWorker.js`;

if (isLocalhost) {
// This is running on localhost. Let's check if a service worker still exists or not.
checkValidServiceWorker(swUrl, config);
// This is running on localhost. Lets check if a service worker still exists or not.
checkValidServiceWorker(swUrl);

// Add some additional logging to localhost, pointing developers to the
// service worker/PWA documentation.
navigator.serviceWorker.ready.then(() => {
console.log(
'This web app is being served cache-first by a service ' +
'worker. To learn more, visit http://bit.ly/CRA-PWA'
'worker. To learn more, visit https://goo.gl/SC7cgQ'
);
});
} else {
// Is not localhost. Just register service worker
registerValidSW(swUrl, config);
// Is not local host. Just register service worker
registerValidSW(swUrl);
}
});
}
Expand Down

0 comments on commit 9f5b947

Please sign in to comment.