UNPKG

1.51 kBJavaScriptView Raw
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*/
8import { removeIgnoredSearchParams } from './removeIgnoredSearchParams.js';
9import '../_version.js';
10/**
11 * Generator function that yields possible variations on the original URL to
12 * check, one at a time.
13 *
14 * @param {string} url
15 * @param {Object} options
16 *
17 * @private
18 * @memberof workbox-precaching
19 */
20export function* generateURLVariations(url, { ignoreURLParametersMatching = [/^utm_/, /^fbclid$/], directoryIndex = 'index.html', cleanURLs = true, urlManipulation, } = {}) {
21 const urlObject = new URL(url, location.href);
22 urlObject.hash = '';
23 yield urlObject.href;
24 const urlWithoutIgnoredParams = removeIgnoredSearchParams(urlObject, ignoreURLParametersMatching);
25 yield urlWithoutIgnoredParams.href;
26 if (directoryIndex && urlWithoutIgnoredParams.pathname.endsWith('/')) {
27 const directoryURL = new URL(urlWithoutIgnoredParams.href);
28 directoryURL.pathname += directoryIndex;
29 yield directoryURL.href;
30 }
31 if (cleanURLs) {
32 const cleanURL = new URL(urlWithoutIgnoredParams.href);
33 cleanURL.pathname += '.html';
34 yield cleanURL.href;
35 }
36 if (urlManipulation) {
37 const additionalURLs = urlManipulation({ url: urlObject });
38 for (const urlToAttempt of additionalURLs) {
39 yield urlToAttempt.href;
40 }
41 }
42}