Repository navigation
Plugins: Keep a PluginArea updating after another one unmounts - #87510
Conversation
`removeAction` removes every callback registered under a namespace, so two mounted `PluginArea` instances sharing the fixed namespaces unsubscribed each other: closing the media editor, which mounts a second instance for its own scope, left the host editor's instance deaf to later `registerPlugin` and `unregisterPlugin` calls until something else re-rendered it. Derive the namespace from the instance id instead, and cover it with a test that unmounts one instance and expects the other to keep updating.
🤖 PR meta 🤖🎉 PropsIf 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. Props Bot commandsYou can trigger Props Bot actions by commenting on this PR:
Updated as activity occurs, without notifying anyone named here. Add the 📦 Bundle sizeSize Change: +1 B (0%) Total Size: 8.73 MB 📦 View Changed
⚡ PerformanceShow the resultsClient side metrics exclude the server response time. front-end-block-theme
front-end-classic-theme
media-processing
media-upload
post-editor
site-editor
|
| // A namespace of this instance's own: `removeAction` removes every | ||
| // callback registered under a namespace, so instances sharing one would | ||
| // unsubscribe each other when one of them unmounts. | ||
| const namespace = useInstanceId( PluginArea, 'core/plugins/plugin-area' ); |
There was a problem hiding this comment.
Returns namespaces like
'core/plugins/plugin-area-0'
'core/plugins/plugin-area-1'
'core/plugins/plugin-area-2'
Every PluginArea listens for plugins being registered or unregistered, so it can re-render when that happens. It does that with addAction, and every instance uses the same two namespaces: and core/plugins/plugin-area/plugins-unregistered.
From what I can see using the same namespace core/plugins/plugin-area/plugins-registered is fine with only one PluginArea on the page, but what if there are two?
removeAction( hookName, namespace ) doesn’t remove your callback, it removes every callback under that namespace. So when one area unmounts, it unsubscribes the other one too.
This is not a huge deal right now.
It came up in a conversation while testing out scenarios for
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues were reported for the change isolating PluginArea subscriptions between instances.
No open issues 🎉
What changed in this PR
Fixes PluginArea subscription cleanup so unmounting one instance does not stop other mounted instances from receiving plugin updates. This supports screens with nested plugin areas, such as the planned media editor modal.
Changes:
- Gives each
PluginAreainstance a unique hook namespace. - Adds a regression test for plugin registration after another instance unmounts.
- Documents the fix in the package changelog.
| File | Description |
|---|---|
| packages/plugins/src/components/test/plugin-area.jsdom.test.tsx | Tests that a remaining instance continues updating after another unmounts. |
| packages/plugins/src/components/plugin-area/index.tsx | Isolates hook subscriptions and cleanup with an instance-specific namespace. |
| packages/plugins/CHANGELOG.md | Records the fix under Unreleased. |
Review effort: Balanced
Based on this, should we use a |
Good question. This PR was pretty naive on my part. Do you mean adding a scope? If I'm not mistaken, two areas with the same scope would still share the same namespace. What about if we added a scope like this: As mentioned, this is a made up problem for now. I'm working on adding extension points for media in #84664 so unsure if it will become a "real thing". Maybe it will. |
|
Plugin areas can be scoped, so why not use a namespace? This way, the values are predictable. const namespace = scope ? `core/plugins/plugin-area/${ scope }` : 'core/plugins/plugin-area' |
See? I knew there was a good reason to add you as a reviewer 😆 This is way simpler. I'll update. Thank you!!! |
Predictable names instead of an instance id, as suggested in review. Areas with different scopes no longer share a namespace, which covers every case in Core; two areas with the same scope already render every plugin twice.
Prop farming 101 |
…ress#87510) Co-authored-by: ramonjd <ramonopoly@git.wordpress.org> Co-authored-by: Mamaduka <mamaduka@git.wordpress.org>

What?
Note
Not sure if this required. It's a hypothetical edge case so far. If it's harmless then maybe we could get it in. Looking for advice.
Each
PluginAreasubscribes to plugin registration under a namespace named after its scope, instead of one fixed namespace shared by every area.Why?
removeActionremoves every callback registered under a namespace. With twoPluginAreainstances mounted, unmounting one removed the other's subscription too, so the one still on screen stopped showing plugins registered or unregistered after that.For example: a screen renders
<PluginArea />, and a modal on it renders<PluginArea scope="my-modal" />. After the modal closes, a plugin registered withregisterPlugin()never appears in the screen's area.Core has one
PluginAreaper screen today, so this only affects code that mounts a second one. The media editor modal in #84664 is the first in Core: it mounts one inside the post editor and unmounts it on close.Testing Instructions
npm run test:unit -- packages/plugins/src/components/test/plugin-area.jsdom.test.tsx.packages/plugins/src/components/plugin-area/index.tsxback and run it again: that test fails.