Skip to content

Commit 0cf67b8

Browse files
committed
Switched to WorkboxWebpackPlugin.InjectManifest from WorkboxWebpackPlugin.GenerateSW
1 parent 4604c5e commit 0cf67b8

File tree

2 files changed

+15
-14
lines changed

2 files changed

+15
-14
lines changed

packages/react-scripts/config/paths.js

+3
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ module.exports = {
8181
appPublic: resolveApp('public'),
8282
appHtml: resolveApp('public/index.html'),
8383
appIndexJs: resolveModule(resolveApp, 'src/index'),
84+
appServiceWorkerJs: resolveModule(resolveApp, 'src/service-worker'),
8485
appPackageJson: resolveApp('package.json'),
8586
appSrc: resolveApp('src'),
8687
appTsConfig: resolveApp('tsconfig.json'),
@@ -104,6 +105,7 @@ module.exports = {
104105
appPublic: resolveApp('public'),
105106
appHtml: resolveApp('public/index.html'),
106107
appIndexJs: resolveModule(resolveApp, 'src/index'),
108+
appServiceWorkerJs: resolveModule(resolveApp, 'src/service-worker'),
107109
appPackageJson: resolveApp('package.json'),
108110
appSrc: resolveApp('src'),
109111
appTsConfig: resolveApp('tsconfig.json'),
@@ -140,6 +142,7 @@ if (
140142
appPublic: resolveOwn(`${templatePath}/public`),
141143
appHtml: resolveOwn(`${templatePath}/public/index.html`),
142144
appIndexJs: resolveModule(resolveOwn, `${templatePath}/src/index`),
145+
appServiceWorkerJs: resolveModule(resolveOwn, `${templatePath}/src/service-worker`),
143146
appPackageJson: resolveOwn('package.json'),
144147
appSrc: resolveOwn(`${templatePath}/src`),
145148
appTsConfig: resolveOwn(`${templatePath}/tsconfig.json`),

packages/react-scripts/config/webpack.config.js

+12-14
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ const imageInlineSizeLimit = parseInt(
5050
process.env.IMAGE_INLINE_SIZE_LIMIT || '10000'
5151
);
5252

53+
// Check if Service Worker is setup
54+
const useServiceWorker = fs.existsSync(paths.appServiceWorkerJs);
55+
5356
// Check if TypeScript is setup
5457
const useTypeScript = fs.existsSync(paths.appTsConfig);
5558

@@ -170,6 +173,9 @@ module.exports = function(webpackEnv) {
170173
// require.resolve('webpack/hot/dev-server'),
171174
isEnvDevelopment &&
172175
require.resolve('react-dev-utils/webpackHotDevClient'),
176+
// Add service worker if it's available.
177+
isEnvProduction && useServiceWorker &&
178+
paths.appServiceWorkerJs,
173179
// Finally, this is your app's code:
174180
paths.appIndexJs,
175181
// We include the app code last so that if there is a runtime error during
@@ -674,23 +680,15 @@ module.exports = function(webpackEnv) {
674680
// https://github.com/jmblog/how-to-optimize-momentjs-with-webpack
675681
// You can remove this if you don't use Moment.js:
676682
new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
677-
// Generate a service worker script that will precache, and keep up to date,
678-
// the HTML & assets that are part of the Webpack build.
683+
// Inject a manifest into a service worker script that would allow
684+
// precaching, and keep up to date, the HTML & assets that are part of
685+
// the Webpack build.
679686
isEnvProduction &&
680-
new WorkboxWebpackPlugin.GenerateSW({
681-
clientsClaim: true,
687+
useServiceWorker &&
688+
new WorkboxWebpackPlugin.InjectManifest({
689+
swSrc: paths.appServiceWorkerJs,
682690
exclude: [/\.map$/, /asset-manifest\.json$/],
683691
importWorkboxFrom: 'cdn',
684-
navigateFallback: publicUrl + '/index.html',
685-
navigateFallbackBlacklist: [
686-
// Exclude URLs starting with /_, as they're likely an API call
687-
new RegExp('^/_'),
688-
// Exclude any URLs whose last part seems to be a file extension
689-
// as they're likely a resource and not a SPA route.
690-
// URLs containing a "?" character won't be blacklisted as they're likely
691-
// a route with query params (e.g. auth callbacks).
692-
new RegExp('/[^/?]+\\.[^/]+$'),
693-
],
694692
}),
695693
// TypeScript type checking
696694
useTypeScript &&

0 commit comments

Comments
 (0)