serviceworker-webpack-plugin icon indicating copy to clipboard operation
serviceworker-webpack-plugin copied to clipboard

Get publicPath at runtime with file://

Open amaury1093 opened this issue 7 years ago • 2 comments
trafficstars

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?

amaury1093 avatar Apr 16 '18 11:04 amaury1093

Would you be able to setup a repo that reproduces the issue with the least amount of code necessary?

woutervanvliet avatar Apr 19 '18 09:04 woutervanvliet

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.

jeniljain29392 avatar Mar 15 '19 00:03 jeniljain29392