@serwist/cacheable-response 8.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +10 -0
- package/.turbo/turbo-lint.log +4 -0
- package/.turbo/turbo-typecheck.log +4 -0
- package/CHANGELOG.md +33 -0
- package/LICENSE +21 -0
- package/README.md +1 -0
- package/dist/CacheableResponse.d.ts +45 -0
- package/dist/CacheableResponse.d.ts.map +1 -0
- package/dist/CacheableResponsePlugin.d.ts +28 -0
- package/dist/CacheableResponsePlugin.d.ts.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +129 -0
- package/dist/index.old.cjs +132 -0
- package/package.json +41 -0
- package/rollup.config.js +27 -0
- package/src/CacheableResponse.ts +140 -0
- package/src/CacheableResponsePlugin.ts +48 -0
- package/src/index.ts +15 -0
- package/tsconfig.json +7 -0
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
|
|
2
|
+
> @serwist/cacheable-response@8.0.0 build /home/runner/work/serwist/serwist/packages/cacheable-response
|
|
3
|
+
> cross-env NODE_ENV=production rollup --waitForBundleInput --config rollup.config.js
|
|
4
|
+
|
|
5
|
+
(node:2290) ExperimentalWarning: Import assertions are not a stable feature of the JavaScript language. Avoid relying on their current behavior and syntax as those might change in a future version of Node.js.
|
|
6
|
+
(Use `node --trace-warnings ...` to show where the warning was created)
|
|
7
|
+
(node:2290) ExperimentalWarning: Importing JSON modules is an experimental feature and might change at any time
|
|
8
|
+
[36m
|
|
9
|
+
[1msrc/index.ts[22m → [1mdist/index.old.cjs, dist/index.js[22m...[39m
|
|
10
|
+
[32mcreated [1mdist/index.old.cjs, dist/index.js[22m in [1m7.6s[22m[39m
|
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# @serwist/cacheable-response
|
|
2
|
+
|
|
3
|
+
## 8.0.0
|
|
4
|
+
|
|
5
|
+
### Major Changes
|
|
6
|
+
|
|
7
|
+
- [`e0313f0`](https://github.com/serwist/serwist/commit/e0313f02f661a07ccbe9edc64e44e1af6136c73e) Thanks [@DuCanhGH](https://github.com/DuCanhGH)! - chore: initial release
|
|
8
|
+
|
|
9
|
+
- Reimagined `@serwist/next`.
|
|
10
|
+
- Removed various options.
|
|
11
|
+
- Removed `aggressiveFrontEndNavCaching` to reassess its usefulness.
|
|
12
|
+
- Removed `browserslist`. Use `swcEnvTargets` instead (TODO: add this option).
|
|
13
|
+
- Custom workers have been removed. You should use `swSrc`.
|
|
14
|
+
- Removed `extendDefaultRuntimeCaching`. Use the spread syntax instead (use `import { defaultCache } from "@serwist/next/browser"` to import the default runtimeCaching array).
|
|
15
|
+
- Temporarily removed `fallbacks` to investigate module-friendly alternatives.
|
|
16
|
+
- Removed `swcMinify`.
|
|
17
|
+
- Removed `watchWorkersInDev`.
|
|
18
|
+
- Removed `cacheStartUrl`, `dynamicStartUrl`, and `dynamicStartUrl`. These shall be re-added only when their use cases are made more clear to me.
|
|
19
|
+
- Merged `workboxOptions` with the plugin's options.
|
|
20
|
+
- Removed `swc-loader`, `terser-minify`, `webpack-builders`,... (we now leverage `ChildCompilationPlugin` to compile workers - this change will be backported to `@ducanh2912/next-pwa@10`)
|
|
21
|
+
- Removed the ability to use GenerateSW. `@serwist/sw.installSerwist` is provided as a replacement.
|
|
22
|
+
- `swSrc` is now a required property.
|
|
23
|
+
- Moved minimum support Next.js version from `11.0.0` to `14.0.0`.
|
|
24
|
+
- Removed GenerateSW (replaced by `@serwist/sw.installSerwist`).
|
|
25
|
+
- See `examples/next-basic/app/sw.ts` to see how `installSerwist` should be used.
|
|
26
|
+
- Repurposed `@serwist/sw`.
|
|
27
|
+
- The old package might be reintroduced if there's demand.
|
|
28
|
+
- **Note:** This is just the initial release, and there is still a lot of ground to cover (a lot of legacy code to be removed, a lot of features to be reintroduced,...). Here's to a bright future for the project :\_)
|
|
29
|
+
|
|
30
|
+
### Patch Changes
|
|
31
|
+
|
|
32
|
+
- Updated dependencies [[`e0313f0`](https://github.com/serwist/serwist/commit/e0313f02f661a07ccbe9edc64e44e1af6136c73e)]:
|
|
33
|
+
- @serwist/core@8.0.0
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2018 Google LLC, 2019 ShadowWalker w@weiw.io https://weiw.io, 2023 Serwist
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
This module's documentation can be found at https://developers.google.com/web/tools/workbox/modules/workbox-cacheable-response
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
export interface CacheableResponseOptions {
|
|
2
|
+
/**
|
|
3
|
+
* One or more status codes that a `Response` can have to be considered cacheable.
|
|
4
|
+
*/
|
|
5
|
+
statuses?: number[];
|
|
6
|
+
/**
|
|
7
|
+
* A mapping of header names and expected values that a `Response` can have and be
|
|
8
|
+
* considered cacheable. If multiple headers are provided, only one needs to be present.
|
|
9
|
+
*/
|
|
10
|
+
headers?: {
|
|
11
|
+
[headerName: string]: string;
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* This class allows you to set up rules determining what
|
|
16
|
+
* status codes and/or headers need to be present in order for a
|
|
17
|
+
* [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response)
|
|
18
|
+
* to be considered cacheable.
|
|
19
|
+
*/
|
|
20
|
+
declare class CacheableResponse {
|
|
21
|
+
private readonly _statuses?;
|
|
22
|
+
private readonly _headers?;
|
|
23
|
+
/**
|
|
24
|
+
* To construct a new CacheableResponse instance you must provide at least
|
|
25
|
+
* one of the `config` properties.
|
|
26
|
+
*
|
|
27
|
+
* If both `statuses` and `headers` are specified, then both conditions must
|
|
28
|
+
* be met for the `Response` to be considered cacheable.
|
|
29
|
+
*
|
|
30
|
+
* @param config
|
|
31
|
+
*/
|
|
32
|
+
constructor(config?: CacheableResponseOptions);
|
|
33
|
+
/**
|
|
34
|
+
* Checks a response to see whether it's cacheable or not, based on this
|
|
35
|
+
* object's configuration.
|
|
36
|
+
*
|
|
37
|
+
* @param response The response whose cacheability is being
|
|
38
|
+
* checked.
|
|
39
|
+
* @returns `true` if the `Response` is cacheable, and `false`
|
|
40
|
+
* otherwise.
|
|
41
|
+
*/
|
|
42
|
+
isResponseCacheable(response: Response): boolean;
|
|
43
|
+
}
|
|
44
|
+
export { CacheableResponse };
|
|
45
|
+
//# sourceMappingURL=CacheableResponse.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"CacheableResponse.d.ts","sourceRoot":"","sources":["../src/CacheableResponse.ts"],"names":[],"mappings":"AAUA,MAAM,WAAW,wBAAwB;IACvC;;OAEG;IACH,QAAQ,CAAC,EAAE,MAAM,EAAE,CAAC;IACpB;;;OAGG;IACH,OAAO,CAAC,EAAE;QAAE,CAAC,UAAU,EAAE,MAAM,GAAG,MAAM,CAAA;KAAE,CAAC;CAC5C;AAED;;;;;GAKG;AACH,cAAM,iBAAiB;IACrB,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAC,CAAuC;IAClE,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAsC;IAEhE;;;;;;;;OAQG;gBACS,MAAM,GAAE,wBAA6B;IAiCjD;;;;;;;;OAQG;IACH,mBAAmB,CAAC,QAAQ,EAAE,QAAQ,GAAG,OAAO;CAsDjD;AAED,OAAO,EAAE,iBAAiB,EAAE,CAAC"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { SerwistPlugin } from "@serwist/core";
|
|
2
|
+
import type { CacheableResponseOptions } from "./CacheableResponse.js";
|
|
3
|
+
/**
|
|
4
|
+
* A class implementing the `cacheWillUpdate` lifecycle callback. This makes it
|
|
5
|
+
* easier to add in cacheability checks to requests made via Serwist's built-in
|
|
6
|
+
* strategies.
|
|
7
|
+
*/
|
|
8
|
+
declare class CacheableResponsePlugin implements SerwistPlugin {
|
|
9
|
+
private readonly _cacheableResponse;
|
|
10
|
+
/**
|
|
11
|
+
* To construct a new CacheableResponsePlugin instance you must provide at
|
|
12
|
+
* least one of the `config` properties.
|
|
13
|
+
*
|
|
14
|
+
* If both `statuses` and `headers` are specified, then both conditions must
|
|
15
|
+
* be met for the `Response` to be considered cacheable.
|
|
16
|
+
*
|
|
17
|
+
* @param config
|
|
18
|
+
*/
|
|
19
|
+
constructor(config: CacheableResponseOptions);
|
|
20
|
+
/**
|
|
21
|
+
* @param options
|
|
22
|
+
* @returns
|
|
23
|
+
* @private
|
|
24
|
+
*/
|
|
25
|
+
cacheWillUpdate: SerwistPlugin["cacheWillUpdate"];
|
|
26
|
+
}
|
|
27
|
+
export { CacheableResponsePlugin };
|
|
28
|
+
//# sourceMappingURL=CacheableResponsePlugin.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"CacheableResponsePlugin.d.ts","sourceRoot":"","sources":["../src/CacheableResponsePlugin.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAEnD,OAAO,KAAK,EAAE,wBAAwB,EAAE,MAAM,wBAAwB,CAAC;AAGvE;;;;GAIG;AACH,cAAM,uBAAwB,YAAW,aAAa;IACpD,OAAO,CAAC,QAAQ,CAAC,kBAAkB,CAAoB;IAEvD;;;;;;;;OAQG;gBACS,MAAM,EAAE,wBAAwB;IAI5C;;;;OAIG;IACH,eAAe,EAAE,aAAa,CAAC,iBAAiB,CAAC,CAK/C;CACH;AAED,OAAO,EAAE,uBAAuB,EAAE,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { CacheableResponseOptions } from "./CacheableResponse.js";
|
|
2
|
+
import { CacheableResponse } from "./CacheableResponse.js";
|
|
3
|
+
import { CacheableResponsePlugin } from "./CacheableResponsePlugin.js";
|
|
4
|
+
export { CacheableResponse, CacheableResponsePlugin };
|
|
5
|
+
export type { CacheableResponseOptions };
|
|
6
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,wBAAwB,EAAE,MAAM,wBAAwB,CAAC;AACvE,OAAO,EAAE,iBAAiB,EAAE,MAAM,wBAAwB,CAAC;AAC3D,OAAO,EAAE,uBAAuB,EAAE,MAAM,8BAA8B,CAAC;AAEvE,OAAO,EAAE,iBAAiB,EAAE,uBAAuB,EAAE,CAAC;AAEtD,YAAY,EAAE,wBAAwB,EAAE,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import { SerwistError, assert, logger, getFriendlyURL } from '@serwist/core/internal';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* This class allows you to set up rules determining what
|
|
5
|
+
* status codes and/or headers need to be present in order for a
|
|
6
|
+
* [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response)
|
|
7
|
+
* to be considered cacheable.
|
|
8
|
+
*/ class CacheableResponse {
|
|
9
|
+
_statuses;
|
|
10
|
+
_headers;
|
|
11
|
+
/**
|
|
12
|
+
* To construct a new CacheableResponse instance you must provide at least
|
|
13
|
+
* one of the `config` properties.
|
|
14
|
+
*
|
|
15
|
+
* If both `statuses` and `headers` are specified, then both conditions must
|
|
16
|
+
* be met for the `Response` to be considered cacheable.
|
|
17
|
+
*
|
|
18
|
+
* @param config
|
|
19
|
+
*/ constructor(config = {}){
|
|
20
|
+
if (process.env.NODE_ENV !== "production") {
|
|
21
|
+
if (!(config.statuses || config.headers)) {
|
|
22
|
+
throw new SerwistError("statuses-or-headers-required", {
|
|
23
|
+
moduleName: "@serwist/cacheable-response",
|
|
24
|
+
className: "CacheableResponse",
|
|
25
|
+
funcName: "constructor"
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
if (config.statuses) {
|
|
29
|
+
assert.isArray(config.statuses, {
|
|
30
|
+
moduleName: "@serwist/cacheable-response",
|
|
31
|
+
className: "CacheableResponse",
|
|
32
|
+
funcName: "constructor",
|
|
33
|
+
paramName: "config.statuses"
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
if (config.headers) {
|
|
37
|
+
assert.isType(config.headers, "object", {
|
|
38
|
+
moduleName: "@serwist/cacheable-response",
|
|
39
|
+
className: "CacheableResponse",
|
|
40
|
+
funcName: "constructor",
|
|
41
|
+
paramName: "config.headers"
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
this._statuses = config.statuses;
|
|
46
|
+
this._headers = config.headers;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Checks a response to see whether it's cacheable or not, based on this
|
|
50
|
+
* object's configuration.
|
|
51
|
+
*
|
|
52
|
+
* @param response The response whose cacheability is being
|
|
53
|
+
* checked.
|
|
54
|
+
* @returns `true` if the `Response` is cacheable, and `false`
|
|
55
|
+
* otherwise.
|
|
56
|
+
*/ isResponseCacheable(response) {
|
|
57
|
+
if (process.env.NODE_ENV !== "production") {
|
|
58
|
+
assert.isInstance(response, Response, {
|
|
59
|
+
moduleName: "@serwist/cacheable-response",
|
|
60
|
+
className: "CacheableResponse",
|
|
61
|
+
funcName: "isResponseCacheable",
|
|
62
|
+
paramName: "response"
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
let cacheable = true;
|
|
66
|
+
if (this._statuses) {
|
|
67
|
+
cacheable = this._statuses.includes(response.status);
|
|
68
|
+
}
|
|
69
|
+
if (this._headers && cacheable) {
|
|
70
|
+
cacheable = Object.keys(this._headers).some((headerName)=>{
|
|
71
|
+
return response.headers.get(headerName) === this._headers[headerName];
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
if (process.env.NODE_ENV !== "production") {
|
|
75
|
+
if (!cacheable) {
|
|
76
|
+
logger.groupCollapsed(`The request for ` + `'${getFriendlyURL(response.url)}' returned a response that does ` + `not meet the criteria for being cached.`);
|
|
77
|
+
logger.groupCollapsed(`View cacheability criteria here.`);
|
|
78
|
+
logger.log(`Cacheable statuses: ` + JSON.stringify(this._statuses));
|
|
79
|
+
logger.log(`Cacheable headers: ` + JSON.stringify(this._headers, null, 2));
|
|
80
|
+
logger.groupEnd();
|
|
81
|
+
const logFriendlyHeaders = {};
|
|
82
|
+
response.headers.forEach((value, key)=>{
|
|
83
|
+
logFriendlyHeaders[key] = value;
|
|
84
|
+
});
|
|
85
|
+
logger.groupCollapsed(`View response status and headers here.`);
|
|
86
|
+
logger.log(`Response status: ${response.status}`);
|
|
87
|
+
logger.log(`Response headers: ` + JSON.stringify(logFriendlyHeaders, null, 2));
|
|
88
|
+
logger.groupEnd();
|
|
89
|
+
logger.groupCollapsed(`View full response details here.`);
|
|
90
|
+
logger.log(response.headers);
|
|
91
|
+
logger.log(response);
|
|
92
|
+
logger.groupEnd();
|
|
93
|
+
logger.groupEnd();
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
return cacheable;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* A class implementing the `cacheWillUpdate` lifecycle callback. This makes it
|
|
102
|
+
* easier to add in cacheability checks to requests made via Serwist's built-in
|
|
103
|
+
* strategies.
|
|
104
|
+
*/ class CacheableResponsePlugin {
|
|
105
|
+
_cacheableResponse;
|
|
106
|
+
/**
|
|
107
|
+
* To construct a new CacheableResponsePlugin instance you must provide at
|
|
108
|
+
* least one of the `config` properties.
|
|
109
|
+
*
|
|
110
|
+
* If both `statuses` and `headers` are specified, then both conditions must
|
|
111
|
+
* be met for the `Response` to be considered cacheable.
|
|
112
|
+
*
|
|
113
|
+
* @param config
|
|
114
|
+
*/ constructor(config){
|
|
115
|
+
this._cacheableResponse = new CacheableResponse(config);
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* @param options
|
|
119
|
+
* @returns
|
|
120
|
+
* @private
|
|
121
|
+
*/ cacheWillUpdate = async ({ response })=>{
|
|
122
|
+
if (this._cacheableResponse.isResponseCacheable(response)) {
|
|
123
|
+
return response;
|
|
124
|
+
}
|
|
125
|
+
return null;
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
export { CacheableResponse, CacheableResponsePlugin };
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var internal = require('@serwist/core/internal');
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* This class allows you to set up rules determining what
|
|
7
|
+
* status codes and/or headers need to be present in order for a
|
|
8
|
+
* [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response)
|
|
9
|
+
* to be considered cacheable.
|
|
10
|
+
*/ class CacheableResponse {
|
|
11
|
+
_statuses;
|
|
12
|
+
_headers;
|
|
13
|
+
/**
|
|
14
|
+
* To construct a new CacheableResponse instance you must provide at least
|
|
15
|
+
* one of the `config` properties.
|
|
16
|
+
*
|
|
17
|
+
* If both `statuses` and `headers` are specified, then both conditions must
|
|
18
|
+
* be met for the `Response` to be considered cacheable.
|
|
19
|
+
*
|
|
20
|
+
* @param config
|
|
21
|
+
*/ constructor(config = {}){
|
|
22
|
+
if (process.env.NODE_ENV !== "production") {
|
|
23
|
+
if (!(config.statuses || config.headers)) {
|
|
24
|
+
throw new internal.SerwistError("statuses-or-headers-required", {
|
|
25
|
+
moduleName: "@serwist/cacheable-response",
|
|
26
|
+
className: "CacheableResponse",
|
|
27
|
+
funcName: "constructor"
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
if (config.statuses) {
|
|
31
|
+
internal.assert.isArray(config.statuses, {
|
|
32
|
+
moduleName: "@serwist/cacheable-response",
|
|
33
|
+
className: "CacheableResponse",
|
|
34
|
+
funcName: "constructor",
|
|
35
|
+
paramName: "config.statuses"
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
if (config.headers) {
|
|
39
|
+
internal.assert.isType(config.headers, "object", {
|
|
40
|
+
moduleName: "@serwist/cacheable-response",
|
|
41
|
+
className: "CacheableResponse",
|
|
42
|
+
funcName: "constructor",
|
|
43
|
+
paramName: "config.headers"
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
this._statuses = config.statuses;
|
|
48
|
+
this._headers = config.headers;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Checks a response to see whether it's cacheable or not, based on this
|
|
52
|
+
* object's configuration.
|
|
53
|
+
*
|
|
54
|
+
* @param response The response whose cacheability is being
|
|
55
|
+
* checked.
|
|
56
|
+
* @returns `true` if the `Response` is cacheable, and `false`
|
|
57
|
+
* otherwise.
|
|
58
|
+
*/ isResponseCacheable(response) {
|
|
59
|
+
if (process.env.NODE_ENV !== "production") {
|
|
60
|
+
internal.assert.isInstance(response, Response, {
|
|
61
|
+
moduleName: "@serwist/cacheable-response",
|
|
62
|
+
className: "CacheableResponse",
|
|
63
|
+
funcName: "isResponseCacheable",
|
|
64
|
+
paramName: "response"
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
let cacheable = true;
|
|
68
|
+
if (this._statuses) {
|
|
69
|
+
cacheable = this._statuses.includes(response.status);
|
|
70
|
+
}
|
|
71
|
+
if (this._headers && cacheable) {
|
|
72
|
+
cacheable = Object.keys(this._headers).some((headerName)=>{
|
|
73
|
+
return response.headers.get(headerName) === this._headers[headerName];
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
if (process.env.NODE_ENV !== "production") {
|
|
77
|
+
if (!cacheable) {
|
|
78
|
+
internal.logger.groupCollapsed(`The request for ` + `'${internal.getFriendlyURL(response.url)}' returned a response that does ` + `not meet the criteria for being cached.`);
|
|
79
|
+
internal.logger.groupCollapsed(`View cacheability criteria here.`);
|
|
80
|
+
internal.logger.log(`Cacheable statuses: ` + JSON.stringify(this._statuses));
|
|
81
|
+
internal.logger.log(`Cacheable headers: ` + JSON.stringify(this._headers, null, 2));
|
|
82
|
+
internal.logger.groupEnd();
|
|
83
|
+
const logFriendlyHeaders = {};
|
|
84
|
+
response.headers.forEach((value, key)=>{
|
|
85
|
+
logFriendlyHeaders[key] = value;
|
|
86
|
+
});
|
|
87
|
+
internal.logger.groupCollapsed(`View response status and headers here.`);
|
|
88
|
+
internal.logger.log(`Response status: ${response.status}`);
|
|
89
|
+
internal.logger.log(`Response headers: ` + JSON.stringify(logFriendlyHeaders, null, 2));
|
|
90
|
+
internal.logger.groupEnd();
|
|
91
|
+
internal.logger.groupCollapsed(`View full response details here.`);
|
|
92
|
+
internal.logger.log(response.headers);
|
|
93
|
+
internal.logger.log(response);
|
|
94
|
+
internal.logger.groupEnd();
|
|
95
|
+
internal.logger.groupEnd();
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return cacheable;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* A class implementing the `cacheWillUpdate` lifecycle callback. This makes it
|
|
104
|
+
* easier to add in cacheability checks to requests made via Serwist's built-in
|
|
105
|
+
* strategies.
|
|
106
|
+
*/ class CacheableResponsePlugin {
|
|
107
|
+
_cacheableResponse;
|
|
108
|
+
/**
|
|
109
|
+
* To construct a new CacheableResponsePlugin instance you must provide at
|
|
110
|
+
* least one of the `config` properties.
|
|
111
|
+
*
|
|
112
|
+
* If both `statuses` and `headers` are specified, then both conditions must
|
|
113
|
+
* be met for the `Response` to be considered cacheable.
|
|
114
|
+
*
|
|
115
|
+
* @param config
|
|
116
|
+
*/ constructor(config){
|
|
117
|
+
this._cacheableResponse = new CacheableResponse(config);
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* @param options
|
|
121
|
+
* @returns
|
|
122
|
+
* @private
|
|
123
|
+
*/ cacheWillUpdate = async ({ response })=>{
|
|
124
|
+
if (this._cacheableResponse.isResponseCacheable(response)) {
|
|
125
|
+
return response;
|
|
126
|
+
}
|
|
127
|
+
return null;
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
exports.CacheableResponse = CacheableResponse;
|
|
132
|
+
exports.CacheableResponsePlugin = CacheableResponsePlugin;
|
package/package.json
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@serwist/cacheable-response",
|
|
3
|
+
"version": "8.0.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": "Google's Web DevRel Team, Serwist's Team",
|
|
7
|
+
"description": "This library takes a Response object and determines whether it's cacheable based on a specific configuration.",
|
|
8
|
+
"repository": "serwist/serwist",
|
|
9
|
+
"bugs": "https://github.com/serwist/serwist/issues",
|
|
10
|
+
"homepage": "https://ducanh-next-pwa.vercel.app",
|
|
11
|
+
"keywords": [
|
|
12
|
+
"serwist",
|
|
13
|
+
"serwistjs",
|
|
14
|
+
"service worker",
|
|
15
|
+
"sw",
|
|
16
|
+
"serwist-plugin"
|
|
17
|
+
],
|
|
18
|
+
"module": "./dist/index.js",
|
|
19
|
+
"main": "./dist/index.old.cjs",
|
|
20
|
+
"types": "./dist/index.d.ts",
|
|
21
|
+
"exports": {
|
|
22
|
+
".": {
|
|
23
|
+
"import": "./dist/index.js",
|
|
24
|
+
"require": "./dist/index.old.cjs",
|
|
25
|
+
"types": "./dist/index.d.ts"
|
|
26
|
+
},
|
|
27
|
+
"./package.json": "./package.json"
|
|
28
|
+
},
|
|
29
|
+
"dependencies": {
|
|
30
|
+
"@serwist/core": "8.0.0"
|
|
31
|
+
},
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"rollup": "3.28.1",
|
|
34
|
+
"@serwist/constants": "8.0.0"
|
|
35
|
+
},
|
|
36
|
+
"scripts": {
|
|
37
|
+
"build": "cross-env NODE_ENV=production rollup --waitForBundleInput --config rollup.config.js",
|
|
38
|
+
"lint": "eslint src --ext ts,tsx,js,jsx,cjs,mjs",
|
|
39
|
+
"typecheck": "tsc"
|
|
40
|
+
}
|
|
41
|
+
}
|
package/rollup.config.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
import { getRollupOptions } from "@serwist/constants/rollup";
|
|
3
|
+
|
|
4
|
+
import packageJson from "./package.json" assert { type: "json" };
|
|
5
|
+
|
|
6
|
+
const isDev = process.env.NODE_ENV === "development";
|
|
7
|
+
|
|
8
|
+
export default getRollupOptions({
|
|
9
|
+
packageJson,
|
|
10
|
+
jsFiles: [
|
|
11
|
+
{
|
|
12
|
+
input: "src/index.ts",
|
|
13
|
+
output: [
|
|
14
|
+
{
|
|
15
|
+
file: "dist/index.old.cjs",
|
|
16
|
+
format: "cjs",
|
|
17
|
+
exports: "named",
|
|
18
|
+
},
|
|
19
|
+
{
|
|
20
|
+
file: "dist/index.js",
|
|
21
|
+
format: "esm",
|
|
22
|
+
},
|
|
23
|
+
],
|
|
24
|
+
},
|
|
25
|
+
],
|
|
26
|
+
shouldEmitDeclaration: !isDev,
|
|
27
|
+
});
|
|
@@ -0,0 +1,140 @@
|
|
|
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
|
+
|
|
9
|
+
import { assert, getFriendlyURL, logger, SerwistError } from "@serwist/core/internal";
|
|
10
|
+
|
|
11
|
+
export interface CacheableResponseOptions {
|
|
12
|
+
/**
|
|
13
|
+
* One or more status codes that a `Response` can have to be considered cacheable.
|
|
14
|
+
*/
|
|
15
|
+
statuses?: number[];
|
|
16
|
+
/**
|
|
17
|
+
* A mapping of header names and expected values that a `Response` can have and be
|
|
18
|
+
* considered cacheable. If multiple headers are provided, only one needs to be present.
|
|
19
|
+
*/
|
|
20
|
+
headers?: { [headerName: string]: string };
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* This class allows you to set up rules determining what
|
|
25
|
+
* status codes and/or headers need to be present in order for a
|
|
26
|
+
* [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response)
|
|
27
|
+
* to be considered cacheable.
|
|
28
|
+
*/
|
|
29
|
+
class CacheableResponse {
|
|
30
|
+
private readonly _statuses?: CacheableResponseOptions["statuses"];
|
|
31
|
+
private readonly _headers?: CacheableResponseOptions["headers"];
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* To construct a new CacheableResponse instance you must provide at least
|
|
35
|
+
* one of the `config` properties.
|
|
36
|
+
*
|
|
37
|
+
* If both `statuses` and `headers` are specified, then both conditions must
|
|
38
|
+
* be met for the `Response` to be considered cacheable.
|
|
39
|
+
*
|
|
40
|
+
* @param config
|
|
41
|
+
*/
|
|
42
|
+
constructor(config: CacheableResponseOptions = {}) {
|
|
43
|
+
if (process.env.NODE_ENV !== "production") {
|
|
44
|
+
if (!(config.statuses || config.headers)) {
|
|
45
|
+
throw new SerwistError("statuses-or-headers-required", {
|
|
46
|
+
moduleName: "@serwist/cacheable-response",
|
|
47
|
+
className: "CacheableResponse",
|
|
48
|
+
funcName: "constructor",
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
if (config.statuses) {
|
|
53
|
+
assert!.isArray(config.statuses, {
|
|
54
|
+
moduleName: "@serwist/cacheable-response",
|
|
55
|
+
className: "CacheableResponse",
|
|
56
|
+
funcName: "constructor",
|
|
57
|
+
paramName: "config.statuses",
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
if (config.headers) {
|
|
62
|
+
assert!.isType(config.headers, "object", {
|
|
63
|
+
moduleName: "@serwist/cacheable-response",
|
|
64
|
+
className: "CacheableResponse",
|
|
65
|
+
funcName: "constructor",
|
|
66
|
+
paramName: "config.headers",
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
this._statuses = config.statuses;
|
|
72
|
+
this._headers = config.headers;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Checks a response to see whether it's cacheable or not, based on this
|
|
77
|
+
* object's configuration.
|
|
78
|
+
*
|
|
79
|
+
* @param response The response whose cacheability is being
|
|
80
|
+
* checked.
|
|
81
|
+
* @returns `true` if the `Response` is cacheable, and `false`
|
|
82
|
+
* otherwise.
|
|
83
|
+
*/
|
|
84
|
+
isResponseCacheable(response: Response): boolean {
|
|
85
|
+
if (process.env.NODE_ENV !== "production") {
|
|
86
|
+
assert!.isInstance(response, Response, {
|
|
87
|
+
moduleName: "@serwist/cacheable-response",
|
|
88
|
+
className: "CacheableResponse",
|
|
89
|
+
funcName: "isResponseCacheable",
|
|
90
|
+
paramName: "response",
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
let cacheable = true;
|
|
95
|
+
|
|
96
|
+
if (this._statuses) {
|
|
97
|
+
cacheable = this._statuses.includes(response.status);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
if (this._headers && cacheable) {
|
|
101
|
+
cacheable = Object.keys(this._headers).some((headerName) => {
|
|
102
|
+
return response.headers.get(headerName) === this._headers![headerName];
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
if (process.env.NODE_ENV !== "production") {
|
|
107
|
+
if (!cacheable) {
|
|
108
|
+
logger.groupCollapsed(
|
|
109
|
+
`The request for ` + `'${getFriendlyURL(response.url)}' returned a response that does ` + `not meet the criteria for being cached.`
|
|
110
|
+
);
|
|
111
|
+
|
|
112
|
+
logger.groupCollapsed(`View cacheability criteria here.`);
|
|
113
|
+
logger.log(`Cacheable statuses: ` + JSON.stringify(this._statuses));
|
|
114
|
+
logger.log(`Cacheable headers: ` + JSON.stringify(this._headers, null, 2));
|
|
115
|
+
logger.groupEnd();
|
|
116
|
+
|
|
117
|
+
const logFriendlyHeaders: { [key: string]: string } = {};
|
|
118
|
+
response.headers.forEach((value, key) => {
|
|
119
|
+
logFriendlyHeaders[key] = value;
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
logger.groupCollapsed(`View response status and headers here.`);
|
|
123
|
+
logger.log(`Response status: ${response.status}`);
|
|
124
|
+
logger.log(`Response headers: ` + JSON.stringify(logFriendlyHeaders, null, 2));
|
|
125
|
+
logger.groupEnd();
|
|
126
|
+
|
|
127
|
+
logger.groupCollapsed(`View full response details here.`);
|
|
128
|
+
logger.log(response.headers);
|
|
129
|
+
logger.log(response);
|
|
130
|
+
logger.groupEnd();
|
|
131
|
+
|
|
132
|
+
logger.groupEnd();
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
return cacheable;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
export { CacheableResponse };
|
|
@@ -0,0 +1,48 @@
|
|
|
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
|
+
|
|
9
|
+
import type { SerwistPlugin } from "@serwist/core";
|
|
10
|
+
|
|
11
|
+
import type { CacheableResponseOptions } from "./CacheableResponse.js";
|
|
12
|
+
import { CacheableResponse } from "./CacheableResponse.js";
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* A class implementing the `cacheWillUpdate` lifecycle callback. This makes it
|
|
16
|
+
* easier to add in cacheability checks to requests made via Serwist's built-in
|
|
17
|
+
* strategies.
|
|
18
|
+
*/
|
|
19
|
+
class CacheableResponsePlugin implements SerwistPlugin {
|
|
20
|
+
private readonly _cacheableResponse: CacheableResponse;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* To construct a new CacheableResponsePlugin instance you must provide at
|
|
24
|
+
* least one of the `config` properties.
|
|
25
|
+
*
|
|
26
|
+
* If both `statuses` and `headers` are specified, then both conditions must
|
|
27
|
+
* be met for the `Response` to be considered cacheable.
|
|
28
|
+
*
|
|
29
|
+
* @param config
|
|
30
|
+
*/
|
|
31
|
+
constructor(config: CacheableResponseOptions) {
|
|
32
|
+
this._cacheableResponse = new CacheableResponse(config);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* @param options
|
|
37
|
+
* @returns
|
|
38
|
+
* @private
|
|
39
|
+
*/
|
|
40
|
+
cacheWillUpdate: SerwistPlugin["cacheWillUpdate"] = async ({ response }) => {
|
|
41
|
+
if (this._cacheableResponse.isResponseCacheable(response)) {
|
|
42
|
+
return response;
|
|
43
|
+
}
|
|
44
|
+
return null;
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export { CacheableResponsePlugin };
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
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
|
+
|
|
9
|
+
import type { CacheableResponseOptions } from "./CacheableResponse.js";
|
|
10
|
+
import { CacheableResponse } from "./CacheableResponse.js";
|
|
11
|
+
import { CacheableResponsePlugin } from "./CacheableResponsePlugin.js";
|
|
12
|
+
|
|
13
|
+
export { CacheableResponse, CacheableResponsePlugin };
|
|
14
|
+
|
|
15
|
+
export type { CacheableResponseOptions };
|