sw-precache-webpack-plugin: wepback public path make index.html cache not working.

  • I’m submitting a bug report
  • I’m submitting a feature request
  • I’m submitting a support request

webpack version: 2.2.1

sw-precache-webpack-plugin version: 0.9.2

Please tell us about your environment: OSX 10.12.4

Browser: [Chrome 58.0.3029.110 (64-bit)]

Current behavior:

// webpack.config.js
...
output: {
  publichPath: 'https://cdn.com/'
},
...
plugins: [
  new SWPrecacheWebpackPlugin({
    filename: 'sw.js',
    staticFileGlobsIgnorePatterns: [/\.map$/]
  })
]
//sw.js
...
var precacheConfig = [
  ['https://cdn.com/index.html', '1927d0eabd7c5d5d786731cf24b2675b']
...

That https://cdn.com/index.html make the index.html cache won’t work. Cause the request url won’t match.

Expected/desired behavior: index.html can be cached properly.

If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem along with your:

  • Webpack configuration:
// webpack.config.js
...
output: {
  publichPath: 'https://cdn.com/'
},
...
plugins: [
  new SWPrecacheWebpackPlugin({
    filename: 'sw.js',
    staticFileGlobsIgnorePatterns: [/\.map$/]
  })
]
  • Generated service worker (not minified):
//sw.js
...
var precacheConfig = [
  ['https://cdn.com/index.html', '1927d0eabd7c5d5d786731cf24b2675b']
...

About this issue

  • Original URL
  • State: open
  • Created 7 years ago
  • Comments: 27 (6 by maintainers)

Commits related to this issue

Most upvoted comments

👍 For exposing an API for handling this use case. I have the exact same need as well. Some assets come from a CDN while some (index.html) need to come from the applications url.

new SWPrecacheWebpackPlugin({
      cacheId: 'fancy',
      filename: 'service-worker.js',
      // staticFileGlobs: ['dist/**/*.{js,html,css}'],
      staticFileGlobs: ['dist/index.html'],
      staticFileGlobsIgnorePatterns:[/\.map$/],
      // minify: true, todo: close in deploy
      mergeStaticsConfig:true,
      stripPrefix: 'dist/'
    })

it will output index.html alone image

Seems like a legitimate feature request, will think about an api for this unless you have a suggestion @darrylsepeda

I got the same issue with @lili21. All of my files is cached, but my webpack public path is directed to the other URL as explained by @lili21. So I can’t refresh my page while offline because my index file is pointed to cdn.

I’ve read @jeffposnick reference, but still can’t find any solution. What he actually did is comparing the public path and the origin then if it is different, dissable the Service Worker.

It is useless. My goal is to make this SW works no matter if the public path and the origin is different. Is there any way to overwrite the SW generated by sw-precache-webpack-plugin? so my index file can be cache correctly (not using webpack public path).

I’ve tried to overwrite my service worker generated by sw-precache-webpack-plugin manually from http://cdn.com/index.html to index.html and the offline mode works well, but I can’t make it that way since it won’t be efficient if we should maintain our service worker file manually everytime someone re-build our app.

Related: https://github.com/facebookincubator/create-react-app/pull/2432

(I’m not sure this is a supported use case.)