Skip to content

Plugins: Keep a PluginArea updating after another one unmounts - #87510

Merged
ramonjd merged 4 commits into
trunkfrom
fix/plugin-area-hook-namespace
Nov 27, 2026
Merged

ramonjd merged 4 commits into
trunkfrom
fix/plugin-area-hook-namespace

Conversation

@ramonjd

@ramonjd ramonjd commented Nov 27, 2026 •

Copy link
Copy Markdown
Member

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 PluginArea subscribes to plugin registration under a namespace named after its scope, instead of one fixed namespace shared by every area.

Why?

removeAction removes every callback registered under a namespace. With two PluginArea instances 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 with registerPlugin() never appears in the screen's area.

Core has one PluginArea per 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

  1. Run npm run test:unit -- packages/plugins/src/components/test/plugin-area.jsdom.test.tsx.
  2. Check "keeps rerendering after another PluginArea unmounts" passes.
  3. Put trunk's packages/plugins/src/components/plugin-area/index.tsx back and run it again: that test fails.

`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.
@ramonjd ramonjd added [Type] Bug An existing feature does not function as intended [Package] Plugins /packages/plugins labels Nov 27, 2026
@github-actions

github-actions Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

🤖 PR meta 🤖

Preview in WordPress Playground Preview in Storybook

🎉 Props

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: ramonjd <ramonopoly@git.wordpress.org>
Co-authored-by: Mamaduka <mamaduka@git.wordpress.org>

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


Props Bot commands

You can trigger Props Bot actions by commenting on this PR:

  • @propsbot add <username> to give props to a GitHub user. Requires write access.
  • @propsbot remove <username> to leave a user out of the props. Requires write access.
  • @propsbot refresh to refresh this list. Adding the props-bot label works too.

Updated as activity occurs, without notifying anyone named here. Add the props-bot label to refresh.

📦 Bundle size

Size Change: +1 B (0%)

Total Size: 8.73 MB

📦 View Changed
Filename Size Change
build/scripts/plugins/index.min.js 2.63 kB +1 B (+0.04%)

5dcb13f Run

⚡ Performance

Show the results

Client side metrics exclude the server response time.

front-end-block-theme

Metric 2f2f6b1 trunk % Change
timeToFirstByte 48.75 ms +3.9% -6.77% 46.85 ms +5.34% -2.77% 4.06%
largestContentfulPaint 76 ms +7.89% -2.63% 76 ms +5.26% -5.26% 0%
lcpMinusTtfb 27.05 ms +15.53% -8.13% 26.65 ms +23.83% -7.69% 1.5%
wpBeforeTemplate 24.81 ms +13.62% -3.31% 24.21 ms +9.38% -2.23% 2.48%
wpTemplate 19.07 ms +7.97% -5.3% 18.54 ms +5.07% -4.53% 2.86%
wpTotal 45.07 ms +3.93% -6.94% 43.2 ms +5.97% -2.96% 4.33%
wpMemoryUsage 7.75 MB +0% -0% 7.72 MB +0% -0% 0.45%
wpDbQueries 16 +0% -0% 16 +0% -0% 0%

front-end-classic-theme

Metric 2f2f6b1 trunk % Change
timeToFirstByte 38.2 ms +9.95% -4.06% 39.35 ms +5.46% -4.57% -2.92%
largestContentfulPaint 68 ms +5.88% -0% 68 ms +5.88% -0% 0%
lcpMinusTtfb 30.25 ms +3.31% -3.14% 30.45 ms +6.4% -4.43% -0.66%
wpBeforeTemplate 20.78 ms +9.1% -2.07% 21.17 ms +8.88% -2.08% -1.84%
wpTemplate 13.91 ms +6.04% -4.1% 14.04 ms +9.12% -2.42% -0.93%
wpTotal 35.06 ms +10.38% -3.11% 36.25 ms +6.07% -4.69% -3.28%
wpMemoryUsage 6.44 MB +0% -0% 6.41 MB +0% -0% 0.56%
wpDbQueries 13 +0% -0% 13 +0% -0% 0%

media-processing

Metric 2f2f6b1 trunk % Change
mediaProcessingJpeg 329.96 ms +0.94% -0.11% 329.89 ms +1.59% -0.37% 0.02%
mediaProcessingAvif 4924.17 ms +0.37% -0.36% 4900.55 ms +0.14% -0.25% 0.48%
mediaProcessingJpegToAvif 3415.65 ms +0.38% -0.42% 3402.05 ms +0.14% -0.09% 0.4%

media-upload

Metric 2f2f6b1 trunk % Change
jpegUploadProcessing 1481.38 ms +3.62% -0.81% 1463.91 ms +1.17% -0.48% 1.19%
pngUploadProcessing 243.85 ms +10.55% -3.6% 253.98 ms +2.96% -1.68% -3.99%
largeJpegUploadProcessing 1464.78 ms +1.44% -0.47% 1458.91 ms +0.92% -0.78% 0.4%
multipleImageUploadProcessing 1565.87 ms +0.35% -0.5% 1587.04 ms +2.85% -0.96% -1.33%

post-editor

Metric 2f2f6b1 trunk % Change
serverResponse 273.08 ms +7.24% -8.1% 276.43 ms +3.45% -10.27% -1.21%
firstPaint 328.53 ms +4.43% -4.23% 289.9 ms +4.55% -5.13% 13.33%
domContentLoaded 604.49 ms +3.91% -2.44% 552.61 ms +3.43% -2.2% 9.39%
loaded 605.37 ms +3.91% -2.42% 553.44 ms +3.43% -2.2% 9.38%
firstContentfulPaint 356.3 ms +2.04% -7.77% 311.59 ms +4.99% -2.04% 14.35%
firstBlock 1632.29 ms +2.69% -3.05% 1480.41 ms +2.2% -0.44% 10.26%
type 14.54 ms +5.57% -9.49% 13.86 ms +1.8% -6.93% 4.91%
typeWithoutInspector 14.66 ms +6.34% -12.07% 14.02 ms +5.63% -14.05% 4.56%
typeWithTopToolbar 18.49 ms +4.38% -11.79% 16.58 ms +11.1% -5.49% 11.52%
typeContainer 6.14 ms +7.82% -12.7% 5.74 ms +8.01% -9.76% 6.97%
focus 75.42 ms +6.99% -5.62% 70.98 ms +5.1% -2.16% 6.26%
firstFocus 178.94 ms +0% -0% 161.01 ms +0% -0% 11.14%
selectAll 231.98 ms +2.16% -0.65% 240.16 ms +2.61% -2.75% -3.41%
undo 20.46 ms +18.57% -6.21% 21.16 ms +9.36% -4.11% -3.31%
listViewOpen 25.33 ms +8.13% -1.58% 25.58 ms +14.23% -6.76% -0.98%
inserterOpen 28.03 ms +5.21% -5.89% 25.86 ms +8.7% -3.05% 8.39%
inserterHover 8.34 ms +9.83% -5.76% 8.58 ms +15.73% -8.74% -2.8%
inserterSearch 6.72 ms +4.02% -11.16% 6.69 ms +7.92% -7.03% 0.45%
loadPatterns 434.84 ms +4.49% -7.58% 423.81 ms +4.22% -1.19% 2.6%
wpTotal 265.52 ms +7.54% -8.14% 269.35 ms +3.56% -10.5% -1.42%
wpMemoryUsage 14.81 MB +0% -0% 14.76 MB +0% -0% 0.33%
wpDbQueries 55 +0% -0% 55 +0% -0% 0%

site-editor

Metric 2f2f6b1 trunk % Change
serverResponse 409.23 ms +1.05% -2.25% 396.75 ms +4.56% -4.54% 3.15%
firstPaint 449.47 ms +6.6% -13.28% 461.9 ms +1.7% -3.73% -2.69%
domContentLoaded 831.49 ms +12.21% -0.7% 821.57 ms +0.34% -0.99% 1.21%
loaded 832.57 ms +12.19% -0.72% 822.63 ms +0.34% -1% 1.21%
firstContentfulPaint 471.78 ms +5.87% -3.75% 463.53 ms +1.35% -3.3% 1.78%
firstBlock 2492.14 ms +3.16% -1.09% 2459.96 ms +0.64% -0.42% 1.31%
type 17.32 ms +6.18% -12.18% 17.22 ms +8.94% -10.57% 0.58%
navigate 117.21 ms +1.13% -2.32% 111.91 ms +10.97% -2.15% 4.74%
loadPatterns 1386.07 ms +4.92% -12.36% 1261.76 ms +7.99% -1.64% 9.85%
loadPages 933.85 ms +1.59% -0.79% 939.13 ms +0.85% -1.36% -0.56%
wpTotal 399.84 ms +1.1% -2.46% 387.34 ms +4.73% -4.6% 3.23%
wpMemoryUsage 12.84 MB +0% -0% 12.80 MB +0% -0% 0.28%
wpDbQueries 45 +0% -0% 45 +0% -0% 0%

5dcb13f Run

// 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' );

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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

@ramonjd ramonjd self-assigned this Nov 27, 2026
@ramonjd
ramonjd requested review from Mamaduka and jsnajdr and a balanced review from Copilot November 27, 2026 05:52

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 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 PluginArea instance 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

@Mamaduka

Copy link
Copy Markdown
Member

For example: a screen renders , and a modal on it renders . After the modal closes, a plugin registered with registerPlugin() never appears in the screen's area.

Based on this, should we use a scope as part of the current namespace, instead of generating a "random" one?

@ramonjd
ramonjd marked this pull request as ready for review November 27, 2026 06:23
@ramonjd

ramonjd commented Nov 27, 2026

Copy link
Copy Markdown
Member Author

Based on this, should we use a scope as part of the current namespace, instead of generating a "random" one?

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: core/plugins/plugin-area/an-excellent-scope-3. That might address the randomness question.

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.

@Mamaduka

Copy link
Copy Markdown
Member

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'

@ramonjd

ramonjd commented Nov 27, 2026

Copy link
Copy Markdown
Member Author

Plugin areas can be scoped, so why not use a namespace? This way, the values are predictable.

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.

@Mamaduka Mamaduka 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.

Thanks, @ramonjd!

@Mamaduka

Copy link
Copy Markdown
Member

I should start referring to myself in the third person as well. Looks cool 😄

CleanShot 2026-11-27 at 11 03 29

@ramonjd

ramonjd commented Nov 27, 2026

Copy link
Copy Markdown
Member Author

I should start referring to myself in the third person as well. Looks cool 😄

Prop farming 101 :trollface:

@ramonjd
ramonjd enabled auto-merge (squash) November 27, 2026 07:20
@ramonjd
ramonjd merged commit fd1bc1d into trunk Nov 27, 2026
84 of 85 checks passed
@ramonjd
ramonjd deleted the fix/plugin-area-hook-namespace branch November 27, 2026 07:22
@github-actions github-actions Bot added this to the Gutenberg 24.6 milestone Nov 27, 2026
Vedant-Gandhi pushed a commit to Vedant-Gandhi/gutenberg that referenced this pull request Nov 30, 2026
…ress#87510)

Co-authored-by: ramonjd <ramonopoly@git.wordpress.org>
Co-authored-by: Mamaduka <mamaduka@git.wordpress.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Package] Plugins /packages/plugins [Type] Bug An existing feature does not function as intended

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants