~ekez/discord-notifier

dc7fd4235297218cd0bdfe0f2fc7e7cd156f39c4 — Zeke Medley 13 days ago d7e759e
Correctly server 404 page when appropriate:
1 files changed, 27 insertions(+), 0 deletions(-)

M discord-server/workers-site/index.js
M discord-server/workers-site/index.js => discord-server/workers-site/index.js +27 -0
@@ 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 })
    }
}