Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support to customize the Service Worker with InjectManifest and Workbox v5 #8485

Closed
wants to merge 19 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/babel-preset-react-app/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ Make sure you have a `tsconfig.json` file at the root directory. You can also us
```

## Absolute Runtime Paths

Absolute paths are enabled by default for imports. To use relative paths instead, set the `absoluteRuntime` option in `.babelrc` to `false`:

```
Expand Down
5 changes: 4 additions & 1 deletion packages/cra-template-typescript/template.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
"@types/react": "^16.9.0",
"@types/react-dom": "^16.9.0",
"@types/jest": "^24.0.0",
"typescript": "^3.8.0"
"typescript": "^3.8.0",
"workbox-core": "5.0.0",
"workbox-precaching": "5.0.0",
"workbox-routing": "5.0.0"
}
}
}
23 changes: 23 additions & 0 deletions packages/cra-template-typescript/template/src/sw-template.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { clientsClaim } from 'workbox-core';
import { NavigationRoute, registerRoute } from 'workbox-routing';
import { precacheAndRoute, createHandlerBoundToURL } from 'workbox-precaching';

declare let self: ServiceWorkerGlobalScope;

self.addEventListener('message', function(event: ExtendableMessageEvent) {
if (event.data && event.data.type === 'SKIP_WAITING') {
self.skipWaiting();
}
});

clientsClaim();

precacheAndRoute(self.__WB_MANIFEST);

const handler = createHandlerBoundToURL(`${process.env.PUBLIC_URL}/index.html`);

const navigationRoute = new NavigationRoute(handler, {
denylist: [new RegExp('^/_'), new RegExp('/[^/?]+\\.[^/]+$')],
});

registerRoute(navigationRoute);
5 changes: 4 additions & 1 deletion packages/cra-template/template.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
"dependencies": {
"@testing-library/react": "^9.3.2",
"@testing-library/jest-dom": "^4.2.4",
"@testing-library/user-event": "^7.1.2"
"@testing-library/user-event": "^7.1.2",
"workbox-core": "5.0.0",
"workbox-precaching": "5.0.0",
"workbox-routing": "5.0.0"
}
}
}
22 changes: 22 additions & 0 deletions packages/cra-template/template/src/sw-template.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/* eslint-disable no-restricted-globals */
import { clientsClaim } from 'workbox-core';
import { NavigationRoute, registerRoute } from 'workbox-routing';
import { precacheAndRoute, createHandlerBoundToURL } from 'workbox-precaching';

self.addEventListener('message', function(event) {
if (event.data && event.data.type === 'SKIP_WAITING') {
self.skipWaiting();
}
});

clientsClaim();

precacheAndRoute(self.__WB_MANIFEST);

const handler = createHandlerBoundToURL(`${process.env.PUBLIC_URL}/index.html`);

const navigationRoute = new NavigationRoute(handler, {
denylist: [new RegExp('^/_'), new RegExp('/[^/?]+\\.[^/]+$')],
});

registerRoute(navigationRoute);
1,301 changes: 764 additions & 537 deletions packages/create-react-app/yarn.lock.cached

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions packages/react-scripts/config/paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ module.exports = {
proxySetup: resolveApp('src/setupProxy.js'),
appNodeModules: resolveApp('node_modules'),
publicUrlOrPath,
serviceWorkerTemplate: resolveModule(resolveApp, 'src/sw-template'),
};

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

const ownPackageJson = require('../package.json');
Expand Down Expand Up @@ -135,6 +137,10 @@ if (
ownNodeModules: resolveOwn('node_modules'),
appTypeDeclarations: resolveOwn(`${templatePath}/src/react-app-env.d.ts`),
ownTypeDeclarations: resolveOwn('lib/react-app.d.ts'),
serviceWorkerTemplate: resolveModule(
resolveOwn,
`${templatePath}/src/sw-template`
),
};
}
// @remove-on-eject-end
Expand Down
15 changes: 13 additions & 2 deletions packages/react-scripts/config/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ const imageInlineSizeLimit = parseInt(
// Check if TypeScript is setup
const useTypeScript = fs.existsSync(paths.appTsConfig);

// Check if service worker template is setup
const useServiceWorkerTemplate = fs.existsSync(paths.serviceWorkerTemplate);

// style files regexes
const cssRegex = /\.css$/;
const cssModuleRegex = /\.module\.css$/;
Expand Down Expand Up @@ -682,12 +685,12 @@ module.exports = function(webpackEnv) {
// Generate a service worker script that will precache, and keep up to date,
// the HTML & assets that are part of the webpack build.
isEnvProduction &&
!useServiceWorkerTemplate &&
new WorkboxWebpackPlugin.GenerateSW({
clientsClaim: true,
exclude: [/\.map$/, /asset-manifest\.json$/],
importWorkboxFrom: 'cdn',
navigateFallback: paths.publicUrlOrPath + 'index.html',
navigateFallbackBlacklist: [
navigateFallbackDenylist: [
// Exclude URLs starting with /_, as they're likely an API call
new RegExp('^/_'),
// Exclude any URLs whose last part seems to be a file extension
Expand All @@ -697,6 +700,14 @@ module.exports = function(webpackEnv) {
new RegExp('/[^/?]+\\.[^/]+$'),
],
}),
// If app has file sw-template.js, workbox use this configuration for generate a advanced config based in template
isEnvProduction &&
useServiceWorkerTemplate &&
new WorkboxWebpackPlugin.InjectManifest({
exclude: [/\.map$/, /asset-manifest\.json$/],
swSrc: `${paths.appSrc}/sw-template.${useTypeScript ? 'ts' : 'js'}`,
swDest: `${paths.appBuild}/service-worker.js`,
}),
// TypeScript type checking
useTypeScript &&
new ForkTsCheckerWebpackPlugin({
Expand Down
2 changes: 1 addition & 1 deletion packages/react-scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
"webpack": "4.42.0",
"webpack-dev-server": "3.10.3",
"webpack-manifest-plugin": "2.2.0",
"workbox-webpack-plugin": "4.3.1"
"workbox-webpack-plugin": "5.0.0"
},
"devDependencies": {
"react": "^16.12.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ function verifyTypeScriptSetup() {
parsedValue: ts.ScriptTarget.ES5,
suggested: 'es5',
},
lib: { suggested: ['dom', 'dom.iterable', 'esnext'] },
lib: { suggested: ['dom', 'dom.iterable', 'esnext', 'webworker'] },
allowJs: { suggested: true },
skipLibCheck: { suggested: true },
esModuleInterop: { suggested: true },
Expand Down
4 changes: 2 additions & 2 deletions tasks/e2e-installs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ function exists {
# Check for accidental dependencies in package.json
function checkDependencies {
if ! awk '/"dependencies": {/{y=1;next}/},/{y=0; next}y' package.json | \
grep -v -q -E '^\s*"(@testing-library\/.+)|(react(-dom|-scripts)?)"'; then
grep -v -q -E '^\s*"(@testing-library\/.+)|(react(-dom|-scripts)?)|(workbox-\w*)"'; then
echo "Dependencies are correct"
else
echo "There are extraneous dependencies in package.json"
Expand All @@ -62,7 +62,7 @@ function checkDependencies {
# Check for accidental dependencies in package.json
function checkTypeScriptDependencies {
if ! awk '/"dependencies": {/{y=1;next}/},/{y=0; next}y' package.json | \
grep -v -q -E '^\s*"(@testing-library\/.+)|(@types\/.+)|typescript|(react(-dom|-scripts)?)"'; then
grep -v -q -E '^\s*"(@testing-library\/.+)|(@types\/.+)|typescript|(react(-dom|-scripts)?)|(workbox-\w*)"'; then
echo "Dependencies are correct"
else
echo "There are extraneous dependencies in package.json"
Expand Down