text: /**
   * Copyright 2019 Google LLC
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *   https://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  /* global importScripts, languages  */
  import { CacheableResponsePlugin } from 'workbox-cacheable-response';
  import { CacheFirst, StaleWhileRevalidate } from 'workbox-strategies';
  import { ExpirationPlugin } from 'workbox-expiration';
  import { precacheAndRoute } from 'workbox-precaching';
  import { registerRoute, setCatchHandler } from 'workbox-routing';
  import { i18nHandler } from 'service-worker-i18n-redirect';
  import { preferences } from 'service-worker-i18n-redirect/preferences';
  import { serviceWorkerIncludePlugin } from 'service-worker-includes';
  
  importScripts('/js/languages.js');
  
  precacheAndRoute(self.__WB_MANIFEST);
  
  // Handle navigation requests with htmlHandler.
  const htmlCachingStrategy = new StaleWhileRevalidate({
    cacheName: 'pages-cache',
    plugins: [
      serviceWorkerIncludePlugin,
      new CacheableResponsePlugin({
        statuses: [200, 301, 404],
      }),
    ],
  });
  
  registerRoute(({ request }) => request.mode === 'navigate', i18nHandler(languages, preferences, htmlCachingStrategy));
  
  // Cache the Google Fonts stylesheets with a stale-while-revalidate strategy.
  registerRoute(
    ({ request }) => request.destination === 'style',
    new StaleWhileRevalidate({
      cacheName: 'stylesheets',
    }),
  );
  
  // Cache the underlying font files with a cache-first strategy for 1 year.
  registerRoute(
    ({ request }) => request.destination === 'font',
    new CacheFirst({
      cacheName: 'webfonts',
      plugins: [
        new CacheableResponsePlugin({
          statuses: [0, 200],
        }),
        new ExpirationPlugin({
          maxAgeSeconds: 60 * 60 * 24 * 365,
          maxEntries: 30,
        }),
      ],
    }),
  );
  
  // Images
  registerRoute(
    ({ request }) => request.destination === 'image',
    new CacheFirst({
      cacheName: 'images-cache',
      plugins: [
        new ExpirationPlugin({
          maxEntries: 50,
          maxAgeSeconds: 30 * 24 * 60 * 60, // 30 Days
        }),
      ],
    }),
  );
  
  setCatchHandler(({ event }) => {
    // The FALLBACK_URL entries must be added to the cache ahead of time, either via runtime
    // or precaching.
    // If they are precached, then call matchPrecache(FALLBACK_URL).
    //
    // Use event, request, and url to figure out how to respond.
    // One approach would be to use request.destination, see
    // https://medium.com/dev-channel/service-worker-caching-strategies-based-on-request-types-57411dd7652c
    switch (event.request.destination) {
      case 'document':
        return caches.match('/404/');
  
      // case 'image':
      //   return matchPrecache(FALLBACK_IMAGE_URL);
      // break;
  
      // case 'font':
      //   return matchPrecache(FALLBACK_FONT_URL);
      // break;
  
      default:
        // If we don't have a fallback, just return an error response.
        return Response.error();
    }
  });
  