Skip to content

Commit 076bd41

Browse files
committed
add transformUrl hook
1 parent 3cd2fef commit 076bd41

2 files changed

Lines changed: 11 additions & 7 deletions

File tree

src/routers/Router.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,17 @@ 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 };
9+
export type RouterProps = BaseRouterProps & { url?: string, actionBase?: string, explicitLinks?: boolean, preload?: boolean, transformUrl?: (url: string) => string };
1010

1111
export function Router(props: RouterProps): JSX.Element {
1212
if (isServer) return StaticRouter(props);
13-
const getSource = () => ({
14-
value: window.location.pathname + window.location.search + window.location.hash,
15-
state: window.history.state
16-
});
13+
const getSource = () => {
14+
const url = window.location.pathname + window.location.search;
15+
return {
16+
value: props.transformUrl ? props.transformUrl(url) + window.location.hash : url + window.location.hash,
17+
state: window.history.state
18+
}
19+
};
1720
const beforeLeave = createBeforeLeave();
1821
return createRouter({
1922
get: getSource,

src/routers/StaticRouter.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@ function getPath(url: string) {
77
return u.pathname + u.search;
88
}
99

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

1212
export function StaticRouter(props: StaticRouterProps): JSX.Element {
1313
let e;
14+
const url = props.url || ((e = getRequestEvent()) && getPath(e.request.url)) || ""
1415
const obj = {
15-
value: props.url || ((e = getRequestEvent()) && getPath(e.request.url)) || ""
16+
value: props.transformUrl ? props.transformUrl(url) : url,
1617
};
1718
return createRouterComponent({
1819
signal: [() => obj, next => Object.assign(obj, next)]

0 commit comments

Comments
 (0)