Skip to content

fix: preserve processed client upload contents - #18387

Closed
nathanlentz wants to merge 15 commits into
mainfrom
fix/vercel-blob-temp-uploads
Closed

nathanlentz wants to merge 15 commits into
mainfrom
fix/vercel-blob-temp-uploads

Conversation

@nathanlentz

@nathanlentz nathanlentz commented Sep 29, 2026 •

Copy link
Copy Markdown
Collaborator

Description

Fixes #18333. Processed direct-client uploads could leave a zero-byte Vercel Blob object while the document reported the processed file size. The upload now reads the processed temporary file. GCS and R2 use the temporary file too when one exists.

The final crop and resize bytes are written to that file. Processed client uploads keep the object key assigned by the upload receipt. Server-side uploads retain their configured random-suffix behavior.

Shared-hook behavior

  • The cloud-storage hook saves only values an adapter changed. It does not write an unchanged copy of the document.
  • Image-size upload results use File.sizeName to update the matching size, even when sizes share a filename.
  • After upload, the hook clears req.file, req.payloadUploadSizes, and req.context._payloadCloudStorage so later operations do not upload the same file again.

Validation

  • Vercel Blob client-upload integration tests cover crop, resize, image sizes, formats, and upload prefixes.
  • Unit tests cover temporary-file uploads for GCS and R2. R2 has not been verified with a real Workers or Miniflare binding.

Backport to 3.x: #18388.

@github-actions

github-actions Bot commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

📦 esbuild Bundle Analysis for payload

This analysis was generated by esbuild-bundle-analyzer. 🤖

Meta File Out File Size (raw) Note
packages/next/meta_index.json esbuild/index.js 216.84 KB ⚠️ +2.07 KB (+1.0%)
packages/payload/meta_index.json esbuild/index.js 1.87 MB ⚠️ +87.05 KB (+4.9%)
packages/payload/meta_shared.json esbuild/exports/shared.js 562.36 KB ⚠️ +13.05 KB (+2.4%)
packages/richtext-lexical/meta_client.json esbuild/exports/client_optimized/index.js 296.14 KB ⚠️ +9.64 KB (+3.4%)
packages/ui/meta_client.json esbuild/exports/client_optimized/index.js 36.81 KB ⚠️ +266 B (+0.7%)
packages/ui/meta_shared.json esbuild/exports/shared_optimized/index.js 18.98 KB ⚠️ +34 B (+0.2%)
Largest paths These visualization shows top 20 largest paths in the bundle.

Meta file: packages/next/meta_index.json, Out file: esbuild/index.js

Path Size
../../node_modules ${{\color{Goldenrod}{ ████████████████████████▋ }}}$ 98.8%, 212.32 KB
dist/adapters/router.js ${{\color{Goldenrod}{ }}}$ 0.3%, 718 B
dist/adapters/server.js ${{\color{Goldenrod}{ }}}$ 0.2%, 533 B
dist/adapters/layout.js ${{\color{Goldenrod}{ }}}$ 0.2%, 529 B
dist/adapters/views.js ${{\color{Goldenrod}{ }}}$ 0.2%, 324 B
dist/utilities/initAdminContext.js ${{\color{Goldenrod}{ }}}$ 0.1%, 315 B
dist/utilities/selectiveCache.js ${{\color{Goldenrod}{ }}}$ 0.1%, 263 B
dist/esbuildEntry.js ${{\color{Goldenrod}{ }}}$ 0.0%, 0 B

Meta file: packages/payload/meta_index.json, Out file: esbuild/index.js

Path Size
../../node_modules ${{\color{Goldenrod}{ █████████████████▉ }}}$ 71.9%, 1.34 MB
dist/collections/operations ${{\color{Goldenrod}{ ▋ }}}$ 2.9%, 54.77 KB
dist/fields/hooks ${{\color{Goldenrod}{ ▋ }}}$ 2.5%, 46.15 KB
dist/auth/operations ${{\color{Goldenrod}{ ▏ }}}$ 0.9%, 16.95 KB
dist/globals/operations ${{\color{Goldenrod}{ ▏ }}}$ 0.9%, 16.18 KB
dist/utilities/configToJSONSchema.js ${{\color{Goldenrod}{ ▏ }}}$ 0.9%, 16.02 KB
dist/queues/operations ${{\color{Goldenrod}{ ▏ }}}$ 0.8%, 14.31 KB
dist/fields/config ${{\color{Goldenrod}{ ▏ }}}$ 0.7%, 13.65 KB
dist/utilities/telemetry ${{\color{Goldenrod}{ ▏ }}}$ 0.6%, 11.88 KB
dist/collections/config ${{\color{Goldenrod}{ ▏ }}}$ 0.6%, 11.00 KB
dist/fields/validations.js ${{\color{Goldenrod}{ ▏ }}}$ 0.6%, 10.82 KB
dist/cli/commands ${{\color{Goldenrod}{ ▏ }}}$ 0.5%, 9.92 KB
dist/config/orderable ${{\color{Goldenrod}{ ▏ }}}$ 0.5%, 8.76 KB
dist/uploads/fetchAPI-multipart ${{\color{Goldenrod}{ }}}$ 0.4%, 8.38 KB
dist/database/migrations ${{\color{Goldenrod}{ }}}$ 0.4%, 8.16 KB
dist/config/sanitize.js ${{\color{Goldenrod}{ }}}$ 0.4%, 7.95 KB
dist/index.js ${{\color{Goldenrod}{ }}}$ 0.4%, 7.79 KB
dist/hierarchy/utils ${{\color{Goldenrod}{ }}}$ 0.4%, 7.66 KB
dist/auth/strategies ${{\color{Goldenrod}{ }}}$ 0.4%, 7.36 KB
dist/utilities/entityInputSchema ${{\color{Goldenrod}{ }}}$ 0.4%, 7.34 KB
(other) ${{\color{Goldenrod}{ ███████ }}}$ 28.1%, 524.45 KB

Meta file: packages/payload/meta_shared.json, Out file: esbuild/exports/shared.js

Path Size
../../node_modules ${{\color{Goldenrod}{ ██████████████████████▏ }}}$ 88.9%, 495.80 KB
dist/fields/validations.js ${{\color{Goldenrod}{ ▍ }}}$ 1.9%, 10.79 KB
dist/fields/config ${{\color{Goldenrod}{ ▎ }}}$ 1.0%, 5.83 KB
dist/utilities/traverseFields.js ${{\color{Goldenrod}{ ▏ }}}$ 0.8%, 4.34 KB
dist/utilities/deepCopyObject.js ${{\color{Goldenrod}{ ▏ }}}$ 0.6%, 3.52 KB
dist/collections/config ${{\color{Goldenrod}{ ▏ }}}$ 0.6%, 3.42 KB
dist/config/orderable ${{\color{Goldenrod}{ ▏ }}}$ 0.6%, 3.13 KB
dist/fields/baseFields ${{\color{Goldenrod}{ ▏ }}}$ 0.5%, 2.79 KB
dist/config/client.js ${{\color{Goldenrod}{ ▏ }}}$ 0.5%, 2.56 KB
dist/auth/cookies.js ${{\color{Goldenrod}{ }}}$ 0.3%, 1.55 KB
dist/utilities/flattenTopLevelFields.js ${{\color{Goldenrod}{ }}}$ 0.3%, 1.42 KB
dist/utilities/getVersionsConfig.js ${{\color{Goldenrod}{ }}}$ 0.2%, 1.04 KB
dist/globals/config ${{\color{Goldenrod}{ }}}$ 0.2%, 939 B
dist/utilities/unflatten.js ${{\color{Goldenrod}{ }}}$ 0.2%, 850 B
dist/utilities/flattenAllFields.js ${{\color{Goldenrod}{ }}}$ 0.1%, 798 B
dist/utilities/sanitizeUserDataForEmail.js ${{\color{Goldenrod}{ }}}$ 0.1%, 713 B
dist/auth/extractJWT.js ${{\color{Goldenrod}{ }}}$ 0.1%, 696 B
dist/utilities/getFieldPermissions.js ${{\color{Goldenrod}{ }}}$ 0.1%, 651 B
dist/utilities/fieldPath.js ${{\color{Goldenrod}{ }}}$ 0.1%, 639 B
dist/utilities/getSafeRedirect.js ${{\color{Goldenrod}{ }}}$ 0.1%, 632 B
(other) ${{\color{Goldenrod}{ ██▊ }}}$ 11.1%, 61.67 KB

Meta file: packages/richtext-lexical/meta_client.json, Out file: esbuild/exports/client_optimized/index.js

Path Size
dist/features/blocks ${{\color{Goldenrod}{ ███▋ }}}$ 14.6%, 42.74 KB
dist/lexical/plugins ${{\color{Goldenrod}{ ██▉ }}}$ 11.8%, 34.51 KB
dist/lexical/ui ${{\color{Goldenrod}{ ██▊ }}}$ 11.4%, 33.54 KB
dist/features/table ${{\color{Goldenrod}{ ██▎ }}}$ 9.4%, 27.46 KB
dist/features/link ${{\color{Goldenrod}{ █▋ }}}$ 6.5%, 18.92 KB
dist/features/toolbars ${{\color{Goldenrod}{ █▌ }}}$ 6.3%, 18.41 KB
dist/features/upload ${{\color{Goldenrod}{ █▏ }}}$ 4.9%, 14.28 KB
dist/features/textState ${{\color{Goldenrod}{ ▉ }}}$ 3.8%, 11.08 KB
dist/lexical/utils ${{\color{Goldenrod}{ ▊ }}}$ 3.4%, 10.02 KB
dist/features/relationship ${{\color{Goldenrod}{ ▊ }}}$ 3.2%, 9.43 KB
dist/features/converters ${{\color{Goldenrod}{ ▋ }}}$ 2.9%, 8.40 KB
dist/utilities/fieldsDrawer ${{\color{Goldenrod}{ ▋ }}}$ 2.8%, 8.12 KB
dist/features/debug ${{\color{Goldenrod}{ ▋ }}}$ 2.5%, 7.40 KB
dist/lexical/config ${{\color{Goldenrod}{ ▍ }}}$ 1.8%, 5.14 KB
dist/features/indent ${{\color{Goldenrod}{ ▎ }}}$ 1.4%, 4.19 KB
dist/features/lists ${{\color{Goldenrod}{ ▎ }}}$ 1.3%, 3.67 KB
dist/lexical/LexicalEditor.js ${{\color{Goldenrod}{ ▎ }}}$ 1.1%, 3.33 KB
dist/features/format ${{\color{Goldenrod}{ ▎ }}}$ 1.1%, 3.28 KB
dist/features/horizontalRule ${{\color{Goldenrod}{ ▎ }}}$ 1.1%, 3.18 KB
dist/field/Field.js ${{\color{Goldenrod}{ ▎ }}}$ 1.0%, 3.05 KB
(other) ${{\color{Goldenrod}{ █████████████████████▎ }}}$ 85.4%, 250.19 KB

Meta file: packages/ui/meta_client.json, Out file: esbuild/exports/client_optimized/index.js

Path Size
dist/exports/client ${{\color{Goldenrod}{ █████████████████████████ }}}$ 100.0%, 27.10 KB

Meta file: packages/ui/meta_shared.json, Out file: esbuild/exports/shared_optimized/index.js

Path Size
dist/graphics/Logo ${{\color{Goldenrod}{ ███████▋ }}}$ 30.7%, 5.61 KB
../../node_modules ${{\color{Goldenrod}{ ███▌ }}}$ 14.5%, 2.65 KB
dist/graphics/Icon ${{\color{Goldenrod}{ ██ }}}$ 8.3%, 1.51 KB
dist/utilities/formatDocTitle ${{\color{Goldenrod}{ █▊ }}}$ 7.2%, 1.32 KB
dist/providers/TableColumns ${{\color{Goldenrod}{ █▏ }}}$ 4.7%, 866 B
dist/utilities/getGlobalData.js ${{\color{Goldenrod}{ █ }}}$ 4.2%, 762 B
dist/utilities/api.js ${{\color{Goldenrod}{ █ }}}$ 4.1%, 756 B
dist/utilities/groupNavItems.js ${{\color{Goldenrod}{ █ }}}$ 4.1%, 745 B
dist/elements/Translation ${{\color{Goldenrod}{ ▋ }}}$ 2.7%, 493 B
dist/utilities/handleTakeOver.js ${{\color{Goldenrod}{ ▌ }}}$ 2.4%, 440 B
dist/utilities/traverseForLocalizedFields.js ${{\color{Goldenrod}{ ▌ }}}$ 2.3%, 419 B
dist/elements/withMergedProps ${{\color{Goldenrod}{ ▍ }}}$ 1.9%, 339 B
dist/utilities/getNavGroups.js ${{\color{Goldenrod}{ ▍ }}}$ 1.8%, 338 B
dist/utilities/getVisibleEntities.js ${{\color{Goldenrod}{ ▍ }}}$ 1.8%, 329 B
dist/elements/WithServerSideProps ${{\color{Goldenrod}{ ▎ }}}$ 1.3%, 232 B
dist/layouts/Root ${{\color{Goldenrod}{ ▎ }}}$ 1.3%, 230 B
dist/utilities/handleGoBack.js ${{\color{Goldenrod}{ ▎ }}}$ 1.0%, 180 B
dist/fields/mergeFieldStyles.js ${{\color{Goldenrod}{ ▏ }}}$ 0.9%, 158 B
dist/forms/Form ${{\color{Goldenrod}{ ▏ }}}$ 0.8%, 152 B
dist/utilities/handleBackToDashboard.js ${{\color{Goldenrod}{ ▏ }}}$ 0.8%, 152 B
(other) ${{\color{Goldenrod}{ █████████████████▎ }}}$ 69.3%, 12.68 KB
Details

Next to the size is how much the size has increased or decreased compared with the base branch of this PR.

  • ‼️: Size increased by 20% or more. Special attention should be given to this.
  • ⚠️: Size increased in acceptable range (lower than 20%).
  • ✅: No change or even downsized.
  • 🗑️: The out file is deleted: not found in base branch.
  • 🆕: The out file is newly found: will be added to base branch.

@nathanlentz
nathanlentz marked this pull request as ready for review September 29, 2026 18:32
@nathanlentz nathanlentz changed the title fix(storage-vercel-blob): preserve processed client upload contents fix: preserve processed client upload contents Sep 30, 2026

@paulpopus paulpopus left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A better approach might be to keep client-upload information on the file. Do not infer it from _objectKey or request context.

Define the upload state

type ClientUploadState = {
  isProcessed: boolean
  originalStorageFilePath: string
}

type UploadFile = {
  data: Buffer
  mimetype: string
  name: string
  size: number
  tempFilePath?: string
  clientUpload?: ClientUploadState
}

Add the state after verifying the upload receipt

const file: UploadFile = {
  data: Buffer.alloc(0),
  mimetype: response.headers.get('Content-Type') || instructions.mimeType,
  name: instructions.filename,
  size: instructions.size,
  tempFilePath,
  clientUpload: {
    isProcessed: false,
    originalStorageFilePath: receipt.storageFilePath,
  },
}

This records that the file came from a verified client upload. It also records the exact object path assigned by the server.

Update the complete file after processing

When Payload processes the file, update the file object and put it back on the request:

const processedFile: UploadFile = {
  ...file,
  data: Buffer.alloc(0),
  mimetype: processedMimeType,
  name: processedFilename,
  size: processedSize,
  tempFilePath,
  clientUpload: file.clientUpload
    ? {
        ...file.clientUpload,
        isProcessed: true,
      }
    : undefined,
}

req.file = processedFile

This prevents req.file from retaining an old uploadReference, MIME type, filename, or file size.

Decide whether the file needs uploading

The storage hook can make this decision from the file state:

const shouldUploadFile = !file.clientUpload || file.clientUpload.isProcessed

if (shouldUploadFile) {
  await adapter.handleUpload({
    file,
    storageFilePath,
  })
}

The rules are:

  • Server uploads must be uploaded.
  • Unprocessed client uploads are already stored and can be skipped.
  • Processed client uploads must be uploaded again.

Keep the server-issued key

Adapters no longer need to inspect _objectKey or request context:

const shouldAddRandomSuffix = file.clientUpload ? false : addRandomSuffix

This also works for legacy documents without _objectKey.

Remove the original object when its path changes

A format conversion can change image.png to image.webp. Remove the original client-uploaded object after the processed upload and metadata update succeed:

const originalStorageFilePath = file.clientUpload?.originalStorageFilePath

if (
  originalStorageFilePath &&
  originalStorageFilePath !== storageFilePath
) {
  await adapter.handleDelete({
    storageFilePath: originalStorageFilePath,
  })
}

This prevents unused objects from remaining in storage.

Isolate bulk-operation request state

Each document in a parallel bulk operation should receive separate mutable request state:

const documentReq = isolateObjectProperty(req, [
  'context',
  'file',
  'payloadUploadSizes',
  'query',
])

documentReq.context = { ...req.context }
documentReq.query = { ...req.query }

This prevents one document from changing uploadEdits or skipCloudStorage while another document is still processing.

Result

This keeps the important facts together:

  • where the client uploaded the original file;
  • whether Payload processed the file;
  • whether the file needs uploading again;
  • whether the original object needs deletion.

It also removes the need to infer file state from _objectKey, temporary-file paths, or shared request context.

Regardless here are other findings on the PR, happy to discuss directly if you want to change the direction of the solution here:

delete fileForResize.uploadReference
if (file.tempFilePath) {
await fs.writeFile(file.tempFilePath, croppedImage)
await fs.writeFile(file.tempFilePath, fileForResize.data)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When detected and submitted MIME types differ, generateFileData clones file and removes uploadReference only from that clone. req.file keeps the reference, so the storage hook skips the processed main file. This can serve original bytes or make the saved URL point to no object. Synchronise the processed state back to req.file, and add a client-upload test with a MIME mismatch and processing.

storageFilePath: buildUploadStoragePathData({
collectionPrefix,
docPrefix: dataForUpload.prefix,
filename: file.filename,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

formatOptions can change image.png to image.webp, so this upload uses a different path from the browser upload. Create operations have no previousDoc, so the original object remains in storage. Delete the original client-upload path after the processed upload succeeds, and add a format-conversion test that checks the final object count.

req.payloadUploadSizes = undefined
const uploadEdits = req.query?.uploadEdits
if (req.query) {
delete req.query.uploadEdits

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bulk document requests share req.query and req.context, but this code changes both across an awaited update. One operation can restore uploadEdits or remove skipCloudStorage while another nested update is still running. Isolate these properties for each document and add a parallel bulk crop or reprocessing test.

const clientUploadTempFilePath = req.context?._payloadClientUploadTempFile
const isProcessedClientUpload = Boolean(
clientUploads &&
data._objectKey &&

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Authorised overwrite receipts for legacy documents can have no _objectKey, so this condition does not identify their processed client uploads. With addRandomSuffix: true, the processed overwrite moves to a second key and changes the document URL. Use the verified temporary-file marker without requiring _objectKey, and add a legacy overwrite test.

nathanlentz added a commit that referenced this pull request Oct 23, 2026
…ploads with addRandomSuffix (#18921)

Stacked on #18913. Review and merge #18388 and #18913 first.

## Summary

With `addRandomSuffix: true`, a processed client upload was saved again
under a new name with a suffix. The original file from the browser
stayed in storage, and nothing used it. Now the processed file replaces
the original file, and the document keeps the original filename.

## How

- The server already saves the temp-file path of a client upload in
`req.context` for cleanup. The adapter turns off `addRandomSuffix` when
the file's `tempFilePath` is the same as that saved path.
- The client cannot set this value, because the server sets both paths.
- The check does not use `_objectKey`, so legacy overwrite receipts
without `_objectKey` are covered too.
- `payload` now exports `CLIENT_UPLOAD_TEMP_FILE_PATH_CONTEXT_KEY`, so
the adapter does not copy the key string.

## Related work

- Stack: #18388 → #18913 → this PR
- Reported in a comment on #18333 and in the #18387 review.
- #18920 fixes a separate `addRandomSuffix` bug with image sizes. Both
PRs add `packages/storage-vercel-blob/src/adapter.spec.ts`, so the
second one to merge needs a rebase.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

storage-vercel-blob: clientUploads stores WebP, GIF and cropped uploads as 0-byte objects

2 participants