serviceworker-webpack-plugin
serviceworker-webpack-plugin copied to clipboard
Get publicPath at runtime with file://
Config 1
My webpack config is
plugins: [
new ServiceWorkerWebpackPlugin({
entry: path.join(__dirname, 'src/sw.js'),
publicPath: '/'
}),
],
which ouputs a sw.js file in a build/ folder.
I then run Electron, which loads a .html file via the file:// protocol. The bundled js loads file:///sw.js, and I get a network error saying the service worker file cannot be fetched.
Config 2
I change the webpack config to:
plugins: [
new ServiceWorkerWebpackPlugin({
entry: path.join(__dirname, 'src/sw.js'),
publicPath: path.join(__dirname, 'build/')
}),
],
When I run electron, the bundled js loads file:///Users/amaurymartiny/path/to/build/sw.js, which is correct, and my service worker is loaded.
However this path is hardcoded into the bundled js, since it's done at compile time, so if I package the electron app and run it on another computer, I get another sw file cannot be fetched error.
Question
Is it possible to set the path of sw.js at runtime?
Would you be able to setup a repo that reproduces the issue with the least amount of code necessary?
I am working on an electron-vue project and I tried to use this plugin but I am facing the same issue. It works in development mode. But for production it throws a network error saying the service worker file cannot be fetched.