Skip to content

Commit d64eba8

Browse files
jeffposnickromaindso
authored andcommitted
Provide a no-op SW that will be served by the dev environment. (facebook#2276)
* Provide a no-op SW that will be served by the dev environment. * Hide no-op service worker from user
1 parent 5397939 commit d64eba8

File tree

4 files changed

+52
-0
lines changed

4 files changed

+52
-0
lines changed

packages/react-dev-utils/README.md

+4
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,10 @@ getProcessForPort(3000);
240240

241241
On macOS, tries to find a known running editor process and opens the file in it. It can also be explicitly configured by `REACT_EDITOR`, `VISUAL`, or `EDITOR` environment variables. For example, you can put `REACT_EDITOR=atom` in your `.env.local` file, and Create React App will respect that.
242242

243+
#### `noopServiceWorkerMiddleware(): ExpressMiddleware`
244+
245+
Returns Express middleware that serves a `/service-worker.js` that resets any previously set service worker configuration. Useful for development.
246+
243247
#### `openBrowser(url: string): boolean`
244248

245249
Attempts to open the browser with a given URL.<br>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Copyright (c) 2015-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
9+
10+
'use strict';
11+
12+
module.exports = function createNoopServiceWorkerMiddleware() {
13+
return function noopServiceWorkerMiddleware(req, res, next) {
14+
if (req.url === '/service-worker.js') {
15+
res.setHeader('Content-Type', 'text/javascript');
16+
res.send(
17+
`// This service worker file is effectively a 'no-op' that will reset any
18+
// previous service worker registered for the same host:port combination.
19+
// In the production build, this file is replaced with an actual service worker
20+
// file that will precache your site's local assets.
21+
// See https://github.com/facebookincubator/create-react-app/issues/2272#issuecomment-302832432
22+
23+
self.addEventListener('install', () => self.skipWaiting());
24+
25+
self.addEventListener('activate', () => {
26+
self.clients.matchAll({ type: 'window' }).then(windowClients => {
27+
for (let windowClient of windowClients) {
28+
// Force open pages to refresh, so that they have a chance to load the
29+
// fresh navigation response from the local dev server.
30+
windowClient.navigate(windowClient.url);
31+
}
32+
});
33+
});
34+
`
35+
);
36+
} else {
37+
next();
38+
}
39+
};
40+
};

packages/react-dev-utils/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"getProcessForPort.js",
2222
"InterpolateHtmlPlugin.js",
2323
"launchEditor.js",
24+
"noopServiceWorkerMiddleware.js",
2425
"ModuleScopePlugin.js",
2526
"openBrowser.js",
2627
"openChrome.applescript",

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

+7
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
'use strict';
1212

1313
const errorOverlayMiddleware = require('react-error-overlay/middleware');
14+
const noopServiceWorkerMiddleware = require('react-dev-utils/noopServiceWorkerMiddleware');
1415
const config = require('./webpack.config.dev');
1516
const paths = require('./paths');
1617

@@ -90,6 +91,12 @@ module.exports = function(proxy, allowedHost) {
9091
setup(app) {
9192
// This lets us open files from the runtime error overlay.
9293
app.use(errorOverlayMiddleware());
94+
// This service worker file is effectively a 'no-op' that will reset any
95+
// previous service worker registered for the same host:port combination.
96+
// We do this in development to avoid hitting the production cache if
97+
// it used the same host and port.
98+
// https://github.com/facebookincubator/create-react-app/issues/2272#issuecomment-302832432
99+
app.use(noopServiceWorkerMiddleware());
93100
},
94101
};
95102
};

0 commit comments

Comments
 (0)