2019-11-22 17:27:40 +01:00
|
|
|
import { getAssetFromKV, mapRequestToAsset } from '@cloudflare/kv-asset-handler'
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The DEBUG flag will do two things that help during development:
|
|
|
|
* 1. we will skip caching on the edge, which makes it easier to
|
|
|
|
* debug.
|
2020-07-02 09:17:05 +02:00
|
|
|
* 2. we will return an error message on exception in your response rather
|
2019-11-22 17:27:40 +01:00
|
|
|
* than the default 404.html page.
|
|
|
|
*/
|
|
|
|
const DEBUG = false
|
|
|
|
|
|
|
|
addEventListener('fetch', event => {
|
|
|
|
try {
|
|
|
|
event.respondWith(handleEvent(event))
|
|
|
|
} catch (e) {
|
|
|
|
if (DEBUG) {
|
|
|
|
return event.respondWith(
|
|
|
|
new Response(e.message || e.toString(), {
|
|
|
|
status: 500,
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
event.respondWith(new Response('Internal Error', { status: 500 }))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
async function handleEvent(event) {
|
|
|
|
const url = new URL(event.request.url)
|
|
|
|
let options = {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* You can add custom logic to how we fetch your assets
|
|
|
|
* by configuring the function `mapRequestToAsset`
|
|
|
|
*/
|
|
|
|
// options.mapRequestToAsset = handlePrefix(/^\/docs/)
|
|
|
|
|
2020-06-22 16:35:46 +02:00
|
|
|
// Path to test secrets passed through Wrangler Action. Create SECRET1 and SECRET2 secrets
|
|
|
|
// in the Action repo to something innocuous like "Hello" and "World!".
|
|
|
|
if (url.pathname === "/secret") {
|
|
|
|
let sec1 = (typeof SECRET1 !== 'undefined') ? SECRET1 : ""
|
|
|
|
let sec2 = (typeof SECRET2 !== 'undefined') ? SECRET2 : ""
|
|
|
|
return new Response(`${sec1} ${sec2}`)
|
|
|
|
}
|
|
|
|
|
2019-11-22 17:27:40 +01:00
|
|
|
try {
|
|
|
|
if (DEBUG) {
|
|
|
|
// customize caching
|
|
|
|
options.cacheControl = {
|
|
|
|
bypassCache: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return await getAssetFromKV(event, options)
|
|
|
|
} 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 })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Here's one example of how to modify a request to
|
|
|
|
* remove a specific prefix, in this case `/docs` from
|
|
|
|
* the url. This can be useful if you are deploying to a
|
|
|
|
* route on a zone, or if you only want your static content
|
|
|
|
* to exist at a specific path.
|
|
|
|
*/
|
|
|
|
function handlePrefix(prefix) {
|
|
|
|
return request => {
|
|
|
|
// compute the default (e.g. / -> index.html)
|
|
|
|
let defaultAssetKey = mapRequestToAsset(request)
|
|
|
|
let url = new URL(defaultAssetKey.url)
|
|
|
|
|
|
|
|
// strip the prefix from the path for lookup
|
|
|
|
url.pathname = url.pathname.replace(prefix, '/')
|
|
|
|
|
|
|
|
// inherit all other props from the default request
|
|
|
|
return new Request(url.toString(), defaultAssetKey)
|
|
|
|
}
|
|
|
|
}
|