@@ 37,6 37,22 @@ async function handleEvent(event) {
}
}
+ return await servePage(event)
+ } catch (e) {
+ return new Response(e.message || e.toString(), { status: 500 })
+ }
+}
+
+async function servePage(event) {
+ let options = {}
+
+ try {
+ if (DEBUG) {
+ options.cacheControl = {
+ bypassCache: true,
+ }
+ }
+
const page = await getAssetFromKV(event, options)
// allow headers to be altered
@@ 51,6 67,17 @@ async function handleEvent(event) {
return response
} catch (e) {
+ // if an error is thrown try to serve the asset at 404.html
+ if (!DEBUG) {
+ try {
+ let notFoundResponse = await getAssetFromKV(event, {
+ mapRequestToAsset: req => new Request(`${new URL(req.url).origin}/404.html`, req),
+ })
+
+ return new Response(notFoundResponse.body, { ...notFoundResponse, status: 404 })
+ } catch (e) {}
+ }
+
return new Response(e.message || e.toString(), { status: 500 })
}
}