Replies: 2 comments 4 replies
-
|
check https://github.com/vite-pwa/vite-plugin-pwa/blob/main/src/vite-build.ts you can use also |
Beta Was this translation helpful? Give feedback.
-
|
Maybe you can create a workbox recipe and use it via generateSW strategy. IIRC workbox will check byte by byte the sw to check if there is a new version, and so, you should use always 200 for the sw and maybe also for the dependencies if any ( |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I need to set up service worker that uses Firebase Cloud Messaging (FCM) for push notifications, so I have to use the
InjectManifeststrategy. I would also like to write the service worker in TypeScript.In the initial version, I've simply used the following service worker code from the documentation:
The service worker generates fine. However, the problem is that all dependencies (currently just workbox) are now bundled in the same
sw.jsfile. As service worker doesn't use hash-based filenames, this means I will be unable to apply the optimal HTTP caching strategy. In comparison, when using the defaultGenerateSWstrategy, the workbox code is split into a separateworkbox-hash.jsfile that is imported fromsw.jsand can be cached separately.I would like to apply the same thing to the custom
InjectManifest-based service worker. For example, that workbox dependency is stored in thesw-workbox-hash.jsfile, and that firebase dependency is in thesw-firebase-hash.js.My first attempt was using the
configureCustomSWViteBuildoption:This correctly splits workbox into a separate
sw-workbox-hash.jsfile. The problem now is that the file is imported from the main service worker file using ES modules, which is not supported in all browsers. Additionally, the filename hash doesn't respect the globalhashCharactersVite/Rollup option (which it did when usingGenerateSWstrategy).I tried to solve the first problem by setting the output format to IIFE:
But that fails the build:
Interestingly,
GenerateSWgenerates split builds just fine without using ES modules.So, is it possible to split service worker dependencies into own files and import them without ES modules, while preferably respecting global Vite/Rollup options (such as
hashCharacters)?Beta Was this translation helpful? Give feedback.
All reactions