Skip to content

Commit 8b766a9

Browse files
committed
restore params into root
1 parent d1a8aab commit 8b766a9

File tree

5 files changed

+64
-58
lines changed

5 files changed

+64
-58
lines changed

.changeset/olive-books-push.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@solidjs/router": patch
3+
---
4+
5+
restore params into root

src/routers/components.tsx

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ import type {
3232
RouteSectionProps,
3333
Location
3434
} from "../types.js";
35-
import { createMemoObject } from "../utils.js";
3635

3736
export type BaseRouterProps = {
3837
base?: string;
@@ -53,16 +52,14 @@ export const createRouterComponent = (router: RouterIntegration) => (props: Base
5352

5453
const branches = createMemo(() => createBranches(routeDefs(), props.base || ""));
5554
let context: Owner;
56-
const routerState = createRouterContext(router, () => context, branches, {
55+
const routerState = createRouterContext(router, branches, () => context, {
5756
base,
5857
singleFlight: props.singleFlight
5958
});
60-
const location = routerState.location;
6159
router.create && router.create(routerState);
62-
6360
return (
6461
<RouterContextObj.Provider value={routerState}>
65-
<Root location={location} root={props.root} load={props.rootLoad}>
62+
<Root routerState={routerState} root={props.root} load={props.rootLoad}>
6663
{(context = getOwner()!) && null}
6764
<Routes routerState={routerState} branches={branches()} />
6865
</Root>
@@ -71,19 +68,20 @@ export const createRouterComponent = (router: RouterIntegration) => (props: Base
7168
};
7269

7370
function Root(props: {
74-
location: Location<unknown>;
71+
routerState: RouterContext;
7572
root?: Component<RouteSectionProps>;
7673
load?: RouteLoadFunc;
7774
children: JSX.Element;
7875
}) {
79-
const location = props.location;
76+
const location = props.routerState.location;
77+
const params = props.routerState.params;
8078
const data = createMemo(
81-
() => props.load && untrack(() => props.load!({ params: {}, location, intent: "preload" }))
79+
() => props.load && untrack(() => props.load!({ params, location, intent: "preload" }))
8280
);
8381
return (
8482
<Show when={props.root} keyed fallback={props.children}>
8583
{Root => (
86-
<Root params={{}} location={location} data={data()}>
84+
<Root params={params} location={location} data={data()}>
8785
{props.children}
8886
</Root>
8987
)}
@@ -92,10 +90,6 @@ function Root(props: {
9290
}
9391

9492
function Routes(props: { routerState: RouterContext; branches: Branch[] }) {
95-
const matches = createMemo(() =>
96-
getRouteMatches(props.branches, props.routerState.location.pathname)
97-
);
98-
9993
if (isServer) {
10094
const e = getRequestEvent();
10195
if (e && e.router && e.router.dataOnly) {
@@ -104,27 +98,20 @@ function Routes(props: { routerState: RouterContext; branches: Branch[] }) {
10498
}
10599
e &&
106100
((e.router || (e.router = {})).matches ||
107-
(e.router.matches = matches().map(({ route, path, params }) => ({
101+
(e.router.matches = props.routerState.matches().map(({ route, path, params }) => ({
108102
path: route.originalPath,
109103
pattern: route.pattern,
110104
match: path,
111105
params,
112106
info: route.info
113107
}))));
114108
}
115-
const params = createMemoObject(() => {
116-
const m = matches();
117-
const params: Params = {};
118-
for (let i = 0; i < m.length; i++) {
119-
Object.assign(params, m[i].params);
120-
}
121-
return params;
122-
});
109+
123110
const disposers: (() => void)[] = [];
124111
let root: RouteContext | undefined;
125112

126113
const routeStates = createMemo(
127-
on(matches, (nextMatches, prevMatches, prev: RouteContext[] | undefined) => {
114+
on(props.routerState.matches, (nextMatches, prevMatches, prev: RouteContext[] | undefined) => {
128115
let equal = prevMatches && nextMatches.length === prevMatches.length;
129116
const next: RouteContext[] = [];
130117
for (let i = 0, len = nextMatches.length; i < len; i++) {
@@ -145,8 +132,7 @@ function Routes(props: { routerState: RouterContext; branches: Branch[] }) {
145132
props.routerState,
146133
next[i - 1] || props.routerState.base,
147134
createOutlet(() => routeStates()[i + 1]),
148-
() => matches()[i],
149-
params
135+
() => props.routerState.matches()[i]
150136
);
151137
});
152138
}

src/routing.ts

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { JSX, Accessor, getOwner, runWithOwner } from "solid-js";
1+
import { JSX, Accessor, runWithOwner } from "solid-js";
22
import {
33
createComponent,
44
createContext,
@@ -91,7 +91,7 @@ export const useMatch = <S extends string>(path: () => S, matchFilters?: MatchFi
9191
});
9292
};
9393

94-
export const useParams = <T extends Params>() => useRoute().params as T;
94+
export const useParams = <T extends Params>() => useRouter().params as T;
9595

9696
export const useSearchParams = <T extends Params>(): [
9797
Partial<T>,
@@ -271,8 +271,8 @@ export function getIntent() {
271271

272272
export function createRouterContext(
273273
integration: RouterIntegration,
274+
branches: () => Branch[],
274275
getContext?: () => any,
275-
getBranches?: () => Branch[],
276276
options: { base?: string; singleFlight?: boolean } = {}
277277
): RouterContext {
278278
const {
@@ -306,9 +306,19 @@ export function createRouterContext(
306306
const referrers: LocationChange[] = [];
307307
const submissions = createSignal<Submission<any, any>[]>(isServer ? initFromFlash() : []);
308308

309+
const matches = createMemo(() => getRouteMatches(branches(), location.pathname));
310+
311+
const params = createMemoObject(() => {
312+
const m = matches();
313+
const params: Params = {};
314+
for (let i = 0; i < m.length; i++) {
315+
Object.assign(params, m[i].params);
316+
}
317+
return params;
318+
});
319+
309320
const baseRoute: RouteContext = {
310321
pattern: basePath,
311-
params: {},
312322
path: () => basePath,
313323
outlet: () => null,
314324
resolvePath(to: string) {
@@ -337,10 +347,12 @@ export function createRouterContext(
337347
return {
338348
base: baseRoute,
339349
location,
350+
params,
340351
isRouting,
341352
renderPath,
342353
parsePath,
343354
navigatorFactory,
355+
matches,
344356
beforeLeave,
345357
preloadRoute,
346358
singleFlight: options.singleFlight === undefined ? true : options.singleFlight,
@@ -436,7 +448,7 @@ export function createRouterContext(
436448
}
437449

438450
function preloadRoute(url: URL, preloadData: boolean) {
439-
const matches = getRouteMatches(getBranches!(), url.pathname);
451+
const matches = getRouteMatches(branches(), url.pathname);
440452
const prevIntent = intent;
441453
intent = "preload";
442454
for (let match in matches) {
@@ -478,9 +490,8 @@ export function createRouteContext(
478490
parent: RouteContext,
479491
outlet: () => JSX.Element,
480492
match: () => RouteMatch,
481-
params: Params
482493
): RouteContext {
483-
const { base, location } = router;
494+
const { base, location, params } = router;
484495
const { pattern, component, load } = match().route;
485496
const path = createMemo(() => match().path);
486497

@@ -493,7 +504,6 @@ export function createRouteContext(
493504
parent,
494505
pattern,
495506
path,
496-
params,
497507
outlet: () =>
498508
component
499509
? createComponent(component, {

src/types.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,6 @@ export interface RouteContext {
145145
parent?: RouteContext;
146146
child?: RouteContext;
147147
pattern: string;
148-
params: Params;
149148
path: () => string;
150149
outlet: () => JSX.Element;
151150
resolvePath(to: string): string | undefined;
@@ -161,8 +160,10 @@ export interface RouterUtils {
161160
export interface RouterContext {
162161
base: RouteContext;
163162
location: Location;
163+
params: Params;
164164
navigatorFactory: NavigatorFactory;
165165
isRouting: () => boolean;
166+
matches: () => RouteMatch[];
166167
renderPath(path: string): string;
167168
parsePath(str: string): string;
168169
beforeLeave: BeforeLeaveLifecycle;

0 commit comments

Comments
 (0)