UNPKG

1.35 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
10const path = require('path');
11
12module.exports = function createNoopServiceWorkerMiddleware(servedPath) {
13 return function noopServiceWorkerMiddleware(req, res, next) {
14 if (req.url === path.join(servedPath, '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/facebook/create-react-app/issues/2272#issuecomment-302832432
22
23self.addEventListener('install', () => self.skipWaiting());
24
25self.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};