#[PWA] Service Worker in React Router v7

1 messages · Page 1 of 1 (latest)

gusty glade
#

Is there a way to compile/build/generate a service worker file (from a TypeScript file) that is deployed along side the app so that it can be registered on the client via navigator.serviceWorker.register("/sw.js")?

Ideally so that pnpm dev and pnpm build already take care of building that (so I'm assuming with a Vite plugin)?

narrow ruin
#

It's a vite plugin, yeah. You still have full control of the vite config, so you can add whatever you need there to support building/bundling the service worker entrypoint.

gusty glade
#

I was wondering if anybody else in my situation were successful

narrow ruin
gusty glade
narrow ruin
#

Looks like you can get use the ?worker&url import suffix to get at the file as a URL:

import swUrl from './sw?worker&url';

navigator.serviceWorker.register(swUrl);

From the vite docs:

vitejs

Next Generation Frontend Tooling

#

the section is oriented towards Web Workers but it seems to do what you want