@@ -117,14 +117,25 @@ export default function Home() {
117117Add a ` fetchData ` export to enable SSR:
118118
119119``` tsx
120+ import { useContext } from " react" ;
121+ import Context from " ~/context" ;
122+
120123// src/pages/ssr.tsx
121124export async function fetchData() {
122125 const data = await fetch (" https://api.example.com/data" );
123- return data .json ();
126+ return {
127+ head: {
128+ // meta tags
129+ },
130+ context: {
131+ myParam: data .myParam ;
132+ }
133+ };
124134}
125135
126136export default function SSRPage({ data }: { data: any }) {
127- return <h1 >Server-rendered: { data .title } </h1 >;
137+ const context = useContext (Context );
138+ return <h1 >Server-rendered: { data .myParam } </h1 >;
128139}
129140```
130141
@@ -134,7 +145,9 @@ Configure routes to pre-render in `src/prerender.ts`:
134145
135146``` tsx
136147// src/prerender.ts
137- export default [{ path: " /" }, { path: " /about" }, { path: " /blog/post-1" }];
148+
149+ // Export an array of paths to be prerendered.
150+ export default [" /" , " /about" , " /blog/post-1" ];
138151```
139152
140153## 🔌 API Routes
@@ -143,29 +156,21 @@ Create API endpoints by adding files to `src/api/`:
143156
144157``` typescript
145158// src/api/hello.ts
146- export default function handler(request : Request ) {
147- return new Response (JSON .stringify ({ message: " Hello, World!" }), {
148- headers: { " Content-Type" : " application/json" },
149- });
150- }
159+ export default async (req : http .IncomingMessage , res : http .ServerResponse ) => {
160+ res .setHeader (" Content-Type" , " application/json" );
161+ res .writeHead (200 , " Success" );
162+ res .write (
163+ JSON .stringify ({
164+ payload:
165+ " This is an API function - can be deployed as a serverless function!" ,
166+ })
167+ );
168+ res .end ();
169+ };
151170```
152171
153172Access at: ` http://localhost:5173/api/hello `
154173
155- ### Dynamic API Routes
156-
157- ``` typescript
158- // src/api/users/[id].ts
159- export default function handler(request : Request ) {
160- const url = new URL (request .url );
161- const id = url .pathname .split (" /" ).pop ();
162-
163- return new Response (JSON .stringify ({ userId: id }), {
164- headers: { " Content-Type" : " application/json" },
165- });
166- }
167- ```
168-
169174## 🚀 Deployment
170175
171176### Stormkit
0 commit comments