Skip to content

Commit 86c3b1f

Browse files
committed
better handling of query only navigation
1 parent 20ab9aa commit 86c3b1f

File tree

6 files changed

+15
-7
lines changed

6 files changed

+15
-7
lines changed
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+
better handling of query only navigation

src/routers/MemoryRouter.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ export function createMemoryHistory() {
4141
scrollToHash(value.split("#")[1] || "", true);
4242
}
4343
}, 0);
44-
4544
},
4645
back: () => {
4746
go(-1);

src/routers/Router.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export function Router(props: RouterProps): JSX.Element {
1414
const url = window.location.pathname.replace(/^\/+/, "/") + window.location.search;
1515
return {
1616
value: props.transformUrl ? props.transformUrl(url) + window.location.hash : url + window.location.hash,
17+
rawPath: url,
1718
state: window.history.state
1819
}
1920
};

src/routers/StaticRouter.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export function StaticRouter(props: StaticRouterProps): JSX.Element {
1414
const url = props.url || ((e = getRequestEvent()) && getPath(e.request.url)) || ""
1515
const obj = {
1616
value: props.transformUrl ? props.transformUrl(url) : url,
17+
rawPath: url,
1718
};
1819
return createRouterComponent({
1920
signal: [() => obj, next => Object.assign(obj, next)]

src/routing.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ export const useHref = (to: () => string | undefined) => {
7777
export const useNavigate = () => useRouter().navigatorFactory();
7878
export const useLocation = <S = unknown>() => useRouter().location as Location<S>;
7979
export const useIsRouting = () => useRouter().isRouting;
80-
export const usePreloadRoute = () => useRouter().preloadRoute
80+
export const usePreloadRoute = () => useRouter().preloadRoute;
8181

8282
export const useMatch = <S extends string>(path: () => S, matchFilters?: MatchFilters<S>) => {
8383
const location = useLocation();
@@ -103,9 +103,7 @@ export const useSearchParams = <T extends Params>(): [
103103
const location = useLocation();
104104
const navigate = useNavigate();
105105
const setSearchParams = (params: SetParams, options?: Partial<NavigateOptions>) => {
106-
const searchString = untrack(
107-
() => location.pathname + mergeSearchString(location.search, params) + location.hash
108-
);
106+
const searchString = untrack(() => mergeSearchString(location.search, params) + location.hash);
109107
navigate(searchString, {
110108
scroll: false,
111109
resolve: false,
@@ -403,19 +401,22 @@ export function createRouterContext(
403401
return;
404402
}
405403

404+
const queryOnly = to[0] === "?";
406405
const {
407406
replace,
408407
resolve,
409408
scroll,
410409
state: nextState
411410
} = {
412411
replace: false,
413-
resolve: true,
412+
resolve: !queryOnly,
414413
scroll: true,
415414
...options
416415
};
417416

418-
const resolvedTo = resolve ? route.resolvePath(to) : resolvePath("", to);
417+
const resolvedTo = resolve
418+
? route.resolvePath(to)
419+
: resolvePath((queryOnly && source().rawPath) || "", to);
419420

420421
if (resolvedTo === undefined) {
421422
throw new Error(`Path '${to}' is not a routable path`);

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ export interface LocationChange<S = unknown> {
5858
replace?: boolean;
5959
scroll?: boolean;
6060
state?: S;
61+
rawPath?: string;
6162
}
6263
export interface RouterIntegration {
6364
signal: Signal<LocationChange>;

0 commit comments

Comments
 (0)