I have an API endpoint set up to serve photos from an R2 bucket. The endpoint looks like this:
import { env } from "cloudflare:workers";
import { route } from "rwsdk/router";
import type { RequestInfo } from "rwsdk/worker";
import { STATUS } from "@/constants";
type PhotoByFileNameRequest = RequestInfo<{
fileName: string;
}>;
const photoByFileName = route("/photo/:fileName", async request => {
const {
params: { fileName },
} = request as PhotoByFileNameRequest;
try {
const object = await env.PHOTOS.get(fileName);
if (!object) {
return new Response("Photo not found", {
status: STATUS.NotFound404.code,
});
}
const headers = new Headers();
object.writeHttpMetadata(headers);
headers.set("Content-Type", object.httpMetadata?.contentType || "image/*");
headers.set("Cache-Control", "public, max-age=31536000");
return new Response(object.body, {
headers,
});
} catch (error) {
return new Response(JSON.stringify(error), {
status: STATUS.InternalServerError500.code,
});
}
});
export const apiPhotoRoutes = photoByFileName;
Error is:
[vite] Internal server error: The Workers runtime canceled this request because it detected that your Worker's code had hung and would never generate a response. Refer to: https://developers.cloudflare.com/workers/observability/errors/
Warning: A promise was resolved or rejected from a different request context than the one it was created in. However, the creating request has already been completed or canceled. Continuations for that request are unlikely to run safely and have been canceled. If this behavior breaks your worker, consider setting the `no_handle_cross_request_promise_resolution` compatibility flag for your worker.
at Object.resolve (<anonymous>)
The client shows 500s for each failed req, but my route's catch isn't getting any errors. Also, all the images resolve as individual URLs