UNPKG

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