Skip to content

Commit

Permalink
minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
slorber committed Jul 8, 2020
1 parent 8112260 commit 788f831
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 16 deletions.
2 changes: 1 addition & 1 deletion packages/docusaurus-plugin-pwa/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function getSWBabelLoader() {
],
],
plugins: [
'@babel/plugin-proposal-object-rest-spread',
require.resolve('@babel/plugin-proposal-object-rest-spread'),
require.resolve('@babel/plugin-proposal-optional-chaining'),
require.resolve('@babel/plugin-proposal-nullish-coalescing-operator'),
],
Expand Down
58 changes: 45 additions & 13 deletions packages/docusaurus-plugin-pwa/src/registerSw.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ const debug = PWA_DEBUG; // shortcut
async function clearRegistrations() {
const registrations = await navigator.serviceWorker.getRegistrations();
if (debug && registrations.length > 0) {
console.log(`Docusaurus PWA: unregister service workers`, registrations);
console.log(
`[Docusaurus-PWA][registerSW]: unregister service workers`,
registrations,
);
}
registrations.forEach((registration) => {
registration.registration.unregister();
Expand Down Expand Up @@ -55,12 +58,12 @@ function isOfflineModeEnabled() {
};
if (enabled) {
console.log(
'Docusaurus PWA: offline mode enabled, because of activation strategies',
'[Docusaurus-PWA][registerSW]: offline mode enabled, because of activation strategies',
logObject,
);
} else {
console.log(
'Docusaurus PWA: offline mode disabled, because none of the offlineModeActivationStrategies could be used',
'[Docusaurus-PWA][registerSW]: offline mode disabled, because none of the offlineModeActivationStrategies could be used',
logObject,
);
}
Expand All @@ -74,7 +77,10 @@ function createServiceWorkerUrl(params) {
paramsQueryString,
)}`;
if (debug) {
console.log(`Docusaurus PWA: service worker url`, {url, params});
console.log(`[Docusaurus-PWA][registerSW]: service worker url`, {
url,
params,
});
}
return url;
}
Expand All @@ -85,7 +91,7 @@ function createServiceWorkerUrl(params) {
}

if (debug) {
console.log('Docusaurus PWA: debug mode enabled');
console.log('[Docusaurus-PWA][registerSW]: debug mode enabled');
}

if ('serviceWorker' in navigator) {
Expand All @@ -101,6 +107,9 @@ function createServiceWorkerUrl(params) {
const sendSkipWaiting = () => wb.messageSW({type: 'SKIP_WAITING'});

const handleServiceWorkerWaiting = async () => {
if (debug) {
console.log('[Docusaurus-PWA][registerSW]: handleServiceWorkerWaiting');
}
// Immediately load new service worker when files aren't cached
if (!offlineMode) {
sendSkipWaiting();
Expand All @@ -117,20 +126,38 @@ function createServiceWorkerUrl(params) {
}
};

if (debug) {
if (registration.active) {
console.log(
'[Docusaurus-PWA][registerSW]: registration.active',
registration,
);
}
if (registration.installing) {
console.log(
'[Docusaurus-PWA][registerSW]: registration.installing',
registration,
);
}
if (registration.waiting) {
console.log(
'[Docusaurus-PWA][registerSW]: registration.waiting',
registration,
);
}
}

// Update service worker if the next one is already in the waiting state.
// This happens when the user doesn't click on `reload` in the popup.
if (registration.waiting) {
if (debug) {
console.log('Docusaurus PWA: registration.waiting', registration);
}
handleServiceWorkerWaiting();
}

// Update the current service worker when the next one has finished
// installing and transitions to waiting state.
wb.addEventListener('waiting', (event) => {
if (debug) {
console.log('Docusaurus PWA: event waiting', event);
console.log('[Docusaurus-PWA][registerSW]: event waiting', event);
}
handleServiceWorkerWaiting();
});
Expand All @@ -139,14 +166,17 @@ function createServiceWorkerUrl(params) {
// moves to waiting state in another tab.
wb.addEventListener('externalwaiting', (event) => {
if (debug) {
console.log('Docusaurus PWA: event externalwaiting', event);
console.log(
'[Docusaurus-PWA][registerSW]: event externalwaiting',
event,
);
}
handleServiceWorkerWaiting();
});

window.addEventListener('appinstalled', (event) => {
if (debug) {
console.log('Docusaurus PWA: event appinstalled', event);
console.log('[Docusaurus-PWA][registerSW]: event appinstalled', event);
}
localStorage.setItem(APP_INSTALLED_KEY, true);

Expand All @@ -159,7 +189,7 @@ function createServiceWorkerUrl(params) {

window.addEventListener('beforeinstallprompt', (event) => {
if (debug) {
console.log('Docusaurus PWA: event appinstalled', event);
console.log('[Docusaurus-PWA][registerSW]: event appinstalled', event);
}
// TODO instead of default browser install UI, show custom docusaurus prompt?
// event.preventDefault();
Expand All @@ -174,6 +204,8 @@ function createServiceWorkerUrl(params) {
}
});
} else if (debug) {
console.log('Docusaurus PWA: browser does not support service workers');
console.log(
'[Docusaurus-PWA][registerSW]: browser does not support service workers',
);
}
})();
9 changes: 7 additions & 2 deletions packages/docusaurus-plugin-pwa/src/sw.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function parseSwParams() {
new URLSearchParams(self.location.search).get('params'),
);
if (params.debug) {
console.log('Docusaurus PWA: Service Worker params:', params);
console.log('[Docusaurus-PWA][SW]: Service Worker params:', params);
}
return params;
}
Expand All @@ -29,7 +29,7 @@ async function runSWCustomCode(params) {
customSW.default(params);
} else if (params.debug) {
console.warn(
'Docusaurus PWA plugin swCustom should have a default export function',
'[Docusaurus-PWA][SW]: swCustom should have a default export function',
);
}
}
Expand Down Expand Up @@ -93,6 +93,11 @@ function getPossibleURLs(url) {
for (let i = 0; i < possibleURLs.length; i += 1) {
const cacheKey = controller.getCacheKeyForURL(possibleURLs[i]);
if (cacheKey) {
if (params.debug) {
console.log('[Docusaurus-PWA][SW]: serving cached asset', {
url: event.request.url,
});
}
event.respondWith(caches.match(cacheKey));
break;
}
Expand Down

0 comments on commit 788f831

Please sign in to comment.