Open History to see the last commit
1Welcome to your new TanStack Start app!
2
3# Getting Started
4
5To run this application:
6
7```bash
8pnpm install
9pnpm dev
10```
11
12# Building For Production
13
14To build this application for production:
15
16```bash
17pnpm build
18```
19
20## Styling
21
22This project uses [Tailwind CSS](https://tailwindcss.com/) for styling.
23
24### Removing Tailwind CSS
25
26If you prefer not to use Tailwind CSS:
27
281. Remove the demo pages in `src/routes/demo/`
292. Replace the Tailwind import in `src/styles.css` with your own styles
303. Remove `tailwindcss()` from the plugins array in `vite.config.ts`
314. Remove `@tailwindcss/vite` and `tailwindcss` from `package.json`
32
33## Deploy to Cloudflare Workers
34
35This project uses the Cloudflare Vite plugin (configured in `vite.config.ts`) and `wrangler.jsonc`:
36
371. Install Wrangler: `npm install -g wrangler`
382. Authenticate: `wrangler login`
393. Deploy: `npx wrangler deploy`
40
41For production env vars, run `wrangler secret put MY_VAR` for each secret listed in `.env.example`. Public (non-secret) vars go in `wrangler.jsonc` under `vars`.
42
43KV, D1, R2, and Durable Object bindings are configured in `wrangler.jsonc` — see https://developers.cloudflare.com/workers/wrangler/configuration/.
44
45## Routing
46
47This project uses [TanStack Router](https://tanstack.com/router) with file-based routing. Routes are managed as files in `src/routes`.
48
49### Adding A Route
50
51To add a new route to your application just add a new file in the `./src/routes` directory.
52
53TanStack will automatically generate the content of the route file for you.
54
55Now that you have two routes you can use a `Link` component to navigate between them.
56
57### Adding Links
58
59To use SPA (Single Page Application) navigation you will need to import the `Link` component from `@tanstack/react-router`.
60
61```tsx
62import { Link } from "@tanstack/react-router";
63```
64
65Then anywhere in your JSX you can use it like so:
66
67```tsx
68<Link to="/about">About</Link>
69```
70
71This will create a link that will navigate to the `/about` route.
72
73More information on the `Link` component can be found in the [Link documentation](https://tanstack.com/router/v1/docs/framework/react/api/router/linkComponent).
74
75### Using A Layout
76
77In the File Based Routing setup the layout is located in `src/routes/__root.tsx`. Anything you add to the root route will appear in all the routes. The route content will appear in the JSX where you render `{children}` in the `shellComponent`.
78
79Here is an example layout that includes a header:
80
81```tsx
82import { HeadContent, Scripts, createRootRoute } from "@tanstack/react-router";
83
84export const Route = createRootRoute({
85 head: () => ({
86 meta: [
87 { charSet: "utf-8" },
88 { name: "viewport", content: "width=device-width, initial-scale=1" },
89 { title: "My App" },
90 ],
91 }),
92 shellComponent: ({ children }) => (
93 <html lang="en">
94 <head>
95 <HeadContent />
96 </head>
97 <body>
98 <header>
99 <nav>
100 <Link to="/">Home</Link>
101 <Link to="/about">About</Link>
102 </nav>
103 </header>
104 {children}
105 <Scripts />
106 </body>
107 </html>
108 ),
109});
110```
111
112More information on layouts can be found in the [Layouts documentation](https://tanstack.com/router/latest/docs/framework/react/guide/routing-concepts#layouts).
113
114## Server Functions
115
116TanStack Start provides server functions that allow you to write server-side code that seamlessly integrates with your client components.
117
118```tsx
119import { createServerFn } from "@tanstack/react-start";
120
121const getServerTime = createServerFn({
122 method: "GET",
123}).handler(async () => {
124 return new Date().toISOString();
125});
126
127// Use in a component
128function MyComponent() {
129 const [time, setTime] = useState("");
130
131 useEffect(() => {
132 getServerTime().then(setTime);
133 }, []);
134
135 return <div>Server time: {time}</div>;
136}
137```
138
139## API Routes
140
141You can create API routes by using the `server` property in your route definitions:
142
143```tsx
144import { createFileRoute } from "@tanstack/react-router";
145import { json } from "@tanstack/react-start";
146
147export const Route = createFileRoute("/api/hello")({
148 server: {
149 handlers: {
150 GET: () => json({ message: "Hello, World!" }),
151 },
152 },
153});
154```
155
156## Data Fetching
157
158There are multiple ways to fetch data in your application. You can use TanStack Query to fetch data from a server. But you can also use the `loader` functionality built into TanStack Router to load the data for a route before it's rendered.
159
160For example:
161
162```tsx
163import { createFileRoute } from "@tanstack/react-router";
164
165export const Route = createFileRoute("/people")({
166 loader: async () => {
167 const response = await fetch("https://swapi.dev/api/people");
168 return response.json();
169 },
170 component: PeopleComponent,
171});
172
173function PeopleComponent() {
174 const data = Route.useLoaderData();
175 return (
176 <ul>
177 {data.results.map((person) => (
178 <li key={person.name}>{person.name}</li>
179 ))}
180 </ul>
181 );
182}
183```
184
185Loaders simplify your data fetching logic dramatically. Check out more information in the [Loader documentation](https://tanstack.com/router/latest/docs/framework/react/guide/data-loading#loader-parameters).
186
187# Learn More
188
189You can learn more about all of the offerings from TanStack in the [TanStack documentation](https://tanstack.com).
190
191For TanStack Start specific documentation, visit [TanStack Start](https://tanstack.com/start).