Skip to content

Commit 9dfffe9

Browse files
fix: omit the search key from schema-less server route args (#615)
serverRouteArgs always returned { params, search } with search undefined when the route declared no schema. Arguments travel as plain JSON by default and the runtime's JSON-safety check rejects explicit undefined, so every schema-less server route threw on client navigation before any request was made (SSR calls the function directly, so first loads worked — a client-nav-only break). Leave the key out instead: server-side destructuring reads identically, the args-memo equality already treats absent and undefined alike, and ServerRouteArgs now types search as optional exactly when no schema narrows it (still required when one does). Co-authored-by: Cursor <[email protected]>
1 parent 675ec74 commit 9dfffe9

4 files changed

Lines changed: 61 additions & 7 deletions

File tree

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+
Server route args omit the `search` key when the route declares no search schema (#615). The explicit `search: undefined` failed the runtime's JSON-safety check on client navigation (arguments travel as plain JSON by default), so every schema-less server route rendered its error boundary instead of the page — SSR was unaffected, making the break client-nav-only. Destructuring on the server reads identically, and `ServerRouteArgs` now types `search` as optional exactly when no schema narrows it.

‎src/serverRouteShared.ts‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,19 @@ export function serverRouteArgs(
5252
query: SearchParams
5353
): ServerRouteArgs<Params, unknown> {
5454
const schema = (route.key as RouteDefinition | undefined)?.search;
55-
let search: unknown;
5655
if (schema) {
5756
const raw = { ...query };
5857
const outcome = validateSearch(schema, raw);
5958
// Issues leave the raw values in place, matching useSearchParams: search
6059
// strings are user input, so defaults belong in the schema itself.
61-
search = outcome.issues ? raw : outcome.value;
60+
return { params: { ...params }, search: outcome.issues ? raw : outcome.value };
6261
}
63-
return { params: { ...params }, search };
62+
// No schema: leave the key out rather than carrying `search: undefined` —
63+
// the args are sent as plain JSON by default and the runtime's JSON-safety
64+
// check rejects explicit `undefined` (#615). Destructuring on the server
65+
// reads the same either way, and the args-memo equality treats an absent
66+
// key and undefined alike.
67+
return { params: { ...params } };
6468
}
6569

6670
const shallowEqual = (a: any, b: any): boolean =>

‎src/types.ts‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -179,12 +179,20 @@ export type RouteSectionComponent<T = unknown, P extends Params = Params> =
179179
/**
180180
* What a server component route is called with (experimental): the params
181181
* the route's pattern (and its ancestors') declares, plus — only when the
182-
* route declares a `search` schema — that schema's validated output.
182+
* route declares a `search` schema — that schema's validated output. Without
183+
* a schema the `search` key is ABSENT, not `undefined`: the args travel as
184+
* plain JSON by default, and the runtime's JSON-safety check rejects
185+
* explicit `undefined` (#615). The type mirrors that — optional exactly when
186+
* the search type admits undefined, required when a schema narrows it.
183187
*/
184-
export interface ServerRouteArgs<P extends Params | TypedRouteConfig = Params, S = undefined> {
188+
export type ServerRouteArgs<P extends Params | TypedRouteConfig = Params, S = undefined> = {
185189
params: ServerRouteParams<P>;
186-
search: P extends TypedRouteConfig ? SearchOutputOf<P> : S;
187-
}
190+
} & (undefined extends ServerRouteSearch<P, S>
191+
? { search?: ServerRouteSearch<P, S> }
192+
: { search: ServerRouteSearch<P, S> });
193+
194+
/** The search half of {@link ServerRouteArgs}: schema output when declared. */
195+
export type ServerRouteSearch<P, S> = P extends TypedRouteConfig ? SearchOutputOf<P> : S;
188196

189197
/**
190198
* `ServerRouteArgs<typeof route>`: a `defineFileRoute` config is a witness —

‎test/server-routes.spec.tsx‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,43 @@ describe("server component routes", () => {
212212
cleanup();
213213
});
214214

215+
test("a route without a search schema omits the `search` key entirely (#615)", async () => {
216+
// Args travel as plain JSON by default and the runtime's JSON-safety
217+
// check rejects explicit `undefined` anywhere in them — `toEqual` can't
218+
// see the difference, so assert key presence directly.
219+
const calls: any[] = [];
220+
const page = serverFunction(async (args: ServerRouteArgs) => {
221+
calls.push(args);
222+
return () => <p>page</p>;
223+
});
224+
let navigate!: Navigator;
225+
const Router = createRouter({
226+
routes: [
227+
defineRoute({ path: "/page", component: serverRouteComponent(query(page, "no-schema")) }),
228+
defineRoute({
229+
path: "/",
230+
component: () => {
231+
navigate = useNavigate();
232+
return <p>home</p>;
233+
}
234+
})
235+
],
236+
history: memoryHistory("/")
237+
});
238+
239+
const { div, cleanup } = mount(() => (
240+
<Router>{props => <Loading fallback="loading">{props.children}</Loading>}</Router>
241+
));
242+
await settle();
243+
navigate("/page");
244+
await settle(20);
245+
expect(calls).toHaveLength(1);
246+
expect("search" in calls[0]).toBe(false);
247+
expect(Object.keys(calls[0])).toEqual(["params"]);
248+
expect(div.textContent).toContain("page");
249+
cleanup();
250+
});
251+
215252
test("hover preload warms the same call the navigation reads", async () => {
216253
const fn = vi.fn(async ({ params }: ServerRouteArgs<{ id: string }>) => {
217254
return () => <article data-story={params.id} />;

0 commit comments

Comments
 (0)