Have you encountered this error when trying to upload a CloudFlare Pages function?
Error: Failed to publish your Function. Got error: Uncaught Error: Some functionality, such as asynchronous I/O, timeouts, and generating random values, can only be performed while handling a request.
I ran into it recently when tying to deploy what I thought should be a relatively simple function.
Directions to tell me that the issue was at worker.mjs:11:28
didn't really help me with where to find the issue in my TypeScript code.
Through a process of trial and elimination I ended up finding that it was down to how I was creating Response
objects for my error cases.
const errorResponse = new Response("Error message", {
status: 400,
});
I had this at the top level of a module I was importing.
Turns out this is not allowed. Instead I just need to create the responses during the execution of the function by turning the assignment statements in to anonymous factory functions.
const errorResponse = () => new Response("Error message", {
status: 400,
});
This keeps the code looking the way I want and CloudFlare is happy to deploy it to a worker.
In retrospect the error message makes sense, but without the context of having solved it I had a really hard time.
Top comments (2)
Thanks, You saved me a lot of time!
Thanks, it really save tons of time here: github.com/irensaltali/serverlessa...