Commit 39126a10 authored by Piotr's avatar Piotr Committed by Dan Abramov
Browse files

Provide callbacks in serviceWorker (next) (#3867)

* Provide callbacks in serviceWorker

* revert exporting default function
parent 6b67f4cf
Showing with 16 additions and 6 deletions
+16 -6
...@@ -18,7 +18,7 @@ const isLocalhost = Boolean( ...@@ -18,7 +18,7 @@ const isLocalhost = Boolean(
) )
); );
export function register() { export function register(config) {
if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) { if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) {
// The URL constructor is available in all browsers that support SW. // The URL constructor is available in all browsers that support SW.
const publicUrl = new URL(process.env.PUBLIC_URL, window.location); const publicUrl = new URL(process.env.PUBLIC_URL, window.location);
...@@ -34,7 +34,7 @@ export function register() { ...@@ -34,7 +34,7 @@ export function register() {
if (isLocalhost) { if (isLocalhost) {
// This is running on localhost. Lets check if a service worker still exists or not. // This is running on localhost. Lets check if a service worker still exists or not.
checkValidServiceWorker(swUrl); checkValidServiceWorker(swUrl, config);
// Add some additional logging to localhost, pointing developers to the // Add some additional logging to localhost, pointing developers to the
// service worker/PWA documentation. // service worker/PWA documentation.
...@@ -46,13 +46,13 @@ export function register() { ...@@ -46,13 +46,13 @@ export function register() {
}); });
} else { } else {
// Is not local host. Just register service worker // Is not local host. Just register service worker
registerValidSW(swUrl); registerValidSW(swUrl, config);
} }
}); });
} }
} }
function registerValidSW(swUrl) { function registerValidSW(swUrl, config) {
navigator.serviceWorker navigator.serviceWorker
.register(swUrl) .register(swUrl)
.then(registration => { .then(registration => {
...@@ -66,11 +66,21 @@ function registerValidSW(swUrl) { ...@@ -66,11 +66,21 @@ function registerValidSW(swUrl) {
// It's the perfect time to display a "New content is // It's the perfect time to display a "New content is
// available; please refresh." message in your web app. // available; please refresh." message in your web app.
console.log('New content is available; please refresh.'); console.log('New content is available; please refresh.');
// Execute callback
if (config.onUpdate) {
config.onUpdate(registration);
}
} else { } else {
// At this point, everything has been precached. // At this point, everything has been precached.
// It's the perfect time to display a // It's the perfect time to display a
// "Content is cached for offline use." message. // "Content is cached for offline use." message.
console.log('Content is cached for offline use.'); console.log('Content is cached for offline use.');
// Execute callback
if (config.onSuccess) {
config.onSuccess(registration);
}
} }
} }
}; };
...@@ -81,7 +91,7 @@ function registerValidSW(swUrl) { ...@@ -81,7 +91,7 @@ function registerValidSW(swUrl) {
}); });
} }
function checkValidServiceWorker(swUrl) { function checkValidServiceWorker(swUrl, config) {
// Check if the service worker can be found. If it can't reload the page. // Check if the service worker can be found. If it can't reload the page.
fetch(swUrl) fetch(swUrl)
.then(response => { .then(response => {
...@@ -98,7 +108,7 @@ function checkValidServiceWorker(swUrl) { ...@@ -98,7 +108,7 @@ function checkValidServiceWorker(swUrl) {
}); });
} else { } else {
// Service worker found. Proceed as normal. // Service worker found. Proceed as normal.
registerValidSW(swUrl); registerValidSW(swUrl, config);
} }
}) })
.catch(() => { .catch(() => {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment