-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Media: Invalidate entities when new media is uploaded #70405
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
Conversation
Size Change: +114 B (+0.01%) Total Size: 1.85 MB
ℹ️ View Unchanged
|
Oh, there's one other case that I forgot about, which is the media modal in the editor. You can upload via there and it also doesn't invalidate entities. I've pushed a commit to fix this. There's not much to hook into, so I've made it so that whenever the media modal is closed the cache is invalidated. |
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 If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.
To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
(failing e2e tests are from a WordPress core change - convo here) |
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.
These changes make sense to me.
I ran through the test steps in the post and site editors.
Gallery and Image functionality works as I'd expected.
44ca1e7
to
0ec6ebe
Compare
I rebased. An e2e fix was merged overnight. |
originalOnClose?.( ...onCloseArgs ); | ||
}; | ||
return <MediaUpload onClose={ onClose } { ...rest } />; | ||
} | ||
|
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.
Would we ever want to use MediaUpload
without the cache invalidation? Wondering if it could be added directly to the MediaUpload component instead of in this wrapper. Or do we want to avoid media-utils
having a core-data
dependency?
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.
I think the test is that packages that don't depend on a server or WordPress backend like the block editor.
The media utils package README however, says the following:
This package is meant to be used by the WordPress core. It may not work as expected outside WordPress usages.
So might be kosher?
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.
I think I lean toward keeping the cache invalidation happening here within the editor
package as that's where we have the filter to include the MediaUpload
component. It's within the editor context where we expect the invalidation to happen, so arguably it's up to editor
to make sure that's the case, whereas possibly media-utils
shouldn't be too concerned about what's happening outside of itself?
Wondering if it could be added directly to the MediaUpload component instead of in this wrapper.
My understanding is that the goal of the MediaUpload
component is to try to be a pretty thin bridge between a React component (with a common interface for drop-in media libraries) and the core media library, so I'd probably avoid adding too much in the way of block-editor logic to that package.
0ec6ebe
to
7fb9cdf
Compare
Flaky tests detected in 7fb9cdf. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/15722139228
|
* Invalidate media entity when new media is uploaded * Add comment * Also invalidate getMediaItems when closing the media library ---- Co-authored-by: talldan <talldanwp@git.wordpress.org> Co-authored-by: ramonjd <ramonopoly@git.wordpress.org> Co-authored-by: tellthemachines <isabel_brison@git.wordpress.org> Co-authored-by: andrewserong <andrewserong@git.wordpress.org>
What?
Fixes #70397
This is a pragmatic attempt at a fix, in the smallest possible way, but there are still some remaining problems, which I'll document.
All this does is call
receiveEntityRecords
with thetrue
invalidateCache
flag whenever themediaUpload
util creates entities.This
mediaUpload
util is used pretty widely, so this fixes the majority of issues with cache invalidation, but there are still quite a lot of other problems that I've discovered. I can log these as separate issues:Inserter Media
When you upload a new image in the block editor canvas, the image doesn't immediately show up in the inserter, a user still needs to close and reopen the Media category.
I think this is because these 'inserter media categories' bypass
useSelect
and thegetEntitiyRecords
selector, so they're not implemented in a reactive way. They instead call thegetMediaItems
resolver viaresolveSelect
from an async function, so it 'pulls' from the core-data rather than have core-data 'push' to it:gutenberg/packages/editor/src/components/media-categories/index.js
Lines 150 to 152 in 8586e63
gutenberg/packages/editor/src/components/media-categories/index.js
Lines 127 to 139 in 8586e63
Widget Editors
The widget editors don't use the

editor
package, they have their own separate implementation ofmediaUpload
. I haven't addressed it as it seems like low hanging fruit. For a start those editors don't actually implement the Inserter 'Media' tab properly (it always shows 'No results found'), and so it's possible the missing cache invalidation doesn't cause any bugs for users in those editors.Image Block Cropping Tools
If you try cropping/editing an image using the image block's tools, this bypasses the
mediaUpload
function that I'm augmenting in this PR. It has its ownapiFetch
. The implementation has some red flags, like the way it uses a restricted import:gutenberg/packages/block-editor/src/components/image-editor/use-save-image.js
Lines 4 to 6 in 8586e63
It's also not possible to import from core-data in the block editor package, so cache invalidation isn't possible. These tools likely need their WordPress API usage extracted from the block editor package to a block editor setting.
Testing Instructions
Also try repeating the above steps, but instead of dragging images into the canvas: