Skip to content

Commit

Permalink
Add query and param wrappers to support non-Proxy envs
Browse files Browse the repository at this point in the history
  • Loading branch information
ryansolid committed Oct 9, 2024
1 parent e09f638 commit a3a36fb
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/friendly-cars-hide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@solidjs/router": patch
---

Add query and param wrappers to support non-Proxy envs
25 changes: 19 additions & 6 deletions src/routing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,11 @@ export function getRouteMatches(branches: Branch[], location: string): RouteMatc
return [];
}

export function createLocation(path: Accessor<string>, state: Accessor<any>): Location {
function createLocation(
path: Accessor<string>,
state: Accessor<any>,
queryWrapper?: (getQuery: () => Params) => Params
): Location {
const origin = new URL(mockBase);
const url = createMemo<URL>(
prev => {
Expand All @@ -244,6 +248,7 @@ export function createLocation(path: Accessor<string>, state: Accessor<any>): Lo
const search = createMemo(() => url().search, true);
const hash = createMemo(() => url().hash);
const key = () => "";
const queryFn = on(search, () => extractSearchParams(url())) as () => Params;

return {
get pathname() {
Expand All @@ -261,7 +266,7 @@ export function createLocation(path: Accessor<string>, state: Accessor<any>): Lo
get key() {
return key();
},
query: createMemoObject(on(search, () => extractSearchParams(url())) as () => Params)
query: queryWrapper ? queryWrapper(queryFn) : createMemoObject(queryFn)
};
}

Expand All @@ -281,7 +286,11 @@ export function createRouterContext(
integration: RouterIntegration,
branches: () => Branch[],
getContext?: () => any,
options: { base?: string; singleFlight?: boolean; transformUrl?: (url: string) => string } = {}
options: {
base?: string;
singleFlight?: boolean;
transformUrl?: (url: string) => string;
} = {}
): RouterContext {
const {
signal: [source, setSource],
Expand Down Expand Up @@ -335,7 +344,7 @@ export function createRouterContext(
};
const [reference, setReference] = createSignal(source().value);
const [state, setState] = createSignal(source().state);
const location = createLocation(reference, state);
const location = createLocation(reference, state, utils.queryWrapper);
const referrers: LocationChange[] = [];
const submissions = createSignal<Submission<any, any>[]>(isServer ? initFromFlash() : []);

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

const params = createMemoObject(() => {
const buildParams = () => {
const m = matches();
const params: Params = {};
for (let i = 0; i < m.length; i++) {
Object.assign(params, m[i].params);
}
return params;
});
};

const params = utils.paramsWrapper
? utils.paramsWrapper(buildParams, branches)
: createMemoObject(buildParams);

const baseRoute: RouteContext = {
pattern: basePath,
Expand Down
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ export interface RouterUtils {
parsePath(str: string): string;
go(delta: number): void;
beforeLeave: BeforeLeaveLifecycle;
paramsWrapper: (getParams: () => Params, branches: () => Branch[]) => Params;
queryWrapper: (getQuery: () => Params) => Params;
}

export interface RouterContext {
Expand Down

0 comments on commit a3a36fb

Please sign in to comment.