1 | /*
|
2 | Copyright 2018 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 | import { WorkboxError } from 'workbox-core/_private/WorkboxError.js';
|
9 | import '../_version.js';
|
10 | // Name of the search parameter used to store revision info.
|
11 | const REVISION_SEARCH_PARAM = '__WB_REVISION__';
|
12 | /**
|
13 | * Converts a manifest entry into a versioned URL suitable for precaching.
|
14 | *
|
15 | * @param {Object|string} entry
|
16 | * @return {string} A URL with versioning info.
|
17 | *
|
18 | * @private
|
19 | * @memberof workbox-precaching
|
20 | */
|
21 | export function createCacheKey(entry) {
|
22 | if (!entry) {
|
23 | throw new WorkboxError('add-to-cache-list-unexpected-type', { entry });
|
24 | }
|
25 | // If a precache manifest entry is a string, it's assumed to be a versioned
|
26 | // URL, like '/app.abcd1234.js'. Return as-is.
|
27 | if (typeof entry === 'string') {
|
28 | const urlObject = new URL(entry, location.href);
|
29 | return {
|
30 | cacheKey: urlObject.href,
|
31 | url: urlObject.href,
|
32 | };
|
33 | }
|
34 | const { revision, url } = entry;
|
35 | if (!url) {
|
36 | throw new WorkboxError('add-to-cache-list-unexpected-type', { entry });
|
37 | }
|
38 | // If there's just a URL and no revision, then it's also assumed to be a
|
39 | // versioned URL.
|
40 | if (!revision) {
|
41 | const urlObject = new URL(url, location.href);
|
42 | return {
|
43 | cacheKey: urlObject.href,
|
44 | url: urlObject.href,
|
45 | };
|
46 | }
|
47 | // Otherwise, construct a properly versioned URL using the custom Workbox
|
48 | // search parameter along with the revision info.
|
49 | const cacheKeyURL = new URL(url, location.href);
|
50 | const originalURL = new URL(url, location.href);
|
51 | cacheKeyURL.searchParams.set(REVISION_SEARCH_PARAM, revision);
|
52 | return {
|
53 | cacheKey: cacheKeyURL.href,
|
54 | url: originalURL.href,
|
55 | };
|
56 | }
|