@cloudflare/pages-shared 0.11.72 → 0.12.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/asset-server/handler.ts +40 -11
- package/package.json +3 -3
package/asset-server/handler.ts
CHANGED
|
@@ -72,6 +72,25 @@ type FindAssetEntryForPath<AssetEntry> = (
|
|
|
72
72
|
path: string
|
|
73
73
|
) => Promise<null | AssetEntry>;
|
|
74
74
|
|
|
75
|
+
function generateETagHeader(assetKey: string) {
|
|
76
|
+
// https://support.cloudflare.com/hc/en-us/articles/218505467-Using-ETag-Headers-with-Cloudflare
|
|
77
|
+
// We sometimes remove etags unless they are wrapped in quotes
|
|
78
|
+
const strongETag = `"${assetKey}"`;
|
|
79
|
+
const weakETag = `W/"${assetKey}"`;
|
|
80
|
+
return { strongETag, weakETag };
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function checkIfNoneMatch(
|
|
84
|
+
request: Request,
|
|
85
|
+
strongETag: string,
|
|
86
|
+
weakETag: string
|
|
87
|
+
) {
|
|
88
|
+
const ifNoneMatch = request.headers.get("if-none-match");
|
|
89
|
+
|
|
90
|
+
// We sometimes downgrade strong etags to a weak ones, so we need to check for both
|
|
91
|
+
return ifNoneMatch === weakETag || ifNoneMatch === strongETag;
|
|
92
|
+
}
|
|
93
|
+
|
|
75
94
|
type ServeAsset<AssetEntry> = (
|
|
76
95
|
assetEntry: AssetEntry,
|
|
77
96
|
options?: { preserve: boolean }
|
|
@@ -80,7 +99,10 @@ type ServeAsset<AssetEntry> = (
|
|
|
80
99
|
type CacheStatus = "hit" | "miss";
|
|
81
100
|
type CacheResult<A extends string> = `${A}-${CacheStatus}`;
|
|
82
101
|
export type HandlerMetrics = {
|
|
83
|
-
preservationCacheResult?:
|
|
102
|
+
preservationCacheResult?:
|
|
103
|
+
| CacheResult<"checked">
|
|
104
|
+
| "not-modified"
|
|
105
|
+
| "disabled";
|
|
84
106
|
earlyHintsResult?: CacheResult<"used" | "notused"> | "disabled";
|
|
85
107
|
};
|
|
86
108
|
|
|
@@ -518,22 +540,16 @@ export async function generateHandler<
|
|
|
518
540
|
|
|
519
541
|
const assetKey = getAssetKey(servingAssetEntry, content);
|
|
520
542
|
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
const weakEtag = `W/${etag}`;
|
|
525
|
-
|
|
526
|
-
const ifNoneMatch = request.headers.get("if-none-match");
|
|
527
|
-
|
|
528
|
-
// We sometimes downgrade strong etags to a weak ones, so we need to check for both
|
|
529
|
-
if (ifNoneMatch === weakEtag || ifNoneMatch === etag) {
|
|
543
|
+
const { strongETag, weakETag } = generateETagHeader(assetKey);
|
|
544
|
+
const isIfNoneMatch = checkIfNoneMatch(request, strongETag, weakETag);
|
|
545
|
+
if (isIfNoneMatch) {
|
|
530
546
|
return new NotModifiedResponse();
|
|
531
547
|
}
|
|
532
548
|
|
|
533
549
|
try {
|
|
534
550
|
const asset = await fetchAsset(assetKey);
|
|
535
551
|
const headers: Record<string, string> = {
|
|
536
|
-
etag,
|
|
552
|
+
etag: strongETag,
|
|
537
553
|
"content-type": asset.contentType,
|
|
538
554
|
};
|
|
539
555
|
let encodeBody: BodyEncoding = "automatic";
|
|
@@ -654,6 +670,19 @@ export async function generateHandler<
|
|
|
654
670
|
return new Response(null, preservedResponse);
|
|
655
671
|
}
|
|
656
672
|
if (assetKey) {
|
|
673
|
+
const { strongETag, weakETag } = generateETagHeader(assetKey);
|
|
674
|
+
const isIfNoneMatch = checkIfNoneMatch(
|
|
675
|
+
request,
|
|
676
|
+
strongETag,
|
|
677
|
+
weakETag
|
|
678
|
+
);
|
|
679
|
+
if (isIfNoneMatch) {
|
|
680
|
+
if (setMetrics) {
|
|
681
|
+
setMetrics({ preservationCacheResult: "not-modified" });
|
|
682
|
+
}
|
|
683
|
+
return new NotModifiedResponse();
|
|
684
|
+
}
|
|
685
|
+
|
|
657
686
|
const asset = await fetchAsset(assetKey);
|
|
658
687
|
|
|
659
688
|
if (asset) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cloudflare/pages-shared",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.12.0",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "https://github.com/cloudflare/workers-sdk.git",
|
|
@@ -13,10 +13,10 @@
|
|
|
13
13
|
"metadata-generator/**/*"
|
|
14
14
|
],
|
|
15
15
|
"dependencies": {
|
|
16
|
-
"miniflare": "3.
|
|
16
|
+
"miniflare": "3.20241218.0"
|
|
17
17
|
},
|
|
18
18
|
"devDependencies": {
|
|
19
|
-
"@cloudflare/workers-types": "^4.
|
|
19
|
+
"@cloudflare/workers-types": "^4.20241218.0",
|
|
20
20
|
"@miniflare/cache": "^2.14.2",
|
|
21
21
|
"@miniflare/core": "^2.14.2",
|
|
22
22
|
"@miniflare/html-rewriter": "^2.14.2",
|