Skip to content

Commit 7a15dd9

Browse files
committed
Add package name to env vars so that registerServiceWorker can know projects package name
1 parent f37690f commit 7a15dd9

File tree

4 files changed

+11
-5
lines changed

4 files changed

+11
-5
lines changed

packages/react-scripts/config/env.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ process.env.NODE_PATH = (process.env.NODE_PATH || '')
6767
// injected into the application via DefinePlugin in Webpack configuration.
6868
const REACT_APP = /^REACT_APP_/i;
6969

70-
function getClientEnvironment(publicUrl) {
70+
function getClientEnvironment(publicUrl, packageName) {
7171
const raw = Object.keys(process.env)
7272
.filter(key => REACT_APP.test(key))
7373
.reduce(
@@ -84,6 +84,9 @@ function getClientEnvironment(publicUrl) {
8484
// This should only be used as an escape hatch. Normally you would put
8585
// images into the `src` and `import` them in code to get their paths.
8686
PUBLIC_URL: publicUrl,
87+
// package.json name property.
88+
// Used by service worker to know the name of the generated service worker.
89+
PACKAGE_NAME: packageName,
8790
}
8891
);
8992
// Stringify all values so we can feed into Webpack DefinePlugin

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@ const publicPath = '/';
2929
// as %PUBLIC_URL% in `index.html` and `process.env.PUBLIC_URL` in JavaScript.
3030
// Omit trailing slash as %PUBLIC_PATH%/xyz looks better than %PUBLIC_PATH%xyz.
3131
const publicUrl = '';
32+
// Get packageName from package.json
33+
const packageName = require(paths.appPackageJson).name;
3234
// Get environment variables to inject into our app.
33-
const env = getClientEnvironment(publicUrl);
35+
const env = getClientEnvironment(publicUrl, packageName);
3436

3537
// This is the development configuration.
3638
// It is focused on developer experience and fast rebuilds.

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,12 @@ const shouldUseRelativeAssetPaths = publicPath === './';
3333
// as %PUBLIC_URL% in `index.html` and `process.env.PUBLIC_URL` in JavaScript.
3434
// Omit trailing slash as %PUBLIC_URL%/xyz looks better than %PUBLIC_URL%xyz.
3535
const publicUrl = publicPath.slice(0, -1);
36-
// Get environment variables to inject into our app.
37-
const env = getClientEnvironment(publicUrl);
3836
// Get packageName from package.json
3937
const packageName = require(paths.appPackageJson).name;
4038

39+
// Get environment variables to inject into our app.
40+
const env = getClientEnvironment(publicUrl, packageName);
41+
4142
// Assert this just to be safe.
4243
// Development builds of React are slow and not intended for production.
4344
if (env.stringified['process.env'].NODE_ENV !== '"production"') {

packages/react-scripts/template/src/registerServiceWorker.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export default function register() {
1212
if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) {
1313
window.addEventListener('load', () => {
1414
// this would become service-work-hash.js
15-
const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`;
15+
const swUrl = `${process.env.PUBLIC_URL}/service-worker-${process.env.PACKAGE_NAME}.js`;
1616
if (!navigator.serviceWorker.controller) {
1717
// No service worker yet
1818
registerServiceWorker(swUrl);

0 commit comments

Comments
 (0)