Repository navigation
Description
One of a series of small proposals, in order; the tracking issue lists them. Depends on: RFC 1 (#2).
Summary
Allow export Router = createRouter({ … }). The exported component's signature summarises what the components it was given need and let out, and is marked made. Also allow a made component's properties, such as Router.paths, to be read as values.
Motivation
Upstream PR solidjs#69 (MO-11f) lets .rx call a factory, a JS fn declared to return a Component (for example @solidjs/router 2.0's createRouter({ routes })). The bound name is applied as <Router />. The checker knows which components the call was given only in the module that makes it, so upstream refuses export Router = … as COMPONENT_AS_VALUE. That forces the route table into the same module as the app shell.
Real apps keep the route table in routes.rx and the shell, with its providers and boundaries, in main.rx. @solidjs/router 2.0 also attaches paths, a typed path builder, to the router it returns, and upstream gives no way to read it.
Proposal
MO-11j (An exported factory's component). export Router = createRouter({ … }) exports the made component. Its signature (MO-3, the summary an importer is checked against) is that of a component whose view is <Router /> in the making module. It lists:
- what the given components let out (failures, R-f-5),
- the contexts they need that the factory does not provide itself (MO-12c),
- their content shape (SV-7d).
The made component is exported as the component it is, not as an accessor. made in the printed signature says it is a JS component, so an importer that has only the signature applies it as JSX's <X />, with no children prop. A children: [] prop would otherwise be read by the router as its root.
// routes.rx
import { createRouter } from "./router.js"
import { Home, Person } from "./pages.rx"
export Router = createRouter({ routes: [{ path: "/", component: Home }, { path: "/people/:id", component: Person }] })
export paths = Router.paths
Its printed signature is:
export component Router(props: {}) made fails(Error) uses(./theme.rx#theme) shape(roots h1; closes-p)
// main.rx
import { Router, paths } from "./routes.rx"
import { theme } from "./theme.rx"
export default component App() {
provide theme = "dark"
errored fallback(e) { <p>failed</p> } {
<a href={paths.people("2")}>Bo</a>
<Router />
}
}
Without the errored, Person's failure escapes at the importer's <Router /> (UNHANDLED_FAILURE). Without the provide, Home's need for theme is CONTEXT_NOT_FOUND.
MO-11m (A made component's properties). A factory's declared result may be Component & { paths: any } (a row shape from RFC 1), and it is still a factory's component. In the module that made it, Router.paths is a value that can be read and exported. Its type comes from the library in the projection, so rx-tsc checks a link built from it, and paths.persons is TS2339. The component itself is still only applied: r = Router is COMPONENT_AS_VALUE.
Drawbacks and alternatives
- The summary is computed from the components the call was given in the making module. An importer cannot add routes. That is intended.
madeis one more word in printed signatures. Without it, an importer that has only a.d.rxcannot tell a JS component from an.rxone, and the two are applied differently.- Alternative: export the pages and build the router in the app module. That works on upstream, but it puts the route table in the shell.
Prototype
Branch claude/magical-bell-pfhb1w of yumemi-thomas/reactivescript:
- f722e5a: the export and its summary.
- 2859a03:
made, printed and read inref/src/signature.tsandcompiler/src/signature.rs. - 29924a8: the crate computes the wrapper's summary without a synthetic declaration.
- 95024b2:
Router.pathsas a value. - eeff40d:
Component & { … }as a factory's result in both parsers.
Spec: MO-11j and MO-11m. Scenarios: S-module-10 is accepted, refuses UNHANDLED_FAILURE, CONTEXT_NOT_FOUND, and COMPONENT_AS_VALUE for r = Router, and type-checks paths.people("2"). S-module-6 drops its old refusal of the export. S-module-10's declaration also writes nests(children in component) (RFC 16); the export does not depend on it. examples/router/routes.rx exports its router this way, and the Vite suite runs it on @solidjs/router 2.0. Both implementations (ref/ and the Rust crate) agree.
Activity