Skip to content

Instantly share code, notes, and snippets.

@oscartbeaumont
Last active January 17, 2022 13:07
Show Gist options
  • Save oscartbeaumont/56e7aaf2a6d510c8a5d48de47e2f2e51 to your computer and use it in GitHub Desktop.
Save oscartbeaumont/56e7aaf2a6d510c8a5d48de47e2f2e51 to your computer and use it in GitHub Desktop.
Cloudflare Workers Serve Specific File from Function Issue
// This handler does a 301 redirect to `/test` in a loop until browser throws ERR_TOO_MANY_REDIRECTS (using wrangler@beta)
export const onRequest: PagesFunction<unknown> = async ({ request, env }) => {
// The goal of this handler is to return a static file called `test.html`.
// Parsing `/test.html` directly returns a URL parsing error as per issue https://github.com/cloudflare/wrangler2/issues/165
const assetReq = new Request("http://fakehost/test.html", {
cf: request.cf,
});
const response = await env.ASSETS.fetch(assetReq);
return new Response(response.body, response);
};
// // As per issue https://github.com/cloudflare/wrangler2/issues/232 this acts as if `next();` was called and does not serve test.html
// export const onRequest: PagesFunction<unknown> = async ({ next }) => {
// return next("http://fakehost/test.html");
// };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment