Image Resizing Fails When addRandomSuffix: true in vercelBlobStorage Configuration
Describe the Bug
When uploading images in various sizes, only the original image file with its original dimensions is created. All other image sizes are not generated and remain empty.
This issue occurs only when the addRandomSuffix: true option is enabled in the vercelBlobStorage configuration.
When the addRandomSuffix option is removed, the functionality works as expected.
Link to the code that reproduces this issue
https://github.com/payloadcms/payload/tree/main/templates/with-vercel-website
Reproduction Steps
Add addRandomSuffix: true to the vercelBlobStorage config inside the payload config file.
Then login to the backend, navigate to the media collection and try to upload some image.
Which area(s) are affected? (Select all that apply)
plugin: other
Environment Info
payload: 3.2.1
next: 15.0.3
@payloadcms/db-mongodb: 3.2.1
@payloadcms/email-resend: 3.2.1
@payloadcms/graphql: 3.2.1
@payloadcms/live-preview: 3.2.1
@payloadcms/live-preview-react: 3.2.1
@payloadcms/next/utilities: 3.2.1
@payloadcms/plugin-cloud-storage: 3.2.1
@payloadcms/plugin-form-builder: 3.2.1
@payloadcms/plugin-nested-docs: 3.2.1
@payloadcms/plugin-seo: 3.2.1
@payloadcms/richtext-lexical: 3.2.1
@payloadcms/storage-vercel-blob: 3.2.1
@payloadcms/translations: 3.2.1
@payloadcms/ui/shared: 3.2.1
react: 19.0.0-rc-65a56d0e-20241020
react-dom: 19.0.0-rc-65a56d0e-20241020
Hey @mednet-ecrep,
Thanks for your patience here!
Can you confirm if this issue is still happening on your end?
I wasn't able to reproduce it on my initial testings
@PatrikKozak
I am also running into this exact problem, when I change the random suffix to false this solved the problem of resizing.
it's been a couple of months but I just had the issue. Using vercel blob, the random suffix gets added to the img url in vercel blob but the url is stored in the db without the suffix, hence they are 404'ing
Just confirming this is still an issue. Thanks!
Still running into this issue.
Encountered this on @payloadcms/[email protected] / @payloadcms/[email protected]. I diffed the relevant files against 3.87.0 (current latest) and they're unchanged, so it should still be present there — though I haven't run it on 3.87.0 to confirm. Here's the mechanism, plus a possible explanation for the earlier non-repro.
Root cause
adapter.js mutates the shared document object on every upload:
handleUpload: async ({ data, file: { buffer, filename, mimeType } }) => {
const result = await uploadFile({ ..., addRandomSuffix, filename, ... })
if (result.filename) {
data.filename = result.filename // `data` is the doc, shared by all files
}
return data // returns the whole doc, not a per-file patch
}
plugin-cloud-storage's getAfterChangeHook calls that for the original and every image size concurrently, then collapses the results last-write-wins:
const uploadResults = await Promise.all(
files.filter((file) => !file.clientUploadContext)
.map((file) => adapter.handleUpload({ ..., data: doc, file, ... }))
)
const uploadMetadata = uploadResults
.filter((r) => r != null && typeof r === 'object')
.reduce((acc, metadata) => ({ ...acc, ...metadata }), {})
// ... payload.update({ data: uploadMetadata })
Every element of uploadResults is the same object reference, so uploadMetadata.filename is whichever upload resolved last. Two consequences:
- The document's top-level
filename/urlpoint at one of the resized variants, not the original. - No
sizes.*.filenameis ever updated with its own random suffix, so every variant URL 404s — those blobs exist under randomized names nothing references, and the true original is orphaned entirely.
A resulting row (source image 1080×1350):
| field | stored value | blob |
|---|---|---|
filename |
Foo-500x500-<random suffix>.png |
200 |
sizes.thumbnail.filename |
Foo-300x375.png |
404 |
sizes.adminThumbnail.filename |
Foo-240x300.png |
404 |
| original | Foo.png |
404 |
A possible explanation for the non-repro
@PatrikKozak — this may be why it didn't reproduce during triage: the discriminator looks like imageSizes rather than the storage mode. Without imageSizes — or anywhere sharp isn't producing sizes — files has a single entry, so there's no race, data.filename correctly receives the randomized original name, and everything works. Might be worth re-checking whether the collection under test had an imageSizes array.
clientUploads doesn't avoid it — it makes it deterministic
getIncomingFiles sets clientUploadContext only on mainFile; the size entries never get it. So files.filter((file) => !file.clientUploadContext) skips only the original, and the sizes still upload server-side in both modes. Size generation itself is unconditional — generateFileData.js gates it on fileSupportsResize && imageSizes only, and under clientUploads the server re-downloads the blob to rebuild the buffer for sharp.
clientUploads: false— the original is in the race, so it occasionally wins andfilenamesurvives.clientUploads: true— the original is filtered out, so a variant always wins.
Variant URLs are broken either way.
Suggested direction
handleUpload returning the mutated doc is what makes the merge unsafe. Returning a per-file patch — { filename } for the main file, { sizes: { [size]: { filename } } } for a size — and deep-merging would fix both halves. That requires handleUpload to know which size it's handling; getIncomingFiles already knows and could pass it through.
🚀 This is included in version v3.92.1
Addressed in #18920