From 3248bd37956f2aeb95fe5b5f7a0edb3a17804574 Mon Sep 17 00:00:00 2001 From: 0froq Date: Sun, 22 Nov 2026 10:57:44 +0800 Subject: [PATCH] fix(routing): keep a query or hash after a built route URL `RouteUrl` allowed a suffix only after a route path, so adding `?page=2` to a `routeUrl()` result failed the type check. Apply the suffix to a shared base instead, in both the runtime and the generated declaration. --- docs/guide/type-safety.md | 2 +- packages/void/src/codegen/route-types.ts | 3 ++- packages/void/src/runtime/routing/route-url.ts | 5 ++++- .../void/test/integration/route-url-builder.test.ts | 12 ++++++++++++ 4 files changed, 19 insertions(+), 3 deletions(-) diff --git a/docs/guide/type-safety.md b/docs/guide/type-safety.md index 4f3de869e..d9abf30ba 100644 --- a/docs/guide/type-safety.md +++ b/docs/guide/type-safety.md @@ -335,7 +335,7 @@ import { routeUrl } from 'void/routes'; ::: -Links also accept same-page queries and hashes (`?page=2`, `#comments`), and `http://`, `https://`, `mailto:`, and `tel:` URLs. Use absolute paths within your app; relative paths such as `../settings` are rejected. Static path segments accept raw text such as `/café menu` or uppercase percent encoding such as `/caf%C3%A9%20menu`. +Links also accept same-page queries and hashes (`?page=2`, `#comments`), and `http://`, `https://`, `mailto:`, and `tel:` URLs. A `routeUrl()` result takes the same query or hash suffix, so `` `${routeUrl('/users/[id]', { params: { id } })}?tab=activity` `` is checked too. Use absolute paths within your app; relative paths such as `../settings` are rejected. Static path segments accept raw text such as `/café menu` or uppercase percent encoding such as `/caf%C3%A9%20menu`. With `routing.typedRoutes`, URLs handled only by root dynamic routes (`/[slug]`, `/[...slug]`, or `/[...]`) require `routeUrl()`, for example `routeUrl('/[...slug]', { params: { slug: 'dashboard' } })`. Other declared routes still accept checked URL strings. For a custom 404 page, use [`_not-found`](./pages-routing/errors.md#missing-pages). diff --git a/packages/void/src/codegen/route-types.ts b/packages/void/src/codegen/route-types.ts index 7578b4ccf..c7c1511f4 100644 --- a/packages/void/src/codegen/route-types.ts +++ b/packages/void/src/codegen/route-types.ts @@ -188,9 +188,10 @@ export function generateRouteTypes( ' type RoutePathUrl = RoutePatternUrl>;', ); lines.push(' type BuiltRouteUrl = string & { readonly __voidRouteUrl: true };'); + lines.push(' type RouteUrlBase = BuiltRouteUrl | RoutePathUrl;'); lines.push(' type TypedRoutes = RouteTypesConfig extends { typedRoutes: true } ? true : false;'); lines.push(' export type RouteUrl = TypedRoutes extends true'); - lines.push(' ? BuiltRouteUrl | RoutePathUrl | `${RoutePathUrl}${"?" | "#"}${string}`'); + lines.push(' ? RouteUrlBase | `${RouteUrlBase}${"?" | "#"}${string}`'); lines.push(' : string;'); lines.push(' export type Href = TypedRoutes extends true'); lines.push( diff --git a/packages/void/src/runtime/routing/route-url.ts b/packages/void/src/runtime/routing/route-url.ts index 75c631804..40a219f0b 100644 --- a/packages/void/src/runtime/routing/route-url.ts +++ b/packages/void/src/runtime/routing/route-url.ts @@ -112,13 +112,16 @@ type RoutePathUrl = RoutePatternUrl