What happens when a Next.js micro-frontend needs a newer dependency?
The cart team ships on Tuesday. The storefront team upgrades its cart package on Thursday. What happens if Tuesday’s button already needs Thursday’s code? I built next-federation so a Next.js host can load a separately
The cart team ships on Tuesday. The storefront team upgrades its cart package on Thursday. What happens if Tuesday’s button already needs Thursday’s code?
I built next-federation so a Next.js host can load a separately deployed React component at runtime. In the demo, the remote Add to cart button changes the count in the storefront header. Both are reading the same cart context. That shared context is where version compatibility matters. App Router demo · Pages Router demo
Give the remote a version requirement
The example remote lists example-cart-context in shared. Replace that setting in examples/components-remote/vite.config.ts with this:
shared: {
'example-cart-context': { requiredVersion: '^1.0.0' },
},
The example host registers 1.0.0. It passes the actual module along with that version. The range accepts compatible stable 1.x releases, so the teams can update the host package independently. If the remote genuinely needs 1.0.0 exactly, use requiredVersion: '1.0.0'. If it starts using an API added in 1.4.0, set the minimum accordingly with ^1.4.0.
Make the mismatch visible
- Start the repository examples and click Add A1 to cart. Check that the header count changes.
- Stop the examples, change the range above to
^2.0.0, then restart them. The example runner builds the components remote again. Leave the host’s registered version at1.0.0. - Reload the storefront. The host should report
NF-036and show the cart button’s configured error fallback. - Restore
^1.0.0, restart, and verify that the header count updates again.
Run the examples · Shared modules guide
For a date formatter, bundling a private copy with fallback: true might be fine. For cart context, I want the button and header using the same store, so I keep it host-provided.
Before a remote depends on a newer share, I’d check which hosts already provide it and what the others will show. If you try this and the warning leaves you guessing which dependency to fix, I’d like to see the smallest repro. If the project helps you, a star is welcome. GitHub repository
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.