noopServiceWorkerMiddleware.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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. 'use strict';
  8. const path = require('path');
  9. module.exports = function createNoopServiceWorkerMiddleware(servedPath) {
  10. return function noopServiceWorkerMiddleware(req, res, next) {
  11. if (req.url === path.join(servedPath, 'service-worker.js')) {
  12. res.setHeader('Content-Type', 'text/javascript');
  13. res.send(
  14. `// This service worker file is effectively a 'no-op' that will reset any
  15. // previous service worker registered for the same host:port combination.
  16. // In the production build, this file is replaced with an actual service worker
  17. // file that will precache your site's local assets.
  18. // See https://github.com/facebook/create-react-app/issues/2272#issuecomment-302832432
  19. self.addEventListener('install', () => self.skipWaiting());
  20. self.addEventListener('activate', () => {
  21. self.clients.matchAll({ type: 'window' }).then(windowClients => {
  22. for (let windowClient of windowClients) {
  23. // Force open pages to refresh, so that they have a chance to load the
  24. // fresh navigation response from the local dev server.
  25. windowClient.navigate(windowClient.url);
  26. }
  27. });
  28. });
  29. `
  30. );
  31. } else {
  32. next();
  33. }
  34. };
  35. };