Skip to content

Commit c62f86f

Browse files
author
Marlon Maxwel
committed
feat(react-scripts): add support to generateSW and injectManifest
1 parent ee56192 commit c62f86f

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

packages/react-scripts/config/paths.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ module.exports = {
7373
proxySetup: resolveApp('src/setupProxy.js'),
7474
appNodeModules: resolveApp('node_modules'),
7575
publicUrlOrPath,
76+
serviceWorkerTemplate: resolveModule(resolveApp, 'src/sw-template'),
7677
};
7778

7879
// @remove-on-eject-begin
@@ -100,6 +101,7 @@ module.exports = {
100101
ownNodeModules: resolveOwn('node_modules'), // This is empty on npm 3
101102
appTypeDeclarations: resolveApp('src/react-app-env.d.ts'),
102103
ownTypeDeclarations: resolveOwn('lib/react-app.d.ts'),
104+
serviceWorkerTemplate: resolveModule(resolveApp, 'src/sw-template'),
103105
};
104106

105107
const ownPackageJson = require('../package.json');
@@ -135,6 +137,7 @@ if (
135137
ownNodeModules: resolveOwn('node_modules'),
136138
appTypeDeclarations: resolveOwn(`${templatePath}/src/react-app-env.d.ts`),
137139
ownTypeDeclarations: resolveOwn('lib/react-app.d.ts'),
140+
serviceWorkerTemplate: resolveModule(resolveApp, 'src/sw-template'),
138141
};
139142
}
140143
// @remove-on-eject-end

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ const imageInlineSizeLimit = parseInt(
5353
// Check if TypeScript is setup
5454
const useTypeScript = fs.existsSync(paths.appTsConfig);
5555

56+
// Check if service worker template is setup
57+
const useServiceWorkerTemplate = fs.existsSync(paths.serviceWorkerTemplate);
58+
5659
// style files regexes
5760
const cssRegex = /\.css$/;
5861
const cssModuleRegex = /\.module\.css$/;
@@ -671,6 +674,25 @@ module.exports = function(webpackEnv) {
671674
// Generate a service worker script that will precache, and keep up to date,
672675
// the HTML & assets that are part of the Webpack build.
673676
isEnvProduction &&
677+
!useServiceWorkerTemplate &&
678+
new WorkboxWebpackPlugin.GenerateSW({
679+
clientsClaim: true,
680+
exclude: [/\.map$/, /asset-manifest\.json$/],
681+
importWorkboxFrom: 'cdn',
682+
navigateFallback: paths.publicUrlOrPath + 'index.html',
683+
navigateFallbackDenylist: [
684+
// Exclude URLs starting with /_, as they're likely an API call
685+
new RegExp('^/_'),
686+
// Exclude any URLs whose last part seems to be a file extension
687+
// as they're likely a resource and not a SPA route.
688+
// URLs containing a "?" character won't be blacklisted as they're likely
689+
// a route with query params (e.g. auth callbacks).
690+
new RegExp('/[^/?]+\\.[^/]+$'),
691+
],
692+
}),
693+
// If app has file sw-template.js, workbox use this configuration for generate a advanced config based in template
694+
isEnvProduction &&
695+
useServiceWorkerTemplate &&
674696
new WorkboxWebpackPlugin.InjectManifest({
675697
exclude: [/\.map$/, /asset-manifest\.json$/],
676698
swSrc: `${paths.appSrc}/sw-template.${useTypeScript ? 'ts' : 'js'}`,

0 commit comments

Comments
 (0)