NekR / offline-plugin

Offline plugin (ServiceWorker, AppCache) for webpack (https://webpack.js.org/)
MIT License
4.52k stars 294 forks source link

Only cache webpack assets and not other routes? #407

Open 0ui opened 6 years ago

0ui commented 6 years ago

I have a small Express.js app serving my SPA. It routes all requests to my index.html which serves the SPA javascript. The only exception is a route /special/* which express should handle, except that the ServiceWorker is intercepting that and any other request that's not for a specific asset and serving index.html 200 OK. I've tried the example SPA configuration, the one from react-boilerplate, and several more.

What settings would allow the ServiceWorker to cache all requests for the assets compiled in by Webpack but skip caching and let express handle all other requests to /any-random-path?

Apologies if this is covered but I've exhaustively searched the documentation, issues, and to some extent the code to try and solve this. Thanks for any help.

GGAlanSmithee commented 5 years ago

@0ui serving all webpack-generated assets would be the more or less the default behavior given a minimal config.

Can you please:

  1. Tell us the actual results?
  2. Clearly state what is wrong (i.e. some assets are not being cached but you would like them too, or some assets are being cached and you would like them to not be)
  3. Can you please share your config?

PS. Here is a basic config that caches all webpack-generated assets without exceptions:

            new OfflinePlugin({
                relativePaths: false,
                publicPath: '/',

                // No need to cache .htaccess. See http://mxs.is/googmp,
                // this is applied before any match in `caches` section
                excludes: ['.htaccess'],

                caches: {
                    // All chunks marked as `additional`, loaded after main section
                    // and do not prevent SW to install. Change to `optional` if
                    // do not want them to be preloaded at all (cached only when first loaded)
                    additional: [
                        ':externals:',
                        '*.chunk.js'
                    ],
                    optional: [
                        ':rest:'
                    ]
                },

                externals: ['/'],

                // Removes warning for `additional` section usage
                safeToUseOptionalCaches: true,

                ServiceWorker: {
                    events: true,
                    navigateFallbackURL: '/'
                },

                AppCache: false
            })