Skip to content

Commit 3b71621

Browse files
davejmTimer
authored andcommitted
Workbox service worker (#4169)
* Add workbox service worker functionality * Remove debug * Set workboxConfig for when there isn't a cra config file * Remove workbox configuration options as c-r-a isn't planning on supporting optional configuration * Remove c-r-a config path from paths * Add workbox service worker functionality * Remove c-r-a config path from paths * Inline the webpack workbox config * Use settings reccommended by @jeffposnick #4169 * Fallback to public url index.html, not root * Add one comment * Update comment * Correct regex
1 parent 328c312 commit 3b71621

File tree

2 files changed

+18
-39
lines changed

2 files changed

+18
-39
lines changed

Diff for: packages/react-scripts/config/webpack.config.prod.js

+16-37
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
1818
const safePostCssParser = require('postcss-safe-parser');
1919
const ManifestPlugin = require('webpack-manifest-plugin');
2020
const InterpolateHtmlPlugin = require('react-dev-utils/InterpolateHtmlPlugin');
21-
const SWPrecacheWebpackPlugin = require('sw-precache-webpack-plugin');
21+
const WorkboxWebpackPlugin = require('workbox-webpack-plugin');
2222
const ModuleScopePlugin = require('react-dev-utils/ModuleScopePlugin');
2323
const getCSSModuleLocalIdent = require('react-dev-utils/getCSSModuleLocalIdent');
2424
const paths = require('./paths');
@@ -481,48 +481,27 @@ module.exports = {
481481
fileName: 'asset-manifest.json',
482482
publicPath: publicPath,
483483
}),
484-
// Generate a service worker script that will precache, and keep up to date,
485-
// the HTML & assets that are part of the Webpack build.
486-
new SWPrecacheWebpackPlugin({
487-
// By default, a cache-busting query parameter is appended to requests
488-
// used to populate the caches, to ensure the responses are fresh.
489-
// If a URL is already hashed by Webpack, then there is no concern
490-
// about it being stale, and the cache-busting can be skipped.
491-
dontCacheBustUrlsMatching: /\.\w{8}\./,
492-
filename: 'service-worker.js',
493-
logger(message) {
494-
if (message.indexOf('Total precache size is') === 0) {
495-
// This message occurs for every build and is a bit too noisy.
496-
return;
497-
}
498-
if (message.indexOf('Skipping static resource') === 0) {
499-
// This message obscures real errors so we ignore it.
500-
// https://github.com/facebook/create-react-app/issues/2612
501-
return;
502-
}
503-
console.log(message);
504-
},
505-
minify: true,
506-
// For unknown URLs, fallback to the index page
507-
navigateFallback: publicUrl + '/index.html',
508-
// Ignores URLs starting from /__ (useful for Firebase):
509-
// https://github.com/facebookincubator/create-react-app/issues/2237#issuecomment-302693219
510-
navigateFallbackWhitelist: [/^(?!\/__).*/],
511-
// Don't precache sourcemaps (they're large) and build asset manifest:
512-
staticFileGlobsIgnorePatterns: [/\.map$/, /asset-manifest\.json$/],
513-
// Disabling skipWaiting ensures better compatibility with web apps that
514-
// use deferred or lazy-loading, at the expense of "keeping around" the
515-
// previously cached version of your web app until all open instances have
516-
// been closed.
517-
// See https://developers.google.com/web/fundamentals/primers/service-workers/lifecycle#skip_the_waiting_phase
518-
skipWaiting: false,
519-
}),
520484
// Moment.js is an extremely popular library that bundles large locale files
521485
// by default due to how Webpack interprets its code. This is a practical
522486
// solution that requires the user to opt into importing specific locales.
523487
// https://github.com/jmblog/how-to-optimize-momentjs-with-webpack
524488
// You can remove this if you don't use Moment.js:
525489
new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
490+
// Generate a service worker script that will precache, and keep up to date,
491+
// the HTML & assets that are part of the Webpack build.
492+
new WorkboxWebpackPlugin.GenerateSW({
493+
clientsClaim: true,
494+
exclude: [/\.map$/, /asset-manifest\.json$/],
495+
importWorkboxFrom: 'local',
496+
navigateFallback: publicUrl + '/index.html',
497+
navigateFallbackBlacklist: [
498+
// Exclude URLs starting with /_, as they're likely an API call
499+
new RegExp('^/_'),
500+
// Exclude URLs containing a dot, as they're likely a resource in
501+
// public/ and not a SPA route
502+
new RegExp('/[^/]+[.][^/]+$'),
503+
],
504+
}),
526505
],
527506
// Some libraries import Node modules but don't use them in the browser.
528507
// Tell Webpack to provide empty mocks for them so importing them works.

Diff for: packages/react-scripts/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,13 @@
5959
"resolve": "1.8.1",
6060
"sass-loader": "7.1.0",
6161
"style-loader": "0.23.0",
62-
"sw-precache-webpack-plugin": "0.11.5",
6362
"terser-webpack-plugin": "1.1.0",
6463
"thread-loader": "1.2.0",
6564
"url-loader": "1.1.1",
6665
"webpack": "4.19.1",
6766
"webpack-dev-server": "3.1.9",
68-
"webpack-manifest-plugin": "2.0.4"
67+
"webpack-manifest-plugin": "2.0.4",
68+
"workbox-webpack-plugin": "3.6.1"
6969
},
7070
"devDependencies": {
7171
"react": "^16.3.2",

0 commit comments

Comments
 (0)