Skip to content

Commit 2f579f8

Browse files
author
Marlon Maxwel
committed
feat(cra-template): add sw-template for builded with Workbox
1 parent 3d457d8 commit 2f579f8

File tree

3 files changed

+24
-24
lines changed

3 files changed

+24
-24
lines changed

packages/cra-template/template/src/sw.js renamed to packages/cra-template-typescript/template/src/sw-template.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ clientsClaim();
1313

1414
precacheAndRoute(self.__WB_MANIFEST);
1515

16-
const handler = createHandlerBoundToURL('/index.html');
16+
const handler = createHandlerBoundToURL(`${process.env.PUBLIC_URL}/index.html`);
1717

1818
const navigationRoute = new NavigationRoute(handler, {
1919
denylist: [new RegExp('^/_'), new RegExp('/[^/?]+\\.[^/]+$')],
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/* eslint-disable no-restricted-globals */
2+
import { clientsClaim } from 'workbox-core';
3+
import { NavigationRoute, registerRoute } from 'workbox-routing';
4+
import { precacheAndRoute, createHandlerBoundToURL } from 'workbox-precaching';
5+
6+
self.addEventListener('message', function(event) {
7+
if (event.data && event.data.type === 'SKIP_WAITING') {
8+
self.skipWaiting();
9+
}
10+
});
11+
12+
clientsClaim();
13+
14+
precacheAndRoute(self.__WB_MANIFEST);
15+
16+
const handler = createHandlerBoundToURL(`${process.env.PUBLIC_URL}/index.html`);
17+
18+
const navigationRoute = new NavigationRoute(handler, {
19+
denylist: [new RegExp('^/_'), new RegExp('/[^/?]+\\.[^/]+$')],
20+
});
21+
22+
registerRoute(navigationRoute);

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

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -670,33 +670,11 @@ module.exports = function(webpackEnv) {
670670
new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
671671
// Generate a service worker script that will precache, and keep up to date,
672672
// the HTML & assets that are part of the Webpack build.
673-
// isEnvProduction &&
674-
// new WorkboxWebpackPlugin.GenerateSW({
675-
// clientsClaim: true,
676-
// exclude: [/\.map$/, /asset-manifest\.json$/],
677-
// importWorkboxFrom: 'cdn',
678-
// navigateFallback: paths.publicUrlOrPath + 'index.html',
679-
// navigateFallbackBlacklist: [
680-
// // Exclude URLs starting with /_, as they're likely an API call
681-
// new RegExp('^/_'),
682-
// // Exclude any URLs whose last part seems to be a file extension
683-
// // as they're likely a resource and not a SPA route.
684-
// // URLs containing a "?" character won't be blacklisted as they're likely
685-
// // a route with query params (e.g. auth callbacks).
686-
// new RegExp('/[^/?]+\\.[^/]+$'),
687-
// ],
688-
// }),
689673
isEnvProduction &&
690674
new WorkboxWebpackPlugin.InjectManifest({
691675
exclude: [/\.map$/, /asset-manifest\.json$/],
692-
// importWorkboxFrom: 'cdn',
693-
// navigateFallback: paths.publicUrlOrPath + 'index.html',
694-
swSrc: `${paths.appSrc}/sw.js`,
676+
swSrc: `${paths.appSrc}/sw-template.js`,
695677
swDest: `${paths.appBuild}/service-worker.js`,
696-
// globDirectory: paths.appBuild,
697-
// importScripts: [
698-
// 'https://storage.googleapis.com/workbox-cdn/releases/4.3.1/workbox-sw.js',
699-
// ],
700678
}),
701679
// TypeScript type checking
702680
useTypeScript &&

0 commit comments

Comments
 (0)