route-factory
This page shows how to link between remotes without hard-coding URLs. Each remote declares its routes once, and every link to them is checked when you run typecheck.
What you get
Every remote gets a routes.ts with its paths and a links object that builds URLs from them. With event-bus installed, the paths move to the contracts package, so a remote can link to another one without importing it. This feature is off by default.
Add it
What changed:
The @mfeflow/route-factory package is added to every remote.
Use it
Declare the routes. A :name is a path parameter, and the names after ? are search parameters, where a trailing ? makes one optional.
Then build links from any remote. The id is <remote>.<route>.
For a link element, @mfeflow/route-factory/react gives you a typed anchor:
The shell can list what it needs to serve each remote from the same definitions:
Use pattern in your router or in the routes field of mfeflow.app.json.
Configure it
The feature has no install options. What you can change is in the code you write:
A base function is read on every call, so it can return a value from runtime config that loads after the links are created:
With React Router, pass its hook so clicks go through the router:
How it works
defineRoutes pairs a mount path with a table of templates. createLinks looks up <remote>.<route> in those tables, fills the parameters, checks the search names and joins the pieces.
The href method includes base. The path method leaves it out.
That is what a router with its own basename wants.
Without a router, a plain left click on a same-origin link pushes the URL and fires popstate, which the generated shell and any history-listening router pick up. Middle clicks, modifier keys and target="_blank" keep the browser's behaviour. The package has no router dependency; React is an optional peer.
Gotchas
-
A missing parameter, a missing required search parameter, an extra search name and an unknown route all fail typecheck. If you bypass the types, they throw an error naming the route.
-
A mount must start with
/and cannot end with one, so/catalog/is rejected. -
Which layout a remote gets is decided by whether
event-busis installed when the feature is added, so installevent-busfirst, or in the same command.
Reference
- event-bus, which holds the shared paths
- Adding a remote
- mfeflow.app.json