Skip to content

Commit 4bf1f63

Browse files
authored
Fix service workers not working after upgrading to rsbuild (#3143)
* add @aaroon/workbox-rspack-plugin to generate service worker for workbox * generate service worker file * fix undefined env var in service worker * deduplicate packages * update extension for service worker file (although does not seem to affect build)
1 parent ab860cb commit 4bf1f63

File tree

4 files changed

+1708
-1063
lines changed

4 files changed

+1708
-1063
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@
108108
"yareco": "^0.1.5"
109109
},
110110
"devDependencies": {
111+
"@aaroon/workbox-rspack-plugin": "^0.3.2",
111112
"@babel/core": "^7.24.5",
112113
"@babel/plugin-proposal-private-property-in-object": "^7.21.11",
113114
"@babel/preset-typescript": "^7.24.1",

rsbuild.config.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { pluginNodePolyfill } from '@rsbuild/plugin-node-polyfill';
44
import { pluginReact } from '@rsbuild/plugin-react';
55
import { pluginSass } from '@rsbuild/plugin-sass';
66
import { pluginSvgr } from '@rsbuild/plugin-svgr';
7+
import { InjectManifest } from '@aaroon/workbox-rspack-plugin';
78

89
const { publicVars, rawPublicVars } = loadEnv({ prefixes: ['REACT_APP_'] });
910

@@ -118,6 +119,15 @@ export default defineConfig({
118119
// })
119120
// ];
120121

122+
config.plugins = [
123+
...config.plugins,
124+
new InjectManifest({
125+
swSrc: './src/service-worker.ts',
126+
swDest: 'service-worker.js',
127+
maximumFileSizeToCacheInBytes: 20 * 1024 * 1024,
128+
})
129+
];
130+
121131
// Workaround to suppress warnings caused by ts-morph in js-slang
122132
// if (config.module) {
123133
// config.module.noParse = /node_modules\/@ts-morph\/common\/dist\/typescript\.js$/;

src/service-worker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ registerRoute(
5050
// Return true to signal that we want to use the handler.
5151
return true;
5252
},
53-
createHandlerBoundToURL(process.env.PUBLIC_URL + '/index.html')
53+
createHandlerBoundToURL((process.env.PUBLIC_URL || '') + '/index.html')
5454
);
5555

5656
// This allows the web app to trigger skipWaiting via

0 commit comments

Comments
 (0)