Skip to content
This repository has been archived by the owner on Sep 23, 2023. It is now read-only.

Commit

Permalink
Merge branch 'main' of https://github.com/remix-pwa/sw
Browse files Browse the repository at this point in the history
  • Loading branch information
ShafSpecs committed Sep 7, 2023
2 parents 85f3ca4 + 3c0b1a0 commit 7ccddad
Showing 1 changed file with 39 additions and 27 deletions.
66 changes: 39 additions & 27 deletions src/react/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,21 @@ declare global {

export type LoadServiceWorkerOptions = RegistrationOptions & {
serviceWorkerUrl?: string;
serviceWorkerregistraionCallback?: (
registration: ServiceWorkerRegistration
) => Promise<void> | void;
};


/**
* Load service worker in `entry.client` when the client gets hydrated.
*
* All parameters are optional.
*
* @param options - Options for loading the service worker.
* @param options.serviceWorkerUrl='/entry.worker.js' - URL of the service worker.
* @param options.serviceWorkerregistraionCallback - Callback function when the service worker gets registered.
* @param ...options.registrationOptions - Options for the service worker registration.
* @returns
* @returns
* ```ts
* loadServiceWorker({
* scope: "/",
Expand All @@ -31,38 +34,47 @@ export type LoadServiceWorkerOptions = RegistrationOptions & {
* ```
*/
export function loadServiceWorker(
{serviceWorkerUrl, ...options}: LoadServiceWorkerOptions = {
{
serviceWorkerUrl,
serviceWorkerregistraionCallback,
...options
}: LoadServiceWorkerOptions = {
scope: '/',
serviceWorkerUrl: '/entry.worker.js'
serviceWorkerUrl: '/entry.worker.js',
serviceWorkerregistraionCallback: () => {}
}
) {
if ('serviceWorker' in navigator) {
async function register() {
const syncRemixManifest = (
serviceWorker: ServiceWorkerContainer = navigator.serviceWorker
) => {
serviceWorker.controller?.postMessage({
type: 'SYNC_REMIX_MANIFEST',
manifest: window.__remixManifest
});
};

try {
await navigator.serviceWorker
//@ts-ignore
.register(serviceWorkerUrl, options)
.then(() => navigator.serviceWorker.ready)
.then(() => {
const registration = await navigator.serviceWorker.register(
serviceWorkerUrl!,
options
);

await serviceWorkerregistraionCallback?.(registration);

await navigator.serviceWorker.ready;

logger.debug('Syncing manifest...');

if (navigator.serviceWorker.controller) {
syncRemixManifest();
} else {
navigator.serviceWorker.addEventListener('controllerchange', () => {
logger.debug('Syncing manifest...');
if (navigator.serviceWorker.controller) {
navigator.serviceWorker.controller.postMessage({
type: 'SYNC_REMIX_MANIFEST',
manifest: window.__remixManifest
});
} else {
navigator.serviceWorker.addEventListener(
'controllerchange',
() => {
logger.debug('Syncing manifest...');
navigator.serviceWorker.controller?.postMessage({
type: 'SYNC_REMIX_MANIFEST',
manifest: window.__remixManifest
});
}
);
}
syncRemixManifest();
});
}
} catch (error) {
// console.error('Service worker registration failed', error);
}
Expand All @@ -77,4 +89,4 @@ export function loadServiceWorker(
window.addEventListener('load', register);
}
}
}
}

0 comments on commit 7ccddad

Please sign in to comment.