Skip to content

Widget Dashboard: a second copy of @wordpress/widget-primitives silently disconnects the host seam #82063

Open

Description

@retrofox

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

  1. Build the packages and mount WidgetDashboard in a plain React application (no WordPress) that provides links through WidgetHostProvider, with one widget declaring an action the application's match recognizes.
  2. With @wordpress/widget-primitives resolved once, the matched action renders through the application's link.
  3. Alias @wordpress/widget-primitives to a second copy for @wordpress/widget-dashboard only: 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-dashboard declares @wordpress/widget-primitives as 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

[Feature] DashboardWordPress admin dashboard, widget framework, and layout customization[Package] Widget Dashboard/packages/widget-dashboard[Package] Widget primitives/packages/widget-primitives[Status] In ProgressTracking issues with work in progress[Type] BugAn existing feature does not function as intended

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions