Skip to content

Commit 56ae111

Browse files
committed
fix(adapter-cloudflare-workers): throws status 404 if KVError instanceof
1 parent 77d0a96 commit 56ae111

File tree

2 files changed

+24
-12
lines changed

2 files changed

+24
-12
lines changed

.changeset/large-guests-fix.md

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

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

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Server } from 'SERVER';
22
import { manifest, prerendered } from 'MANIFEST';
33
import { getAssetFromKV, mapRequestToAsset } from '@cloudflare/kv-asset-handler';
44
import static_asset_manifest_json from '__STATIC_CONTENT_MANIFEST';
5+
import { KVError } from '@cloudflare/kv-asset-handler/dist/types';
56
const static_asset_manifest = JSON.parse(static_asset_manifest_json);
67

78
const server = new Server(manifest);
@@ -86,19 +87,25 @@ export default {
8687
* @param {any} context
8788
*/
8889
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);
90+
try {
91+
return await getAssetFromKV(
92+
{
93+
request: req,
94+
waitUntil(promise) {
95+
return context.waitUntil(promise);
96+
}
97+
},
98+
{
99+
ASSET_NAMESPACE: env.__STATIC_CONTENT,
100+
ASSET_MANIFEST: static_asset_manifest,
101+
mapRequestToAsset: map
94102
}
95-
},
96-
{
97-
ASSET_NAMESPACE: env.__STATIC_CONTENT,
98-
ASSET_MANIFEST: static_asset_manifest,
99-
mapRequestToAsset: map
100-
}
101-
);
103+
);
104+
} catch (err) {
105+
if (err instanceof KVError) err.status = 404;
106+
107+
throw err;
108+
}
102109
}
103110

104111
/**

0 commit comments

Comments
 (0)