Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Client Side Media: Optimize previously uploaded media#79509

Open
adamsilverstein wants to merge 4 commits into
trunkfrom
feature/79364-optimize-existing-media
Open

Client Side Media: Optimize previously uploaded media#79509
adamsilverstein wants to merge 4 commits into
trunkfrom
feature/79364-optimize-existing-media

Conversation

@adamsilverstein

Copy link
Copy Markdown
Member

What?

Adds the ability to optimize previously uploaded media using the client-side (WASM/libvips) pipeline. A new "Optimize" control appears in the inspector of image-bearing blocks; clicking it fetches the attachment's original file, re-compresses it in the browser, regenerates sub-sizes, uploads the result as a new attachment, and repoints the block at it.

Closes part of #76756. Implements #79364.

Why?

Client-side media currently optimizes media only at upload time. Sites that predate the feature (or media uploaded before it was enabled) get no benefit. This lets users re-process existing media (e.g. compress a legacy JPEG, regenerate sub-sizes) using the same in-browser pipeline, with graceful fallback when the browser can't process an item. This is one of the capabilities called out for the 7.1 cycle.

How?

Store (@wordpress/upload-media):

  • New public action optimizeExistingItem( { id, url, fileName, onSuccess, onError, additionalData } ) that enqueues a queue item (with sourceUrl/sourceAttachmentId and a StubFile placeholder) whose operations are [ FetchRemoteFile, TranscodeImage(same format), Upload, ThumbnailGeneration, Finalize ].
  • New FetchRemoteFile operation + fetchFile() utility to download the original by URL.
  • Reuses the existing TranscodeImage operation for same-format re-compression (no duplicate Compress op), and the existing sideload/finalize chain to regenerate sub-sizes.
  • New FETCH_REMOTE_FILE_ERROR code. Unsupported/unprocessable formats are skipped via onError, leaving the original attachment untouched.

UI (@wordpress/editor):

  • An editor.BlockEdit filter adds an "Optimize" inspector panel to core/image, core/cover, core/media-text, core/post-featured-image, and core/site-logo. (Gallery items are themselves core/image blocks, so they're covered when selected.)
  • The control resolves the block's attachment, optimizes its full-size original, repoints the block at the new attachment, and reports progress/errors via snackbars (spinner driven by isUploadingById).
  • Gated on window.__clientSideMediaProcessing and isClientSideMediaSupported().

The optimized result is uploaded as a new attachment and the block is repointed at it (matching the media-experiments prototype). This reuses the existing mediaUpload/mediaSideload/mediaFinalize settings, so no PHP/REST changes are required.

Testing Instructions

  1. Enable client-side media processing (Preferences → enable, or the feature is on by default in a cross-origin-isolated environment).
  2. Insert an Image block and upload a JPEG/PNG.
  3. In the block inspector, expand the Optimize panel and click Optimize image.
  4. Confirm a spinner shows, a success snackbar appears, and the block now points at a new -optimized attachment that is no larger than the original.
  5. Repeat for a Cover, Media & Text, Featured Image, and Site Logo block.

Automated

  • npm run test:unit packages/upload-media — store unit tests for optimizeExistingItem and fetchRemoteFile.
  • npm run test:e2e -- test/e2e/specs/editor/various/client-side-media-processing.spec.js — "optimizes a previously uploaded image into a new attachment".

Out of scope (follow-ups)

  • Bulk action / Media Library integration to optimize many items (part of Client Side Media iteration for WordPress 7.1 #76756 / media library rebuild).
  • Before/after approval dialog.
  • Format conversion (WebP/AVIF) and original-lineage meta to prevent double-compression on repeated runs.

…ration

Add the store primitives to re-process an already-uploaded attachment
client-side: a public optimizeExistingItem action that enqueues a queue
item which fetches the original file, re-compresses it (same format) via
the existing TranscodeImage operation, uploads the result as a new
attachment, and regenerates sub-sizes.

Introduce the FetchRemoteFile operation (and a fetchFile utility) to
download an existing file by URL, and a FETCH_REMOTE_FILE_ERROR code.
Unsupported or unprocessable formats are skipped via onError, leaving the
original attachment untouched.

Part of #79364.
…images

Register an editor.BlockEdit filter that adds an "Optimize" inspector
panel to image-bearing blocks (image, cover, media-text,
post-featured-image, site-logo). The control resolves the block's
attachment, dispatches optimizeExistingItem against its full-size
original, repoints the block at the new optimized attachment, and reports
progress and errors via snackbars.

Gated on window.__clientSideMediaProcessing and isClientSideMediaSupported()
so it only appears when client-side media processing is active.

Part of #79364.
…e2e flow

Add store unit tests for the optimizeExistingItem enqueue shape, its
unsupported/non-image/duplicate guards, and fetchRemoteFile success and
error paths. Extend the client-side media processing e2e spec with an
'optimize a previously uploaded image' case asserting the block repoints
to a new -optimized attachment that is no larger than the original.

Part of #79364.
@github-actions

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: adamsilverstein <[email protected]>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@github-actions github-actions Bot added the [Package] Editor /packages/editor label Jun 24, 2026
@github-actions

Copy link
Copy Markdown

Size Change: +1.38 kB (+0.02%)

Total Size: 7.51 MB

📦 View Changed
Filename Size Change
build/scripts/editor/index.min.js 476 kB +680 B (+0.14%)
build/scripts/upload-media/index.min.js 14.2 kB +702 B (+5.19%) 🔍

compressed-size-action

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

Labels

[Package] Editor /packages/editor

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant