| 1 | /*
|
|---|
| 2 | Copyright 2020 Google LLC
|
|---|
| 3 |
|
|---|
| 4 | Use of this source code is governed by an MIT-style
|
|---|
| 5 | license that can be found in the LICENSE file or at
|
|---|
| 6 | https://opensource.org/licenses/MIT.
|
|---|
| 7 | */
|
|---|
| 8 |
|
|---|
| 9 | import {logger} from 'workbox-core/_private/logger.js';
|
|---|
| 10 | import {getFriendlyURL} from 'workbox-core/_private/getFriendlyURL.js';
|
|---|
| 11 | import {
|
|---|
| 12 | RouteMatchCallback,
|
|---|
| 13 | RouteMatchCallbackOptions,
|
|---|
| 14 | } from 'workbox-core/types.js';
|
|---|
| 15 | import {Route} from 'workbox-routing/Route.js';
|
|---|
| 16 |
|
|---|
| 17 | import {PrecacheRouteOptions} from './_types.js';
|
|---|
| 18 | import {PrecacheController} from './PrecacheController.js';
|
|---|
| 19 | import {generateURLVariations} from './utils/generateURLVariations.js';
|
|---|
| 20 |
|
|---|
| 21 | import './_version.js';
|
|---|
| 22 |
|
|---|
| 23 | /**
|
|---|
| 24 | * A subclass of {@link workbox-routing.Route} that takes a
|
|---|
| 25 | * {@link workbox-precaching.PrecacheController}
|
|---|
| 26 | * instance and uses it to match incoming requests and handle fetching
|
|---|
| 27 | * responses from the precache.
|
|---|
| 28 | *
|
|---|
| 29 | * @memberof workbox-precaching
|
|---|
| 30 | * @extends workbox-routing.Route
|
|---|
| 31 | */
|
|---|
| 32 | class PrecacheRoute extends Route {
|
|---|
| 33 | /**
|
|---|
| 34 | * @param {PrecacheController} precacheController A `PrecacheController`
|
|---|
| 35 | * instance used to both match requests and respond to fetch events.
|
|---|
| 36 | * @param {Object} [options] Options to control how requests are matched
|
|---|
| 37 | * against the list of precached URLs.
|
|---|
| 38 | * @param {string} [options.directoryIndex=index.html] The `directoryIndex` will
|
|---|
| 39 | * check cache entries for a URLs ending with '/' to see if there is a hit when
|
|---|
| 40 | * appending the `directoryIndex` value.
|
|---|
| 41 | * @param {Array<RegExp>} [options.ignoreURLParametersMatching=[/^utm_/, /^fbclid$/]] An
|
|---|
| 42 | * array of regex's to remove search params when looking for a cache match.
|
|---|
| 43 | * @param {boolean} [options.cleanURLs=true] The `cleanURLs` option will
|
|---|
| 44 | * check the cache for the URL with a `.html` added to the end of the end.
|
|---|
| 45 | * @param {workbox-precaching~urlManipulation} [options.urlManipulation]
|
|---|
| 46 | * This is a function that should take a URL and return an array of
|
|---|
| 47 | * alternative URLs that should be checked for precache matches.
|
|---|
| 48 | */
|
|---|
| 49 | constructor(
|
|---|
| 50 | precacheController: PrecacheController,
|
|---|
| 51 | options?: PrecacheRouteOptions,
|
|---|
| 52 | ) {
|
|---|
| 53 | const match: RouteMatchCallback = ({
|
|---|
| 54 | request,
|
|---|
| 55 | }: RouteMatchCallbackOptions) => {
|
|---|
| 56 | const urlsToCacheKeys = precacheController.getURLsToCacheKeys();
|
|---|
| 57 | for (const possibleURL of generateURLVariations(request.url, options)) {
|
|---|
| 58 | const cacheKey = urlsToCacheKeys.get(possibleURL);
|
|---|
| 59 | if (cacheKey) {
|
|---|
| 60 | const integrity =
|
|---|
| 61 | precacheController.getIntegrityForCacheKey(cacheKey);
|
|---|
| 62 | return {cacheKey, integrity};
|
|---|
| 63 | }
|
|---|
| 64 | }
|
|---|
| 65 | if (process.env.NODE_ENV !== 'production') {
|
|---|
| 66 | logger.debug(
|
|---|
| 67 | `Precaching did not find a match for ` + getFriendlyURL(request.url),
|
|---|
| 68 | );
|
|---|
| 69 | }
|
|---|
| 70 | return;
|
|---|
| 71 | };
|
|---|
| 72 |
|
|---|
| 73 | super(match, precacheController.strategy);
|
|---|
| 74 | }
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | export {PrecacheRoute};
|
|---|