Skip to content

Commit 3da069c

Browse files
committed
fix: throw known errors such as MethodNotAllowed, NotFoundError instead of 404 for kv assets
1 parent 3a8195d commit 3da069c

File tree

2 files changed

+51
-32
lines changed

2 files changed

+51
-32
lines changed

.changeset/cool-foxes-battle.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
'@sveltejs/adapter-cloudflare-workers': minor
3+
---
4+
return known errors for non-existent asset rather than throwing KVError

packages/adapter-cloudflare-workers/files/entry.js

Lines changed: 47 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1-
import { Server } from 'SERVER';
2-
import { manifest, prerendered } from 'MANIFEST';
3-
import { getAssetFromKV, mapRequestToAsset } from '@cloudflare/kv-asset-handler';
4-
import static_asset_manifest_json from '__STATIC_CONTENT_MANIFEST';
1+
import { Server } from "SERVER";
2+
import { manifest, prerendered } from "MANIFEST";
3+
import {
4+
getAssetFromKV,
5+
mapRequestToAsset,
6+
MethodNotAllowedError,
7+
NotFoundError,
8+
} from "@cloudflare/kv-asset-handler";
9+
import static_asset_manifest_json from "__STATIC_CONTENT_MANIFEST";
510
const static_asset_manifest = JSON.parse(static_asset_manifest_json);
611

712
const server = new Server(manifest);
@@ -25,23 +30,23 @@ export default {
2530
const res = await get_asset_from_kv(req, env, context);
2631
if (is_error(res.status)) return res;
2732

28-
const cache_control = url.pathname.startsWith(app_path + 'immutable/')
29-
? 'public, immutable, max-age=31536000'
30-
: 'no-cache';
33+
const cache_control = url.pathname.startsWith(app_path + "immutable/")
34+
? "public, immutable, max-age=31536000"
35+
: "no-cache";
3136

3237
return new Response(res.body, {
3338
headers: {
3439
// include original headers, minus cache-control which
3540
// is overridden, and etag which is no longer useful
36-
'cache-control': cache_control,
37-
'content-type': res.headers.get('content-type'),
38-
'x-robots-tag': 'noindex'
39-
}
41+
"cache-control": cache_control,
42+
"content-type": res.headers.get("content-type"),
43+
"x-robots-tag": "noindex",
44+
},
4045
});
4146
}
4247

4348
// prerendered pages and index.html files
44-
const pathname = url.pathname.replace(/\/$/, '');
49+
const pathname = url.pathname.replace(/\/$/, "");
4550
let file = pathname.substring(1);
4651

4752
try {
@@ -52,12 +57,12 @@ export default {
5257

5358
if (
5459
manifest.assets.has(file) ||
55-
manifest.assets.has(file + '/index.html') ||
56-
prerendered.has(pathname || '/')
60+
manifest.assets.has(file + "/index.html") ||
61+
prerendered.has(pathname || "/")
5762
) {
5863
return get_asset_from_kv(req, env, context, (request, options) => {
59-
if (prerendered.has(pathname || '/')) {
60-
url.pathname = '/' + prerendered.get(pathname || '/').file;
64+
if (prerendered.has(pathname || "/")) {
65+
url.pathname = "/" + prerendered.get(pathname || "/").file;
6166
return new Request(url.toString(), request);
6267
}
6368

@@ -71,13 +76,13 @@ export default {
7176
env,
7277
context,
7378
// @ts-expect-error lib.dom is interfering with workers-types
74-
caches
79+
caches,
7580
},
7681
getClientAddress() {
77-
return req.headers.get('cf-connecting-ip');
78-
}
82+
return req.headers.get("cf-connecting-ip");
83+
},
7984
});
80-
}
85+
},
8186
};
8287

8388
/**
@@ -86,19 +91,29 @@ export default {
8691
* @param {any} context
8792
*/
8893
async function get_asset_from_kv(req, env, context, map = mapRequestToAsset) {
89-
return await getAssetFromKV(
90-
{
91-
request: req,
92-
waitUntil(promise) {
93-
return context.waitUntil(promise);
94-
}
95-
},
96-
{
97-
ASSET_NAMESPACE: env.__STATIC_CONTENT,
98-
ASSET_MANIFEST: static_asset_manifest,
99-
mapRequestToAsset: map
94+
try {
95+
return await getAssetFromKV(
96+
{
97+
request: req,
98+
waitUntil(promise) {
99+
return context.waitUntil(promise);
100+
},
101+
},
102+
{
103+
ASSET_NAMESPACE: env.__STATIC_CONTENT,
104+
ASSET_MANIFEST: static_asset_manifest,
105+
mapRequestToAsset: map,
106+
},
107+
);
108+
} catch (e) {
109+
if (e instanceof NotFoundError) {
110+
return new Response("Not found", { status: 404 });
111+
} else if (e instanceof MethodNotAllowedError) {
112+
return new Response("Method not allowed", { status: 405 });
113+
} else {
114+
return new Response("Internal Error", { status: 500 });
100115
}
101-
);
116+
}
102117
}
103118

104119
/**

0 commit comments

Comments
 (0)