PrecacheInstallReportPlugin.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. /*
  2. Copyright 2020 Google LLC
  3. Use of this source code is governed by an MIT-style
  4. license that can be found in the LICENSE file or at
  5. https://opensource.org/licenses/MIT.
  6. */
  7. import '../_version.js';
  8. /**
  9. * A plugin, designed to be used with PrecacheController, to determine the
  10. * of assets that were updated (or not updated) during the install event.
  11. *
  12. * @private
  13. */
  14. class PrecacheInstallReportPlugin {
  15. constructor() {
  16. this.updatedURLs = [];
  17. this.notUpdatedURLs = [];
  18. this.handlerWillStart = async ({ request, state, }) => {
  19. // TODO: `state` should never be undefined...
  20. if (state) {
  21. state.originalRequest = request;
  22. }
  23. };
  24. this.cachedResponseWillBeUsed = async ({ event, state, cachedResponse, }) => {
  25. if (event.type === 'install') {
  26. if (state &&
  27. state.originalRequest &&
  28. state.originalRequest instanceof Request) {
  29. // TODO: `state` should never be undefined...
  30. const url = state.originalRequest.url;
  31. if (cachedResponse) {
  32. this.notUpdatedURLs.push(url);
  33. }
  34. else {
  35. this.updatedURLs.push(url);
  36. }
  37. }
  38. }
  39. return cachedResponse;
  40. };
  41. }
  42. }
  43. export { PrecacheInstallReportPlugin };