-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Client-side media processing: only use media upload provider at the top level of the block editor provider #76124
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
andrewserong
merged 3 commits into
trunk
from
fix/client-side-media-processing-only-use-media-upload-provider-when-not-in-preview-mode
Mar 5, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -146,16 +146,34 @@ export const ExperimentalBlockEditorProvider = withRegistryProvider( | |
| const isClientSideMediaEnabled = | ||
| shouldEnableClientSideMediaProcessing(); | ||
|
|
||
| // Nested providers (e.g. from useBlockPreview) inherit settings | ||
| // where mediaUpload has already been replaced with the | ||
| // interceptor. Detect this so we skip the replacement and | ||
| // MediaUploadProvider for them — see the longer comment below. | ||
| const isMediaUploadIntercepted = | ||
| !! _settings?.mediaUpload?.__isMediaUploadInterceptor; | ||
|
|
||
| const settings = useMemo( () => { | ||
| if ( isClientSideMediaEnabled && _settings?.mediaUpload ) { | ||
| if ( | ||
| isClientSideMediaEnabled && | ||
| _settings?.mediaUpload && | ||
| ! isMediaUploadIntercepted | ||
| ) { | ||
| // Create a new object so that the original props.settings.mediaUpload is not modified. | ||
| const interceptor = mediaUpload.bind( null, registry ); | ||
| interceptor.__isMediaUploadInterceptor = true; | ||
| return { | ||
| ..._settings, | ||
| mediaUpload: mediaUpload.bind( null, registry ), | ||
| mediaUpload: interceptor, | ||
| }; | ||
| } | ||
| return _settings; | ||
| }, [ _settings, registry, isClientSideMediaEnabled ] ); | ||
| }, [ | ||
| _settings, | ||
| registry, | ||
| isClientSideMediaEnabled, | ||
| isMediaUploadIntercepted, | ||
| ] ); | ||
|
|
||
| const { __experimentalUpdateSettings } = unlock( | ||
| useDispatch( blockEditorStore ) | ||
|
|
@@ -215,7 +233,21 @@ export const ExperimentalBlockEditorProvider = withRegistryProvider( | |
| </SelectionContext.Provider> | ||
| ); | ||
|
|
||
| if ( isClientSideMediaEnabled ) { | ||
| // MediaUploadProvider writes the mediaUpload function from | ||
| // _settings into the shared upload-media store so the store can | ||
| // hand files off to the server. useMediaUploadSettings extracts | ||
| // mediaUpload from the original _settings prop — *before* the | ||
| // interceptor replacement above — so the store receives the | ||
| // real server-side upload function. | ||
| // | ||
| // Only the first (outermost) provider should do this. | ||
| // Nested providers (e.g. from useBlockPreview in | ||
| // core/post-template) inherit settings that already contain | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, so would not being able to upload images on a "page" post type be due to the template it's using (in TT5)? |
||
| // the interceptor, so their MediaUploadProvider would | ||
| // overwrite the store's server-side function with the | ||
| // interceptor, causing uploads to loop instead of reaching | ||
| // the server. | ||
| if ( isClientSideMediaEnabled && ! isMediaUploadIntercepted ) { | ||
| return ( | ||
| <MediaUploadProvider | ||
| settings={ mediaUploadSettings } | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Part of the existing condition here is
settings.mediaUploadneeds to be defined for client side media processing to work. I found that curious, is this because client side media processing eventually calls through to the vanilla server version ofmediaUpload?I think an alternative fix might be to make
mediaUploadundefinedforBlockPreview. Users shouldn't be uploading media in previews anyway. That would avoid the need for extraisPreviewchecks (though would still need to do something to avoid rendering the provider, it could work the same).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here's where it could be implemented -
gutenberg/packages/block-editor/src/components/block-preview/index.js
Lines 59 to 66 in d37cda8
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a drive-by comment before I close my laptop for the day: setting it to
undefinedforBlockPreviewresults in the function not being available withinupload-mediaand throws an error (as in this context, it'll wind up updating the shared settings used across the media upload provider). (I tried that approach first, but didn't get very far — I do like the idea of trying to simplify things, though 🤔 )I think a separate fix would be to untangle the re-use of the same key (i.e. call the "real" upload function something different within
upload-media), but that's a bit beyond the scope of the quick fix.It's a little convoluted this!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ideally none of the
upload-mediacode executes for block previews at all, so then there would never be an opportunity to throw an error. 😄There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point! 😄