Repository navigation
Description
Problem
WidgetHostProvider / useWidgetHost (#81740) share one React context object, created when @wordpress/widget-primitives evaluates (packages/widget-primitives/src/widget-host/widget-host.tsx). The application mounts the provider; @wordpress/widget-dashboard reads the context in the actions menu and the footer.
When the application and @wordpress/widget-dashboard resolve two copies of @wordpress/widget-primitives, each copy creates its own context object. The provider writes into one, the consumers read the other, and useWidgetHost() returns the default {}: every capability counts as absent. For links that means every action link falls back to a plain anchor and a full page load, with no error or warning in development or production.
On a WordPress page the package loads as a script module through the import map, so every module built with @wordpress/build shares one instance: built widgets keep their @wordpress/* specifiers and declare them as module dependencies. A widget bundled with another tool that inlines the package hits the same failure.
It can also happen for npm consumers that bundle @wordpress/widget-dashboard and end up with two copies, for example when version ranges place a second copy under node_modules/@wordpress/widget-dashboard/node_modules/. @wordpress/widget-dashboard declares @wordpress/widget-primitives as a regular dependency, and neither the package metadata nor the docs state the single-instance requirement.
Reproduction
- Build the packages and mount
WidgetDashboardin a plain React application (no WordPress) that provideslinksthroughWidgetHostProvider, with one widget declaring an action the application'smatchrecognizes. - With
@wordpress/widget-primitivesresolved once, the matched action renders through the application's link. - Alias
@wordpress/widget-primitivesto a second copy for@wordpress/widget-dashboardonly: the same action renders a plain anchor, silently.
Fix
Treat @wordpress/widget-primitives as a shared contract package that must be a singleton in the module graph, the way react is.
-
@wordpress/widget-dashboarddeclares@wordpress/widget-primitivesas a peer dependency (plus a dev dependency for its own development), with a range the application's copy satisfies. - Both READMEs state the single-instance requirement, for npm consumers and for widget authors bundling with other tools.
- A test that mounts the provider from one instance of the package and the consumers from another, pinning the degraded outcome; the single-copy case stays covered by the existing tests.
- Optional: a development-only warning when a second instance of the package evaluates, so the failure stops being silent.
References
- Dashboard Widgets: let the host decide which component renders an action link #81740, where the seam landed. The failure mode was raised in review: Dashboard Widgets: let the host decide which component renders an action link #81740 (comment)
- The follow-up agreed there, tracked by this issue: Dashboard Widgets: let the host decide which component renders an action link #81740 (comment)
- Raised again after the merge, asking that the contract land with the seam: Dashboard Widgets: let the host decide which component renders an action link #81740 (comment)
Activity