Demo, all content is generated
Question

Edge function returns 546 when resizing uploaded photos

Solved · 291 views · asked by griffin_w · edited

Bolt wrote an edge function that downloads the uploaded image from storage, resizes it to 3 sizes with imagescript and uploads them back. Small images work. Photos from phones (4-8 MB) fail with status 546 and the log says:

CPU time exceeded
shutdown: WORKER_LIMIT

Is there a setting to give it more time?

What I’ve tried

Asked Bolt to optimize it, it switched to a different image library, same error. Looked for a timeout setting in the dashboard.

Comment
What plan are you on? It changes the wall clock limit but not the CPU one, so I suspect it won't help. henrik_l · edited
free plan griffin_w · edited

2 answers

Marked as helpful by the asker
chidi_eze · edited

There's no setting for it. Edge functions get about 2 seconds of actual CPU time per request (waiting on network doesn't count) and 256 MB of memory. Decoding and re-encoding a 12-megapixel photo three times in pure JS/WASM burns through that.

Options, easiest first:

  1. Don't resize at all, use Storage image transformations: getPublicUrl(path, { transform: { width: 400 } }). Resizing happens on request and gets cached. It's a paid-plan feature.
  2. Resize in the browser before upload with a canvas. Phones handle it fine and your uploads get 10x smaller too.
  3. Run the resize somewhere with real CPU (a small worker on Fly/Railway) and trigger it from a storage webhook.

For a Bolt app I'd do option 2, it also fixes slow uploads on mobile data.

Comment
Went with client side resizing (max 1600px, jpeg 0.8). Uploads went from 6MB to 300KB and the edge function is gone. griffin_w · edited
Image transformations saved me here too, although they are paid plan only. Worth it for us. tessa_k · edited
henrik_l · edited

Another reason to not resize in a function after upload: the user sees a broken thumbnail until the function finishes (or fails). Resizing before upload means the image is ready the moment the upload completes.

Comment