@tanstack/solid-router
Version:
Modern and scalable routing for Solid applications
56 lines • 2.52 kB
JSX
import * as Solid from 'solid-js';
import { isServer } from '@tanstack/router-core/isServer';
import { routerContext } from './routerContext';
import { SafeFragment } from './SafeFragment';
import { Matches } from './Matches';
import { serializeMatchTransfer } from './registryTransfer';
const RouterContext = routerContext;
export function RouterContextProvider({ router, children, ...rest }) {
if (Object.keys(rest).length > 0) {
Solid.runWithOwner(null, () => {
router.update({
...router.options,
...rest,
context: {
...router.options.context,
...rest.context,
},
});
});
}
// Server-side, the provider owns the load dispatch: if the app (or a host
// framework like Start) hasn't already loaded the router, kick `load()`
// and park the render on it. Solid's SSR awaits parked reads natively, so
// blocking-loader semantics are preserved without a top-level
// `await router.load()` in the server entry. The memo exists on both
// environments so the component tree (and hydration keys) stay identical;
// on the client it resolves immediately and the boot is handled by the
// registry priming in the Router constructor plus Transitioner's
// settled-time load.
const ready = Solid.createMemo(() => isServer && !router._serverResult ? router.load().then(() => true) : true);
const OptionalWrapper = router.options.Wrap || SafeFragment;
return (<OptionalWrapper>
<RouterContext value={router}>
<Solid.Show when={ready()}>
{(_) => {
// Native SSR transfer for the bare pairing (no-op under Start —
// see registryTransfer): serialize settled match state into the
// hydration registry while the render's serialization context is
// live. Runs after the load gate so matches are committed. The
// client half — the hydration-claiming boot — runs at router
// creation, outside the render.
if (isServer) {
serializeMatchTransfer(router);
}
return children();
}}
</Solid.Show>
</RouterContext>
</OptionalWrapper>);
}
export function RouterProvider({ router, ...rest }) {
return (<RouterContextProvider router={router} {...rest}>
{() => <Matches />}
</RouterContextProvider>);
}
//# sourceMappingURL=RouterProvider.jsx.map