Fix client-side Vercel Blob fallback never executing

HAS_VERCEL_BLOB_STORAGE depends on BLOB_READ_WRITE_TOKEN which is a
server-only env var (not NEXT_PUBLIC_*), so it evaluates to false in
the browser. When the R2 presigned-URL PUT fails (e.g. due to missing
CORS on the R2 bucket), the fallback branch was unreachable and the
original "Failed to fetch" error propagated to the UI.

vercelBlobUploadFromClient works client-side because it obtains the
token via a server round-trip to handleUploadUrl, so try it
unconditionally as a fallback.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Strtus 2026-05-19 10:54:33 +08:00
parent 66fa66d3d3
commit 0ac550c039

View File

@ -160,14 +160,17 @@ export const uploadFileFromClient = async (
try { try {
return await uploadFromClientViaPresignedUrl(file, fileName); return await uploadFromClientViaPresignedUrl(file, fileName);
} catch (error) { } catch (error) {
// Fall back to Vercel Blob when configured, so uploads stay available // HAS_VERCEL_BLOB_STORAGE relies on BLOB_READ_WRITE_TOKEN which is a
// even if third-party object storage is temporarily unreachable. // server-only variable (not NEXT_PUBLIC_*), so it is always false in
if (HAS_VERCEL_BLOB_STORAGE) { // the browser. vercelBlobUploadFromClient only needs the server-side
return vercelBlobUploadFromClient(file, fileName); // token via handleUploadUrl, so try it unconditionally as a fallback.
} try {
return await vercelBlobUploadFromClient(file, fileName);
} catch {
throw error; throw error;
} }
} }
}
return vercelBlobUploadFromClient(file, fileName); return vercelBlobUploadFromClient(file, fileName);
}; };