通用评论 vedio

registerServiceWorker.js 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = register;
  6. exports.unregister = unregister;
  7. // In production, we register a service worker to serve assets from local cache.
  8. // This lets the app load faster on subsequent visits in production, and gives
  9. // it offline capabilities. However, it also means that developers (and users)
  10. // will only see deployed updates on the "N+1" visit to a page, since previously
  11. // cached resources are updated in the background.
  12. // To learn more about the benefits of this model, read https://goo.gl/KwvDNy.
  13. // This link also includes instructions on opting out of this behavior.
  14. var isLocalhost = Boolean(window.location.hostname === "localhost" ||
  15. // [::1] is the IPv6 localhost address.
  16. window.location.hostname === "[::1]" ||
  17. // 127.0.0.1/8 is considered localhost for IPv4.
  18. window.location.hostname.match(/^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/));
  19. function register() {
  20. if (process.env.NODE_ENV === "production" && "serviceWorker" in navigator) {
  21. // The URL constructor is available in all browsers that support SW.
  22. var publicUrl = new URL(process.env.PUBLIC_URL, window.location);
  23. if (publicUrl.origin !== window.location.origin) {
  24. // Our service worker won't work if PUBLIC_URL is on a different origin
  25. // from what our page is served on. This might happen if a CDN is used to
  26. // serve assets; see https://github.com/facebookincubator/create-react-app/issues/2374
  27. return;
  28. }
  29. window.addEventListener("load", function () {
  30. var swUrl = process.env.PUBLIC_URL + "/service-worker.js";
  31. if (isLocalhost) {
  32. // This is running on localhost. Lets check if a service worker still exists or not.
  33. checkValidServiceWorker(swUrl);
  34. // Add some additional logging to localhost, pointing developers to the
  35. // service worker/PWA documentation.
  36. navigator.serviceWorker.ready.then(function () {
  37. console.log("This web app is being served cache-first by a service " + "worker. To learn more, visit https://goo.gl/SC7cgQ");
  38. });
  39. } else {
  40. // Is not local host. Just register service worker
  41. registerValidSW(swUrl);
  42. }
  43. });
  44. }
  45. }
  46. function registerValidSW(swUrl) {
  47. navigator.serviceWorker.register(swUrl).then(function (registration) {
  48. registration.onupdatefound = function () {
  49. var installingWorker = registration.installing;
  50. installingWorker.onstatechange = function () {
  51. if (installingWorker.state === "installed") {
  52. if (navigator.serviceWorker.controller) {
  53. // At this point, the old content will have been purged and
  54. // the fresh content will have been added to the cache.
  55. // It's the perfect time to display a "New content is
  56. // available; please refresh." message in your web app.
  57. console.log("New content is available; please refresh.");
  58. } else {
  59. // At this point, everything has been precached.
  60. // It's the perfect time to display a
  61. // "Content is cached for offline use." message.
  62. console.log("Content is cached for offline use.");
  63. }
  64. }
  65. };
  66. };
  67. }).catch(function (error) {
  68. console.error("Error during service worker registration:", error);
  69. });
  70. }
  71. function checkValidServiceWorker(swUrl) {
  72. // Check if the service worker can be found. If it can't reload the page.
  73. fetch(swUrl).then(function (response) {
  74. // Ensure service worker exists, and that we really are getting a JS file.
  75. if (response.status === 404 || response.headers.get("content-type").indexOf("javascript") === -1) {
  76. // No service worker found. Probably a different app. Reload the page.
  77. navigator.serviceWorker.ready.then(function (registration) {
  78. registration.unregister().then(function () {
  79. window.location.reload();
  80. });
  81. });
  82. } else {
  83. // Service worker found. Proceed as normal.
  84. registerValidSW(swUrl);
  85. }
  86. }).catch(function () {
  87. console.log("No internet connection found. App is running in offline mode.");
  88. });
  89. }
  90. function unregister() {
  91. if ("serviceWorker" in navigator) {
  92. navigator.serviceWorker.ready.then(function (registration) {
  93. registration.unregister();
  94. });
  95. }
  96. }
  97. //# sourceMappingURL=registerServiceWorker.js.map