-
I'm developing a PWA with Parcel and Elm. I'm trying to cache all my files on install in the Service Worker. The cache API wants string paths to my assets. Using Example code: const someMediaAssets = [
require('./img/background.png'),
require('./img/title.png'),
// etc.
]
// added by parcel-plugin-static-files-copy; no need for require
const staticMediaAssets = [
'/audio/intro.mp3',
'/audio/etc.mp3',
]
// built code assets: these fail
const codeAssets = [
require('/index.html'),
require('/index.js'),
]
async function cacheOnInstall () {
const filesToCache = someMediaAssets.concat(staticMediaAssets, codeAssets)
const cache = await caches.open(CACHE_NAME)
return cache.addAll(filesToCache)
}
self.addEventListener('install', event => {
event.waitUntil(cacheOnInstall())
}) |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 5 replies
-
Looks like there's a plugin for this: parcel-plugin-sw-asset-urls. Though in the end I switched to using Workbox and no longer have the need. It appears the plugin plays havoc with ESM module imports, so beware. |
Beta Was this translation helpful? Give feedback.
-
Can't you |
Beta Was this translation helpful? Give feedback.
-
Parcel has special import syntax support so you can do special imports like |
Beta Was this translation helpful? Give feedback.
Looks like there's a plugin for this: parcel-plugin-sw-asset-urls. Though in the end I switched to using Workbox and no longer have the need. It appears the plugin plays havoc with ESM module imports, so beware.