Skip to content

Commit a294630

Browse files
Merge branch 'main' into renovate/globals-17.x-lockfile
2 parents 9c68ef3 + b8c70a9 commit a294630

20 files changed

Lines changed: 1219 additions & 99 deletions

src/routes/solid-start/(0)building-your-application/(3)data-fetching.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
---
22
title: "Data fetching"
3+
version: "1.0"
34
---
45

56
Fetching data from a remote API or database is a core task for most applications.

src/routes/solid-start/(0)building-your-application/(4)data-mutation.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
---
22
title: "Data mutation"
3+
version: "1.0"
34
---
45

56
Mutating data on a server is a common task in most applications.

src/routes/solid-start/(2)migrating-from-v1.mdx

Lines changed: 0 additions & 98 deletions
This file was deleted.
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
---
2+
title: Routing
3+
use_cases: >-
4+
page navigation, url structure, dynamic paths, route organization,
5+
filesystem routing, api endpoints
6+
tags:
7+
- routing
8+
- filesystem
9+
- pages
10+
- dynamic
11+
- api
12+
version: "2.0"
13+
description: >-
14+
SolidStart v2 routing uses the filesystem router to map files in
15+
src/routes to UI routes and API routes.
16+
---
17+
18+
Routes come from the filesystem.
19+
`FileRoutes` from `@solidjs/start/router` maps files in `src/routes` into route paths.
20+
21+
This page covers the route filename conventions.
22+
23+
## UI routes and API routes
24+
25+
There are two route shapes:
26+
27+
- Files with a default export become UI routes.
28+
- Files that export HTTP method names such as `GET` or `POST` become API routes.
29+
30+
You can read more about handler exports in [API routes](/v2/solid-start/building-your-application/api-routes).
31+
32+
## Basic filename mapping
33+
34+
File names map to paths using these rules:
35+
36+
- `src/routes/index.tsx` becomes `/`
37+
- `src/routes/about.tsx` becomes `/about`
38+
- `src/routes/blog/index.tsx` becomes `/blog`
39+
- `src/routes/blog/post.tsx` becomes `/blog/post`
40+
41+
An `.mdx` file in `src/routes` is also treated as a page route.
42+
43+
## Dynamic segments
44+
45+
Bracketed segments become route params:
46+
47+
- `src/routes/users/[id].tsx` becomes `/users/:id`
48+
- `src/routes/users/[[id]].tsx` becomes `/users/:id?`
49+
- `src/routes/docs/[...slug].tsx` becomes `/docs/*slug`
50+
51+
Read them with [`useParams`](/solid-router/reference/primitives/use-params):
52+
53+
```tsx title="src/routes/users/[id].tsx"
54+
import { useParams } from "@solidjs/router";
55+
56+
export default function UserPage() {
57+
const params = useParams();
58+
return <h1>User {params.id}</h1>;
59+
}
60+
```
61+
62+
## Route config exports
63+
64+
The filesystem router also looks for an exported `route` object alongside a page component.
65+
That lets a route file attach route-level behavior while still default-exporting UI.
66+
67+
```tsx title="src/routes/posts/[id].tsx"
68+
import { query, createAsync, type RouteDefinition } from "@solidjs/router";
69+
70+
const getPost = query(async (id: string) => {
71+
"use server";
72+
const response = await fetch(`https://example.com/api/posts/${id}`);
73+
return response.json();
74+
}, "post");
75+
76+
export const route = {
77+
preload: ({ params }) => getPost(params.id),
78+
} satisfies RouteDefinition;
79+
80+
export default function PostPage(props: { params: { id: string } }) {
81+
const post = createAsync(() => getPost(props.params.id));
82+
return <pre>{JSON.stringify(post(), null, 2)}</pre>;
83+
}
84+
```
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
---
2+
title: API routes
3+
use_cases: >-
4+
rest api, webhooks, oauth callbacks, server endpoints, route handlers,
5+
request handling
6+
tags:
7+
- api
8+
- http
9+
- server
10+
- handlers
11+
- endpoints
12+
version: "2.0"
13+
description: >-
14+
SolidStart v2 API routes are filesystem routes that export HTTP method
15+
handlers and receive typed APIEvent objects from @solidjs/start/server.
16+
---
17+
18+
Use an API route when you need a route that returns data or handles incoming HTTP requests instead of rendering page UI.
19+
20+
A file becomes an API route when it exports one or more HTTP method names such as [`GET`](/v2/solid-start/reference/server/get), `POST`, `PATCH`, or `DELETE`.
21+
22+
## Create an API route
23+
24+
An API route lives in `src/routes` and exports handler functions named after the HTTP methods it handles.
25+
26+
```tsx title="src/routes/api/ping.ts"
27+
export function GET() {
28+
return new Response("pong");
29+
}
30+
```
31+
32+
The package exports `APIEvent` from `@solidjs/start/server`.
33+
That type includes:
34+
35+
- `request` for the incoming `Request`
36+
- `params` for dynamic path parameters
37+
- `response` for the mutable response stub
38+
- `locals` for request-scoped locals
39+
- `nativeEvent` for the underlying H3 event
40+
41+
```tsx title="src/routes/api/users/[id].ts"
42+
import type { APIEvent } from "@solidjs/start/server";
43+
44+
export async function GET({ params }: APIEvent) {
45+
return Response.json({ userId: params.id });
46+
}
47+
```
48+
49+
## File naming follows the same router
50+
51+
API routes use the same path conventions as UI routes.
52+
For example:
53+
54+
- `src/routes/api/users.ts` becomes `/api/users`
55+
- `src/routes/api/users/[id].ts` becomes `/api/users/:id`
56+
- `src/routes/api/files/[...slug].ts` becomes `/api/files/*slug`
57+
58+
## HEAD fallback
59+
60+
If a route exports `GET` but not `HEAD`, `HEAD` requests are handled by the `GET` handler.
61+
Export `HEAD` explicitly when you need custom behavior.
62+
63+
## Request helpers
64+
65+
Cookie, session, header, and request helpers are exported from `@solidjs/start/http`.
66+
67+
```tsx title="src/routes/api/session.ts"
68+
import { getCookie } from "@solidjs/start/http";
69+
import type { APIEvent } from "@solidjs/start/server";
70+
71+
export function GET(_event: APIEvent) {
72+
const userId = getCookie("userId");
73+
if (!userId) {
74+
return new Response("Not logged in", { status: 401 });
75+
}
76+
77+
return Response.json({ userId });
78+
}
79+
```
80+
81+
## When to use an API route
82+
83+
API routes are a good fit when you need:
84+
85+
- endpoints for other clients
86+
- webhook receivers
87+
- auth callback handlers
88+
- routes that return non-HTML responses
89+
90+
If the data is only needed by your route UI, prefer [Data fetching](/v2/solid-start/building-your-application/data-fetching) or [Data mutation](/v2/solid-start/building-your-application/data-mutation) before introducing a separate API boundary.
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
---
2+
title: Data fetching
3+
use_cases: >-
4+
api calls, database queries, loading states, preloading data, server-side
5+
fetching, route data
6+
tags:
7+
- fetch
8+
- data
9+
- query
10+
- async
11+
- server
12+
version: "2.0"
13+
description: >-
14+
Fetch data in SolidStart v2 with Solid Router queries, createAsync, and
15+
server-backed functions compiled from the use server directive.
16+
---
17+
18+
Data loading is built around Solid Router's [`query`](/solid-router/reference/data-apis/query) API and [`createAsync`](/solid-router/reference/data-apis/create-async).
19+
The `"use server"` directive lets a query body run only on the server when needed.
20+
21+
## Basic query usage
22+
23+
Use `query` to define cached data loading and `createAsync` to read the result inside a route component.
24+
25+
```tsx title="src/routes/posts.tsx"
26+
import { For } from "solid-js";
27+
import { createAsync, query } from "@solidjs/router";
28+
29+
const getPosts = query(async () => {
30+
const response = await fetch("https://example.com/api/posts");
31+
return response.json() as Promise<Array<{ id: number; title: string }>>;
32+
}, "posts");
33+
34+
export default function PostsPage() {
35+
const posts = createAsync(() => getPosts());
36+
37+
return <For each={posts()}>{(post) => <li>{post.title}</li>}</For>;
38+
}
39+
```
40+
41+
## Keep the data function on the server
42+
43+
If your query needs direct access to server-only resources, add the `"use server"` directive inside the query function.
44+
45+
```tsx title="src/routes/account.tsx"
46+
import { createAsync, query } from "@solidjs/router";
47+
import { useSession } from "@solidjs/start/http";
48+
49+
const getCurrentUser = query(async () => {
50+
"use server";
51+
52+
const session = await useSession<{ userId?: string }>({
53+
password: process.env.SESSION_SECRET as string,
54+
name: "session",
55+
});
56+
57+
return { userId: session.data.userId ?? null };
58+
}, "currentUser");
59+
60+
export default function AccountPage() {
61+
const user = createAsync(() => getCurrentUser());
62+
return <pre>{JSON.stringify(user(), null, 2)}</pre>;
63+
}
64+
```
65+
66+
## Preload route data
67+
68+
If you want to warm the query before rendering the page, export a `route.preload` function from the route module.
69+
70+
```tsx title="src/routes/posts/[id].tsx"
71+
import { createAsync, query, type RouteDefinition } from "@solidjs/router";
72+
73+
const getPost = query(async (id: string) => {
74+
"use server";
75+
const response = await fetch(`https://example.com/api/posts/${id}`);
76+
return response.json();
77+
}, "post");
78+
79+
export const route = {
80+
preload: ({ params }) => getPost(params.id),
81+
} satisfies RouteDefinition;
82+
83+
export default function PostPage(props: { params: { id: string } }) {
84+
const post = createAsync(() => getPost(props.params.id));
85+
return <pre>{JSON.stringify(post(), null, 2)}</pre>;
86+
}
87+
```
88+
89+
Cache invalidation and advanced query behavior are handled by [Solid Router](/solid-router), so use the Solid Router references when you need lower-level cache semantics.

0 commit comments

Comments
 (0)