You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PrecacheEntry.mjs 2.4 KiB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. Copyright 2017 Google Inc.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. https://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. import '../_version.mjs';
  14. /**
  15. * Used as a consistent way of referencing a URL to precache.
  16. *
  17. * @private
  18. * @memberof module:workbox-precaching
  19. */
  20. export default class PrecacheEntry {
  21. /**
  22. * This class ensures all cache list entries are consistent and
  23. * adds cache busting if required.
  24. *
  25. * @param {*} originalInput
  26. * @param {string} url
  27. * @param {string} revision
  28. * @param {boolean} shouldCacheBust
  29. */
  30. constructor(originalInput, url, revision, shouldCacheBust) {
  31. this._originalInput = originalInput;
  32. this._entryId = url;
  33. this._revision = revision;
  34. const requestAsCacheKey = new Request(url, {credentials: 'same-origin'});
  35. this._cacheRequest = requestAsCacheKey;
  36. this._networkRequest = shouldCacheBust ?
  37. this._cacheBustRequest(requestAsCacheKey) : requestAsCacheKey;
  38. }
  39. /**
  40. * This method will either use Request.cache option OR append a cache
  41. * busting parameter to the URL.
  42. *
  43. * @param {Request} request The request to cache bust
  44. * @return {Request} A cachebusted Request
  45. *
  46. * @private
  47. */
  48. _cacheBustRequest(request) {
  49. let url = request.url;
  50. const requestOptions = {
  51. credentials: 'same-origin',
  52. };
  53. if ('cache' in Request.prototype) {
  54. // Make use of the Request cache mode where we can.
  55. // Reload skips the HTTP cache for outgoing requests and updates
  56. // the cache with the returned response.
  57. requestOptions.cache = 'reload';
  58. } else {
  59. const parsedURL = new URL(url, location);
  60. // This is done so the minifier can mangle 'global.encodeURIComponent'
  61. const _encodeURIComponent = encodeURIComponent;
  62. parsedURL.search += (parsedURL.search ? '&' : '') +
  63. _encodeURIComponent(`_workbox-cache-bust`) + '=' +
  64. _encodeURIComponent(this._revision);
  65. url = parsedURL.toString();
  66. }
  67. return new Request(url, requestOptions);
  68. }
  69. }