Skip to content

fix(plugin-cloud-storage): return updatedDoc from afterChange to reflect modified filenames and URLs (#18384) - #18391

Open
PINYOPATTANAWASANPORN wants to merge 1 commit into
payloadcms:mainfrom
PINYOPATTANAWASANPORN:fix/cloud-storage-updated-doc-return-18384
Open

PINYOPATTANAWASANPORN wants to merge 1 commit into
payloadcms:mainfrom
PINYOPATTANAWASANPORN:fix/cloud-storage-updated-doc-return-18384

Conversation

@PINYOPATTANAWASANPORN

Copy link
Copy Markdown
Contributor

Summary of Changes

  • In packages/plugin-cloud-storage/src/hooks/afterChange.ts, update docWithMetadata to return { ...doc, ...updatedDoc } instead of { ...doc, ...uploadMetadata } when select is not provided.
  • Added unit test in packages/plugin-cloud-storage/src/hooks/afterChange.spec.ts asserting that afterChange returns updated document fields (e.g. refreshed url matching the suffixed filename) when adapter upload metadata modifies the file.

Root Cause / Technical Context

When storage adapters modify upload metadata during handleUpload (such as @payloadcms/storage-vercel-blob with addRandomSuffix: true appending a random suffix to filename), the afterChange hook calls req.payload.update(...) to persist the metadata.

This update call properly executes field hooks and generates the updated url matching the new filename in updatedDoc. However, when select was undefined, afterChange previously returned:

docWithMetadata = select ? { ...doc, ...updatedDoc } : { ...doc, ...uploadMetadata }

Because doc still retained the pre-suffix url from before the upload and uploadMetadata only contained { filename: ... } without the recomputed url, the API response returned the new filename paired with the stale/pre-suffix url (which resulted in 404).

Using updatedDoc ensures that the response includes the recomputed url and all persisted metadata fields.

Verification & Testing

  • Added unit test in packages/plugin-cloud-storage/src/hooks/afterChange.spec.ts testing adapter metadata return with mocked handleUpload and payload.update.
  • Verified surgical 1-line code diff in afterChange.ts with strict LF line endings.

Fixes #18384

…ect modified filenames and URLs (payloadcms#18384)

When an adapter modifies upload metadata (e.g. storage-vercel-blob with
addRandomSuffix changing filename), afterChange calls payload.update to
persist the new metadata and recompute hooks (including url generation).
However, when select is not used, afterChange previously merged uploadMetadata
directly onto doc, preserving the pre-update url. Return updatedDoc so that
the response reflects the new filename and corresponding valid url.

Fixes payloadcms#18384

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

storage-vercel-blob with addRandomSuffix: a create or a file replacement answers with a url that gives 404

1 participant