| 1 | /*
|
|---|
| 2 | Copyright 2020 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 {WorkboxPlugin, WorkboxPluginCallbackParam} from 'workbox-core/types.js';
|
|---|
| 10 |
|
|---|
| 11 | import '../_version.js';
|
|---|
| 12 |
|
|---|
| 13 | /**
|
|---|
| 14 | * A plugin, designed to be used with PrecacheController, to determine the
|
|---|
| 15 | * of assets that were updated (or not updated) during the install event.
|
|---|
| 16 | *
|
|---|
| 17 | * @private
|
|---|
| 18 | */
|
|---|
| 19 | class PrecacheInstallReportPlugin implements WorkboxPlugin {
|
|---|
| 20 | updatedURLs: string[] = [];
|
|---|
| 21 | notUpdatedURLs: string[] = [];
|
|---|
| 22 |
|
|---|
| 23 | handlerWillStart: WorkboxPlugin['handlerWillStart'] = async ({
|
|---|
| 24 | request,
|
|---|
| 25 | state,
|
|---|
| 26 | }: WorkboxPluginCallbackParam['handlerWillStart']) => {
|
|---|
| 27 | // TODO: `state` should never be undefined...
|
|---|
| 28 | if (state) {
|
|---|
| 29 | state.originalRequest = request;
|
|---|
| 30 | }
|
|---|
| 31 | };
|
|---|
| 32 |
|
|---|
| 33 | cachedResponseWillBeUsed: WorkboxPlugin['cachedResponseWillBeUsed'] = async ({
|
|---|
| 34 | event,
|
|---|
| 35 | state,
|
|---|
| 36 | cachedResponse,
|
|---|
| 37 | }: WorkboxPluginCallbackParam['cachedResponseWillBeUsed']) => {
|
|---|
| 38 | if (event.type === 'install') {
|
|---|
| 39 | if (
|
|---|
| 40 | state &&
|
|---|
| 41 | state.originalRequest &&
|
|---|
| 42 | state.originalRequest instanceof Request
|
|---|
| 43 | ) {
|
|---|
| 44 | // TODO: `state` should never be undefined...
|
|---|
| 45 | const url = state.originalRequest.url;
|
|---|
| 46 |
|
|---|
| 47 | if (cachedResponse) {
|
|---|
| 48 | this.notUpdatedURLs.push(url);
|
|---|
| 49 | } else {
|
|---|
| 50 | this.updatedURLs.push(url);
|
|---|
| 51 | }
|
|---|
| 52 | }
|
|---|
| 53 | }
|
|---|
| 54 | return cachedResponse;
|
|---|
| 55 | };
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | export {PrecacheInstallReportPlugin};
|
|---|