Skip to content

Commit 1e19fd2

Browse files
committed
Hook transformUrl to the matcher for client side routing.
Move type `transformUrl` definition to BaseRouterProps
1 parent 0c698ed commit 1e19fd2

4 files changed

Lines changed: 13 additions & 5 deletions

File tree

src/routers/Router.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import type { BaseRouterProps } from "./components.jsx";
66
import type { JSX } from "solid-js";
77
import { createBeforeLeave, keepDepth, notifyIfNotBlocked, saveCurrentDepth } from "../lifecycle.js";
88

9-
export type RouterProps = BaseRouterProps & { url?: string, actionBase?: string, explicitLinks?: boolean, preload?: boolean, transformUrl?: (url: string) => string };
9+
export type RouterProps = BaseRouterProps & { url?: string, actionBase?: string, explicitLinks?: boolean, preload?: boolean };
1010

1111
export function Router(props: RouterProps): JSX.Element {
1212
if (isServer) return StaticRouter(props);

src/routers/StaticRouter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ function getPath(url: string) {
77
return u.pathname + u.search;
88
}
99

10-
export type StaticRouterProps = BaseRouterProps & { url?: string, transformUrl?: (url: string) => string };
10+
export type StaticRouterProps = BaseRouterProps & { url?: string };
1111

1212
export function StaticRouter(props: StaticRouterProps): JSX.Element {
1313
let e;

src/routers/components.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export type BaseRouterProps = {
4242
rootLoad?: RouteLoadFunc;
4343
singleFlight?: boolean;
4444
children?: JSX.Element | RouteDefinition | RouteDefinition[];
45+
transformUrl?: (url: string) => string;
4546
};
4647

4748
export const createRouterComponent = (router: RouterIntegration) => (props: BaseRouterProps) => {
@@ -54,7 +55,8 @@ export const createRouterComponent = (router: RouterIntegration) => (props: Base
5455
let context: Owner;
5556
const routerState = createRouterContext(router, branches, () => context, {
5657
base,
57-
singleFlight: props.singleFlight
58+
singleFlight: props.singleFlight,
59+
transformUrl: props.transformUrl,
5860
});
5961
router.create && router.create(routerState);
6062
return (

src/routing.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ export function createRouterContext(
273273
integration: RouterIntegration,
274274
branches: () => Branch[],
275275
getContext?: () => any,
276-
options: { base?: string; singleFlight?: boolean } = {}
276+
options: { base?: string; singleFlight?: boolean, transformUrl?: (url: string) => string } = {}
277277
): RouterContext {
278278
const {
279279
signal: [source, setSource],
@@ -306,7 +306,13 @@ 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));
309+
const matches = createMemo(() => {
310+
if (typeof options.transformUrl === "function") {
311+
return getRouteMatches(branches(), options.transformUrl(location.pathname));
312+
}
313+
314+
return getRouteMatches(branches(), location.pathname);
315+
});
310316

311317
const params = createMemoObject(() => {
312318
const m = matches();

0 commit comments

Comments
 (0)