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