You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

pwa-worker.js 3.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. Copyright 2015, 2019, 2020 Google LLC. All Rights Reserved.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. const CACHE_NAME = 'offline';
  14. // Customize this with a different URL if needed.
  15. const OFFLINE_URL = 'static/offline.html';
  16. self.addEventListener('install', event => {
  17. event.waitUntil(
  18. (async () => {
  19. const cache = await caches.open(CACHE_NAME);
  20. // Setting {cache: 'reload'} in the new request will ensure that the
  21. // response isn't fulfilled from the HTTP cache; i.e., it will be from
  22. // the network.
  23. await cache.add(new Request(OFFLINE_URL, { cache: 'reload' }));
  24. })()
  25. );
  26. // Force the waiting service worker to become the active service worker.
  27. self.skipWaiting();
  28. });
  29. self.addEventListener('activate', event => {
  30. event.waitUntil(
  31. (async () => {
  32. // Enable navigation preload if it's supported.
  33. // See https://developers.google.com/web/updates/2017/02/navigation-preload
  34. if ('navigationPreload' in self.registration) {
  35. await self.registration.navigationPreload.enable();
  36. }
  37. })()
  38. );
  39. // Tell the active service worker to take control of the page immediately.
  40. self.clients.claim();
  41. });
  42. self.addEventListener('fetch', event => {
  43. // We only want to call event.respondWith() if this is a navigation request
  44. // for an HTML page.
  45. if (event.request.mode === 'navigate') {
  46. event.respondWith((async () => {
  47. try {
  48. // First, try to use the navigation preload response if it's supported.
  49. const preloadResponse = await event.preloadResponse;
  50. if (preloadResponse) {
  51. return preloadResponse;
  52. }
  53. // Always try the network first.
  54. const networkResponse = await fetch(event.request);
  55. return networkResponse;
  56. } catch (error) {
  57. // catch is only triggered if an exception is thrown, which is likely
  58. // due to a network error.
  59. // If fetch() returns a valid HTTP response with a response code in
  60. // the 4xx or 5xx range, the catch() will NOT be called.
  61. console.log('Fetch failed; returning offline page instead.', error);
  62. const cache = await caches.open(CACHE_NAME);
  63. const cachedResponse = await cache.match(OFFLINE_URL);
  64. return cachedResponse;
  65. }
  66. })());
  67. }
  68. // If our if() condition is false, then this fetch handler won't intercept the
  69. // request. If there are any other fetch handlers registered, they will get a
  70. // chance to call event.respondWith(). If no fetch handlers call
  71. // event.respondWith(), the request will be handled by the browser as if there
  72. // were no service worker involvement.
  73. });