Skip to content

PoC: wait for Module Federation remotes before the initial scan and implement replace option - #1291

Merged
thet merged 2 commits into
masterfrom
registry-await-module-federation
Sep 11, 2026
Merged

PoC: wait for Module Federation remotes before the initial scan and implement replace option#1291
thet merged 2 commits into
masterfrom
registry-await-module-federation

Conversation

@petschki

Copy link
Copy Markdown
Member

Module Federation remote bundles (add-ons) register their patterns and components asynchronously after document ready, while the registry scanned the DOM right at document ready. Whether an add-on's pattern override or component registration was in place for the initial scan was a race between the remote's chunk loading and the host's own initialization — usually won by the remote, but not guaranteed.

registry.init() now awaits window.__patternslib_mf_initialized, the promise provided by the Module Federation helper of @patternslib/dev, before the initial scan. Without a Module Federation host on the page nothing changes. A timeout (default 5000ms, window.__patternslib_mf_init_timeout) makes sure a broken or slow remote cannot block the page; the scan then runs anyway with a warning. Repeated init() calls while waiting are ignored.

…itial scan

Module Federation remote bundles (add-ons) register their patterns and
components asynchronously after document ready, while the registry
scanned the DOM right at document ready. Whether an add-on's pattern
override or component registration was in place for the initial scan
was a race between the remote's chunk loading and the host's own
initialization — usually won by the remote, but not guaranteed.

``registry.init()`` now awaits ``window.__patternslib_mf_initialized``,
the promise provided by the Module Federation helper of
@patternslib/dev, before the initial scan. Without a Module Federation
host on the page nothing changes. A timeout (default 5000ms,
``window.__patternslib_mf_init_timeout``) makes sure a broken or slow
remote cannot block the page; the scan then runs anyway with a
warning. Repeated ``init()`` calls while waiting are ignored.
@petschki

Copy link
Copy Markdown
Member Author

see analysis and usecase here plone/mockup#1637

… pattern

The first registration of a pattern name wins, so an add-on bundle
could not override a core pattern under its own name — it had to
blacklist the original and register a replacement under a different
name with the original trigger, and then bridge the original's
options by hand.

``registry.register(pattern, name, { replace: true })`` now replaces an
existing registration; ``Base.extend`` accepts ``replace: true`` as
pattern property. Together with the registry waiting for Module
Federation remotes before the initial scan, the replacement is in
place for the initial scan no matter whether the remote or the core
bundle registered first. Replacing after the registry was initialized
logs a warning: already initialized elements keep the previous
pattern, only new elements get the replacement. The blacklist still
wins over a replacement.

Also documents the new Module Federation globals in the README.
@petschki petschki changed the title PoC: wait for Module Federation remotes before the initial scan PoC: wait for Module Federation remotes before the initial scan and implement replace option Sep 10, 2026

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

Also looks very good to me and solves a registration problem very elegantly.

Up until now, a pattern from an add-on remote bundle called registry.register to register the pattern and re-run a dom scan when the scan was already done. Now - with everything in place (also for the enty point's export default import("./bundle"); trick - undocumented yet, but there is not much documentation anyways), everything is done only once and there is a better way to override existing patterns.

Sounds like a big win for me and a more stable overriding situation.

Approved!

@thet
thet marked this pull request as ready for review September 11, 2026 13:09
@thet
thet merged commit 3aed929 into master Sep 11, 2026
1 check passed
@thet
thet deleted the registry-await-module-federation branch September 11, 2026 13:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants