You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 0ebaeb4
Browse filesBrowse the repository at this point in the historyBrowse files
// Each form's data and errors are typed from their respective validators
133
133
```
134
134
135
135
The `action()` helper gets the same type checking. See [Actions & Forms](./pages-routing/actions-and-forms) for the full API.
136
136
137
137
## Typed URLs and Navigation
138
138
139
-
Use `routeUrl()` from `void/routes` to build URLs from your app's page, HTTP, and WebSocket route patterns. For a page at `pages/users/[id].tsx`, `.vue`, or `.svelte`, the pattern is `/users/:id`:
139
+
Use `routeUrl()` from `void/routes` to build URLs from your app's page, HTTP, and WebSocket route patterns. For a page at `pages/users/[id].tsx`, `.vue`, or `.svelte`, the pattern is `/users/[id]`:
Your editor autocompletes route patterns and parameter names. TypeScript checks that the route exists and all required parameters are supplied. Parameter values accept strings or numbers; Void encodes them for the URL. Query keys are chosen by your app, and values can be strings, numbers, booleans, or arrays of those values. Arrays become repeated query parameters; `null` and `undefined` values are omitted.
153
153
154
+
Use bracket patterns with `routeUrl()`, typed `fetch()`, WebSocket `connect()`, page actions, and `useParams()`.
155
+
154
156
Pass the returned string to `Link`, a plain `<a>`, or `c.redirect()`. For programmatic navigation and `c.rewrite()`, you can pass a destination object directly:
Destination objects use the same parameter checking and URL encoding as `routeUrl()`. See [Pages Routing](./pages-routing/overview.md#typed-navigation) for links and router methods, and [Rewrites](../reference/api/rewrites.md#c-rewrite-destination) for server navigation.
168
170
169
-
For a catch-all page such as `pages/files/[...path].tsx`, `.vue`, or `.svelte`, use `/files/:path{.+}` and pass the full path as one value:
171
+
For a catch-all page such as `pages/files/[...path].tsx`, `.vue`, or `.svelte`, use `/files/[...path]` and pass the full path as one value:
Pass a page's URL pattern as the type argument of `useParams()` to type the params it reads. For a page at `pages/users/[id].tsx`, `.vue`, or `.svelte`, the pattern is `/users/:id`:
187
+
Pass a page's URL pattern as the type argument of `useParams()` to type the params it reads. For a page at `pages/users/[id].tsx`, `.vue`, or `.svelte`, the pattern is `/users/[id]`:
179
188
180
189
::: code-group
181
190
182
191
```tsx [React]
183
192
import { useParams } from'@void/react';
184
193
185
-
const { id } =useParams<'/users/:id'>(); // id: string
186
-
useParams<'/users/:id'>().idd; // Type error: no such param
187
-
useParams<'/userz/:id'>(); // Type error: no page matches
194
+
const { id } =useParams<'/users/[id]'>(); // id: string
195
+
useParams<'/users/[id]'>().idd; // Type error: no such param
196
+
useParams<'/userz/[id]'>(); // Type error: no page matches
188
197
```
189
198
190
199
```vue [Vue]
191
200
<script setup lang="ts">
192
201
import { useParams } from '@void/vue';
193
202
194
-
const params = useParams<'/users/:id'>();
203
+
const params = useParams<'/users/[id]'>();
195
204
params.id; // string
196
205
params.idd; // Type error: no such param
197
-
useParams<'/userz/:id'>(); // Type error: no page matches
206
+
useParams<'/userz/[id]'>(); // Type error: no page matches
198
207
</script>
199
208
```
200
209
201
210
```svelte [Svelte]
202
211
<script lang="ts">
203
212
import { useParams } from '@void/svelte';
204
213
205
-
const params = useParams<'/users/:id'>();
214
+
const params = useParams<'/users/[id]'>();
206
215
params.id; // string
207
216
params.idd; // Type error: no such param
208
-
useParams<'/userz/:id'>(); // Type error: no page matches
217
+
useParams<'/userz/[id]'>(); // Type error: no page matches
209
218
</script>
210
219
```
211
220
212
221
```tsx [Solid]
213
222
import { useParams } from'@void/solid';
214
223
215
-
const params =useParams<'/users/:id'>();
224
+
const params =useParams<'/users/[id]'>();
216
225
params.id; // string
217
226
params.idd; // Type error: no such param
218
-
useParams<'/userz/:id'>(); // Type error: no page matches
227
+
useParams<'/userz/[id]'>(); // Type error: no page matches
219
228
```
220
229
221
230
:::
@@ -229,7 +238,7 @@ On the server, `c.req.param('slug')` accepts any name. Read the params object th
<Linkhref={routeUrl('/users/:id', { params: { id } })}>Profile</Link>
332
+
<Linkhref={routeUrl('/users/[id]', { params: { id } })}>Profile</Link>
324
333
<Linkhref="/abuot">About</Link> // Type error: no route matches
325
334
```
326
335
327
336
:::
328
337
329
-
Links can also point to the current page (`?page=2`, `#comments`) and to `http://`, `https://`, `mailto:`, and `tel:` URLs. Relative paths such as `../settings` are rejected; use the absolute path. A fixed part with spaces or non-ASCII characters can be written as is (`/café menu`) or percent-encoded in uppercase, as `routeUrl()` returns it (`/caf%C3%A9%20menu`). A dynamic segment matches any text, including none, so `/users/:id` also accepts `/users/` and `/users/42/edit`. With a root dynamic page such as `pages/[slug].tsx`, `.vue`, or `.svelte`, a protocol-relative URL like `//example.com` passes too.
338
+
Links can also point to the current page (`?page=2`, `#comments`) and to `http://`, `https://`, `mailto:`, and `tel:` URLs. Relative paths such as `../settings` are rejected; use the absolute path. A fixed part with spaces or non-ASCII characters can be written as is (`/café menu`) or percent-encoded in uppercase, as `routeUrl()` returns it (`/caf%C3%A9%20menu`). A dynamic segment matches any text, including none, so `/users/[id]` also accepts `/users/` and `/users/42/edit`. With a root dynamic page such as `pages/[slug].tsx`, `.vue`, or `.svelte`, a protocol-relative URL like `//example.com` passes too.
330
339
331
340
The [SSE](./sse.md#client) and [live stream](./live.md#subscribe-from-the-browser) clients, `connectEventStream()` and `connectLiveStream()`, take one of your routes or an `http://` or `https://` URL for a stream on another origin. They reject same-page, `mailto:`, `tel:`, and `ws://` URLs. Cast a stream URL that TypeScript can't check with `as RouteUrl`; the cast turns off the check for that URL.
332
341
@@ -339,8 +348,8 @@ declare const id: string;
339
348
declareconst dynamic:string;
340
349
341
350
c.redirect('/about');
342
-
c.redirect(routeUrl('/users/:id', { params: { id } }));
343
-
c.redirect({ pathname: '/users/:id', params: { id } });
351
+
c.redirect(routeUrl('/users/[id]', { params: { id } }));
352
+
c.redirect({ pathname: '/users/[id]', params: { id } });
0 commit comments