Skip to content

Commit a3a36fb

Browse files
committed
Add query and param wrappers to support non-Proxy envs
1 parent e09f638 commit a3a36fb

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed

.changeset/friendly-cars-hide.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+
Add query and param wrappers to support non-Proxy envs

src/routing.ts

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,11 @@ export function getRouteMatches(branches: Branch[], location: string): RouteMatc
222222
return [];
223223
}
224224

225-
export function createLocation(path: Accessor<string>, state: Accessor<any>): Location {
225+
function createLocation(
226+
path: Accessor<string>,
227+
state: Accessor<any>,
228+
queryWrapper?: (getQuery: () => Params) => Params
229+
): Location {
226230
const origin = new URL(mockBase);
227231
const url = createMemo<URL>(
228232
prev => {
@@ -244,6 +248,7 @@ export function createLocation(path: Accessor<string>, state: Accessor<any>): Lo
244248
const search = createMemo(() => url().search, true);
245249
const hash = createMemo(() => url().hash);
246250
const key = () => "";
251+
const queryFn = on(search, () => extractSearchParams(url())) as () => Params;
247252

248253
return {
249254
get pathname() {
@@ -261,7 +266,7 @@ export function createLocation(path: Accessor<string>, state: Accessor<any>): Lo
261266
get key() {
262267
return key();
263268
},
264-
query: createMemoObject(on(search, () => extractSearchParams(url())) as () => Params)
269+
query: queryWrapper ? queryWrapper(queryFn) : createMemoObject(queryFn)
265270
};
266271
}
267272

@@ -281,7 +286,11 @@ export function createRouterContext(
281286
integration: RouterIntegration,
282287
branches: () => Branch[],
283288
getContext?: () => any,
284-
options: { base?: string; singleFlight?: boolean; transformUrl?: (url: string) => string } = {}
289+
options: {
290+
base?: string;
291+
singleFlight?: boolean;
292+
transformUrl?: (url: string) => string;
293+
} = {}
285294
): RouterContext {
286295
const {
287296
signal: [source, setSource],
@@ -335,7 +344,7 @@ export function createRouterContext(
335344
};
336345
const [reference, setReference] = createSignal(source().value);
337346
const [state, setState] = createSignal(source().state);
338-
const location = createLocation(reference, state);
347+
const location = createLocation(reference, state, utils.queryWrapper);
339348
const referrers: LocationChange[] = [];
340349
const submissions = createSignal<Submission<any, any>[]>(isServer ? initFromFlash() : []);
341350

@@ -347,14 +356,18 @@ export function createRouterContext(
347356
return getRouteMatches(branches(), location.pathname);
348357
});
349358

350-
const params = createMemoObject(() => {
359+
const buildParams = () => {
351360
const m = matches();
352361
const params: Params = {};
353362
for (let i = 0; i < m.length; i++) {
354363
Object.assign(params, m[i].params);
355364
}
356365
return params;
357-
});
366+
};
367+
368+
const params = utils.paramsWrapper
369+
? utils.paramsWrapper(buildParams, branches)
370+
: createMemoObject(buildParams);
358371

359372
const baseRoute: RouteContext = {
360373
pattern: basePath,

src/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,8 @@ export interface RouterUtils {
158158
parsePath(str: string): string;
159159
go(delta: number): void;
160160
beforeLeave: BeforeLeaveLifecycle;
161+
paramsWrapper: (getParams: () => Params, branches: () => Branch[]) => Params;
162+
queryWrapper: (getQuery: () => Params) => Params;
161163
}
162164

163165
export interface RouterContext {

0 commit comments

Comments
 (0)