@just-be/wildcard 0.5.0 → 0.6.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/package.json +1 -1
- package/src/handlers/static.ts +19 -9
package/package.json
CHANGED
package/src/handlers/static.ts
CHANGED
|
@@ -22,14 +22,15 @@ export const handleStatic: Handler<StaticConfig, { fileLoader: FileLoader }> = a
|
|
|
22
22
|
const basePath = subdomain;
|
|
23
23
|
|
|
24
24
|
return spa
|
|
25
|
-
? handleSpaMode(fileLoader, basePath, url.pathname)
|
|
26
|
-
: handleStaticMode(fileLoader, basePath, url.pathname, fallback);
|
|
25
|
+
? handleSpaMode(fileLoader, basePath, url.pathname, request)
|
|
26
|
+
: handleStaticMode(fileLoader, basePath, url.pathname, request, fallback);
|
|
27
27
|
};
|
|
28
28
|
|
|
29
29
|
async function handleSpaMode(
|
|
30
30
|
fileLoader: FileLoader,
|
|
31
31
|
basePath: string,
|
|
32
|
-
pathname: string
|
|
32
|
+
pathname: string,
|
|
33
|
+
request: Request
|
|
33
34
|
): Promise<Response> {
|
|
34
35
|
// Sanitize paths to prevent directory traversal
|
|
35
36
|
const sanitizedBase = sanitizePath(basePath);
|
|
@@ -53,7 +54,7 @@ async function handleSpaMode(
|
|
|
53
54
|
const fileObject = await fileLoader.loadFile(key);
|
|
54
55
|
|
|
55
56
|
if (fileObject) {
|
|
56
|
-
return buildFileResponse(fileObject, key);
|
|
57
|
+
return buildFileResponse(fileObject, key, request);
|
|
57
58
|
}
|
|
58
59
|
}
|
|
59
60
|
|
|
@@ -70,13 +71,14 @@ async function handleSpaMode(
|
|
|
70
71
|
return new Response("File not found", { status: 404 });
|
|
71
72
|
}
|
|
72
73
|
|
|
73
|
-
return buildFileResponse(indexFile, indexKey);
|
|
74
|
+
return buildFileResponse(indexFile, indexKey, request);
|
|
74
75
|
}
|
|
75
76
|
|
|
76
77
|
async function handleStaticMode(
|
|
77
78
|
fileLoader: FileLoader,
|
|
78
79
|
basePath: string,
|
|
79
80
|
pathname: string,
|
|
81
|
+
request: Request,
|
|
80
82
|
fallback?: string
|
|
81
83
|
): Promise<Response> {
|
|
82
84
|
// Sanitize paths to prevent directory traversal
|
|
@@ -99,7 +101,7 @@ async function handleStaticMode(
|
|
|
99
101
|
const fileObject = await fileLoader.loadFile(key);
|
|
100
102
|
|
|
101
103
|
if (fileObject) {
|
|
102
|
-
return buildFileResponse(fileObject, key);
|
|
104
|
+
return buildFileResponse(fileObject, key, request);
|
|
103
105
|
}
|
|
104
106
|
|
|
105
107
|
// Try fallback if configured
|
|
@@ -113,14 +115,14 @@ async function handleStaticMode(
|
|
|
113
115
|
const fallbackObject = await fileLoader.loadFile(fallbackKey);
|
|
114
116
|
|
|
115
117
|
if (fallbackObject) {
|
|
116
|
-
return buildFileResponse(fallbackObject, fallbackKey);
|
|
118
|
+
return buildFileResponse(fallbackObject, fallbackKey, request);
|
|
117
119
|
}
|
|
118
120
|
}
|
|
119
121
|
|
|
120
122
|
return new Response("File not found", { status: 404 });
|
|
121
123
|
}
|
|
122
124
|
|
|
123
|
-
function buildFileResponse(fileObject: FileObject, key: string): Response {
|
|
125
|
+
function buildFileResponse(fileObject: FileObject, key: string, request: Request): Response {
|
|
124
126
|
const headers = new Headers(fileObject.headers);
|
|
125
127
|
|
|
126
128
|
if (fileObject.etag) {
|
|
@@ -148,8 +150,16 @@ function buildFileResponse(fileObject: FileObject, key: string): Response {
|
|
|
148
150
|
}
|
|
149
151
|
|
|
150
152
|
// Cache control with Vary header for cache poisoning prevention
|
|
151
|
-
headers.set("Cache-Control", "public, max-age=3600");
|
|
153
|
+
headers.set("Cache-Control", "public, max-age=3600"); // 1 hour browser cache
|
|
154
|
+
headers.set("CDN-Cache-Control", "public, max-age=604800"); // 7 days edge cache
|
|
152
155
|
headers.set("Vary", "Accept-Encoding");
|
|
153
156
|
|
|
157
|
+
// Handle conditional requests (If-None-Match)
|
|
158
|
+
const ifNoneMatch = request.headers.get("If-None-Match");
|
|
159
|
+
if (ifNoneMatch && fileObject.etag && ifNoneMatch === fileObject.etag) {
|
|
160
|
+
// Return 304 Not Modified with headers but no body
|
|
161
|
+
return new Response(null, { status: 304, headers });
|
|
162
|
+
}
|
|
163
|
+
|
|
154
164
|
return new Response(fileObject.body, { headers });
|
|
155
165
|
}
|