| 1 | /*
|
|---|
| 2 | Copyright 2019 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 {removeIgnoredSearchParams} from './removeIgnoredSearchParams.js';
|
|---|
| 10 | import {PrecacheRouteOptions} from '../_types.js';
|
|---|
| 11 | import '../_version.js';
|
|---|
| 12 |
|
|---|
| 13 | /**
|
|---|
| 14 | * Generator function that yields possible variations on the original URL to
|
|---|
| 15 | * check, one at a time.
|
|---|
| 16 | *
|
|---|
| 17 | * @param {string} url
|
|---|
| 18 | * @param {Object} options
|
|---|
| 19 | *
|
|---|
| 20 | * @private
|
|---|
| 21 | * @memberof workbox-precaching
|
|---|
| 22 | */
|
|---|
| 23 | export function* generateURLVariations(
|
|---|
| 24 | url: string,
|
|---|
| 25 | {
|
|---|
| 26 | ignoreURLParametersMatching = [/^utm_/, /^fbclid$/],
|
|---|
| 27 | directoryIndex = 'index.html',
|
|---|
| 28 | cleanURLs = true,
|
|---|
| 29 | urlManipulation,
|
|---|
| 30 | }: PrecacheRouteOptions = {},
|
|---|
| 31 | ): Generator<string, void, unknown> {
|
|---|
| 32 | const urlObject = new URL(url, location.href);
|
|---|
| 33 | urlObject.hash = '';
|
|---|
| 34 | yield urlObject.href;
|
|---|
| 35 |
|
|---|
| 36 | const urlWithoutIgnoredParams = removeIgnoredSearchParams(
|
|---|
| 37 | urlObject,
|
|---|
| 38 | ignoreURLParametersMatching,
|
|---|
| 39 | );
|
|---|
| 40 | yield urlWithoutIgnoredParams.href;
|
|---|
| 41 |
|
|---|
| 42 | if (directoryIndex && urlWithoutIgnoredParams.pathname.endsWith('/')) {
|
|---|
| 43 | const directoryURL = new URL(urlWithoutIgnoredParams.href);
|
|---|
| 44 | directoryURL.pathname += directoryIndex;
|
|---|
| 45 | yield directoryURL.href;
|
|---|
| 46 | }
|
|---|
| 47 |
|
|---|
| 48 | if (cleanURLs) {
|
|---|
| 49 | const cleanURL = new URL(urlWithoutIgnoredParams.href);
|
|---|
| 50 | cleanURL.pathname += '.html';
|
|---|
| 51 | yield cleanURL.href;
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|
| 54 | if (urlManipulation) {
|
|---|
| 55 | const additionalURLs = urlManipulation({url: urlObject});
|
|---|
| 56 | for (const urlToAttempt of additionalURLs) {
|
|---|
| 57 | yield urlToAttempt.href;
|
|---|
| 58 | }
|
|---|
| 59 | }
|
|---|
| 60 | }
|
|---|