|
| 1 | +Set up server-side rendering (SSR) with Solid, Vite, and Nitro. This setup enables streaming HTML responses, automatic asset management, and client hydration. |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +1. Add the Nitro Vite plugin to your Vite config |
| 6 | +2. Create a server entry that renders your app to HTML |
| 7 | +3. Create a client entry that hydrates the server-rendered HTML |
| 8 | + |
| 9 | +## 1. Configure Vite |
| 10 | + |
| 11 | +Add the SolidStart and Nitro plugins to your Vite config. |
| 12 | + |
| 13 | +```js [vite.config.ts] |
| 14 | +import { defineConfig } from "vite"; |
| 15 | +import { solidStart } from "@solidjs/start/config"; |
| 16 | +import { nitro } from "nitro/vite"; |
| 17 | + |
| 18 | +export default defineConfig({ |
| 19 | + plugins: [solidStart(), nitro()], |
| 20 | +}); |
| 21 | +``` |
| 22 | + |
| 23 | +## 2. Create the App Component |
| 24 | + |
| 25 | +Create a shared Solid component that runs on both server and client: |
| 26 | + |
| 27 | +```tsx [src/app.tsx] |
| 28 | +import { MetaProvider, Title } from "@solidjs/meta"; |
| 29 | +import { Router } from "@solidjs/router"; |
| 30 | +import { FileRoutes } from "@solidjs/start/router"; |
| 31 | +import { Suspense } from "solid-js"; |
| 32 | + |
| 33 | +export default function App() { |
| 34 | + return ( |
| 35 | + <Router |
| 36 | + root={(props) => ( |
| 37 | + <MetaProvider> |
| 38 | + <Title>SolidStart - Basic</Title> |
| 39 | + <Suspense>{props.children}</Suspense> |
| 40 | + </MetaProvider> |
| 41 | + )} |
| 42 | + > |
| 43 | + <FileRoutes /> |
| 44 | + </Router> |
| 45 | + ); |
| 46 | +} |
| 47 | +``` |
| 48 | + |
| 49 | +## 3. Create the Server Entry |
| 50 | + |
| 51 | +The server entry renders your Solid app to a streaming HTML response: |
| 52 | + |
| 53 | +```tsx [src/entry-server.tsx] |
| 54 | +// @refresh reload |
| 55 | +import { createHandler, StartServer } from "@solidjs/start/server"; |
| 56 | + |
| 57 | +export default createHandler(() => ( |
| 58 | + <StartServer |
| 59 | + document={({ assets, children, scripts }) => ( |
| 60 | + <html lang="en"> |
| 61 | + <head> |
| 62 | + <meta charset="utf-8" /> |
| 63 | + <meta name="viewport" content="width=device-width, initial-scale=1" /> |
| 64 | + <link rel="icon" href="/favicon.ico" /> |
| 65 | + {assets} |
| 66 | + </head> |
| 67 | + <body> |
| 68 | + <div id="app">{children}</div> |
| 69 | + {scripts} |
| 70 | + </body> |
| 71 | + </html> |
| 72 | + )} |
| 73 | + /> |
| 74 | +)); |
| 75 | +``` |
| 76 | + |
| 77 | +## 4. Create the Client Entry |
| 78 | + |
| 79 | +The client entry hydrates the server-rendered HTML, attaching Solid's event handlers: |
| 80 | + |
| 81 | +```tsx [src/entry-client.tsx] |
| 82 | +// @refresh reload |
| 83 | +import { mount, StartClient } from "@solidjs/start/client"; |
| 84 | + |
| 85 | +mount(() => <StartClient />, document.getElementById("app")!); |
| 86 | +``` |
0 commit comments