Hello. I have a static HTML site that I host with Cloudflare Workers. I’m now trying to migrate it from Wrangler 1 to Wrangler 2.
I just copied the contents of the old index.js file that Wrangler 1 used to the new index.ts file (see code below). The website is working fine, with the only problem being that when I enter a non-existing URL, the browser shows the contents of 404.html as raw text instead of parsing it as an HTML page. It worked fine with Wrangler 1, but now it doesn’t. Can you help me out?
import { getAssetFromKV, mapRequestToAsset } from '@cloudflare/kv-asset-handler'
addEventListener('fetch', event => {
event.respondWith(handleEvent(event))
})
async function handleEvent(event) {
try {
return await getAssetFromKV(event)
} catch (e) {
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, statusText: "Not Found" })
} catch (e) {
return new Response('Error 404: Page not found', { status: 404, statusText: "Not Found" })
}
}
}