diff --git a/packages/react-scripts/config/webpack.config.prod.js b/packages/react-scripts/config/webpack.config.prod.js index 8fe4f6189c6..55692cbf38a 100644 --- a/packages/react-scripts/config/webpack.config.prod.js +++ b/packages/react-scripts/config/webpack.config.prod.js @@ -18,7 +18,7 @@ const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin'); const safePostCssParser = require('postcss-safe-parser'); const ManifestPlugin = require('webpack-manifest-plugin'); const InterpolateHtmlPlugin = require('react-dev-utils/InterpolateHtmlPlugin'); -const SWPrecacheWebpackPlugin = require('sw-precache-webpack-plugin'); +const WorkboxWebpackPlugin = require('workbox-webpack-plugin'); const ModuleScopePlugin = require('react-dev-utils/ModuleScopePlugin'); const getCSSModuleLocalIdent = require('react-dev-utils/getCSSModuleLocalIdent'); const paths = require('./paths'); @@ -481,48 +481,27 @@ module.exports = { fileName: 'asset-manifest.json', publicPath: publicPath, }), - // Generate a service worker script that will precache, and keep up to date, - // the HTML & assets that are part of the Webpack build. - new SWPrecacheWebpackPlugin({ - // By default, a cache-busting query parameter is appended to requests - // used to populate the caches, to ensure the responses are fresh. - // If a URL is already hashed by Webpack, then there is no concern - // about it being stale, and the cache-busting can be skipped. - dontCacheBustUrlsMatching: /\.\w{8}\./, - filename: 'service-worker.js', - logger(message) { - if (message.indexOf('Total precache size is') === 0) { - // This message occurs for every build and is a bit too noisy. - return; - } - if (message.indexOf('Skipping static resource') === 0) { - // This message obscures real errors so we ignore it. - // https://github.com/facebook/create-react-app/issues/2612 - return; - } - console.log(message); - }, - minify: true, - // For unknown URLs, fallback to the index page - navigateFallback: publicUrl + '/index.html', - // Ignores URLs starting from /__ (useful for Firebase): - // https://github.com/facebookincubator/create-react-app/issues/2237#issuecomment-302693219 - navigateFallbackWhitelist: [/^(?!\/__).*/], - // Don't precache sourcemaps (they're large) and build asset manifest: - staticFileGlobsIgnorePatterns: [/\.map$/, /asset-manifest\.json$/], - // Disabling skipWaiting ensures better compatibility with web apps that - // use deferred or lazy-loading, at the expense of "keeping around" the - // previously cached version of your web app until all open instances have - // been closed. - // See https://developers.google.com/web/fundamentals/primers/service-workers/lifecycle#skip_the_waiting_phase - skipWaiting: false, - }), // Moment.js is an extremely popular library that bundles large locale files // by default due to how Webpack interprets its code. This is a practical // solution that requires the user to opt into importing specific locales. // https://github.com/jmblog/how-to-optimize-momentjs-with-webpack // You can remove this if you don't use Moment.js: new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/), + // Generate a service worker script that will precache, and keep up to date, + // the HTML & assets that are part of the Webpack build. + new WorkboxWebpackPlugin.GenerateSW({ + clientsClaim: true, + exclude: [/\.map$/, /asset-manifest\.json$/], + importWorkboxFrom: 'local', + navigateFallback: publicUrl + '/index.html', + navigateFallbackBlacklist: [ + // Exclude URLs starting with /_, as they're likely an API call + new RegExp('^/_'), + // Exclude URLs containing a dot, as they're likely a resource in + // public/ and not a SPA route + new RegExp('/[^/]+[.][^/]+$'), + ], + }), ], // Some libraries import Node modules but don't use them in the browser. // Tell Webpack to provide empty mocks for them so importing them works. diff --git a/packages/react-scripts/package.json b/packages/react-scripts/package.json index b2b1d970c55..0738b4b74ab 100644 --- a/packages/react-scripts/package.json +++ b/packages/react-scripts/package.json @@ -59,13 +59,13 @@ "resolve": "1.8.1", "sass-loader": "7.1.0", "style-loader": "0.23.0", - "sw-precache-webpack-plugin": "0.11.5", "terser-webpack-plugin": "1.1.0", "thread-loader": "1.2.0", "url-loader": "1.1.1", "webpack": "4.19.1", "webpack-dev-server": "3.1.9", - "webpack-manifest-plugin": "2.0.4" + "webpack-manifest-plugin": "2.0.4", + "workbox-webpack-plugin": "3.6.1" }, "devDependencies": { "react": "^16.3.2",