The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | --- |
| 2 | title: SolidStart |
| 3 | description: Deploy a SolidStart app to AWS with AWS.Website.SolidStart — SSR on a Lambda Web Function, S3 assets behind CloudFront, and SolidStart's own Vite dev server under alchemy dev. |
| 4 | sidebar: |
| 5 | order: 10.5 |
| 6 | --- |
| 7 | |
| 8 | import PackageManagers from "../../../../components/PackageManagers.astro"; |
| 9 | |
| 10 | export const devDeps = "@alchemy.run/frontend-frameworks"; |
| 11 | |
| 12 | `AWS.Website.SolidStart` deploys a |
| 13 | [SolidStart](https://start.solidjs.com) app to AWS. It runs your |
| 14 | project's own `vite build` and appends nitro's `node` preset: the SSR |
| 15 | server runs on a [Lambda Web Function](/aws/compute/web-functions) and |
| 16 | streams its responses, and client assets plus prerendered pages are |
| 17 | served from a private S3 bucket through CloudFront. A CloudFront |
| 18 | Function routes each request at the edge — uploaded files go to S3, |
| 19 | everything else streams from the server. |
| 20 | |
| 21 | :::note[Verified version line] |
| 22 | This page tracks `@solidjs/start` **2.x** with |
| 23 | `@solidjs/vite-plugin-nitro-2` — the Vite-native line where |
| 24 | `solidStart()` is a plain Vite plugin and nitro is a second plugin. |
| 25 | ::: |
| 26 | |
| 27 | ## Install |
| 28 | |
| 29 | The build integration is not bundled with alchemy. Install |
| 30 | `@alchemy.run/frontend-frameworks`; the resource loads its |
| 31 | `/solidstart` and `/solidstart/node` exports from your project at |
| 32 | deploy time. It is only used at build time, so a dev dependency is |
| 33 | enough: |
| 34 | |
| 35 | <PackageManagers dev={devDeps} /> |
| 36 | |
| 37 | `@solidjs/vite-plugin-nitro-2` is the server half of a SolidStart |
| 38 | build and must be a project dependency too: |
| 39 | |
| 40 | <PackageManagers add="@solidjs/vite-plugin-nitro-2" /> |
| 41 | |
| 42 | ## Configure Vite |
| 43 | |
| 44 | Your `vite.config.ts` is just the SolidStart plugin — no adapter, no |
| 45 | nitro wiring: |
| 46 | |
| 47 | ```typescript |
| 48 | // vite.config.ts |
| 49 | import { solidStart } from "@solidjs/start/config"; |
| 50 | import { defineConfig } from "vite"; |
| 51 | |
| 52 | export default defineConfig({ |
| 53 | plugins: [solidStart()], |
| 54 | }); |
| 55 | ``` |
| 56 | |
| 57 | Alchemy appends its own `nitroV2Plugin()` instance at build time, |
| 58 | carrying nitro's `node` preset (`aws-lambda`, streaming enabled, with |
| 59 | `server: "function"`). |
| 60 | Don't register `nitroV2Plugin()` yourself — the appended instance |
| 61 | would discard its options, so the build fails with an actionable |
| 62 | error instead. Pass nitro options through the resource's `nitro` |
| 63 | prop; see [Prerendering](#prerendering) below. |
| 64 | |
| 65 | ## Declare the Website |
| 66 | |
| 67 | Declare the site as a module-level const (rather than inline in the |
| 68 | Stack): |
| 69 | |
| 70 | ```typescript |
| 71 | // alchemy.run.ts |
| 72 | import * as AWS from "@alchemy.run/aws"; |
| 73 | |
| 74 | export const Website = AWS.Website.SolidStart("Website"); |
| 75 | ``` |
| 76 | |
| 77 | `rootDir` is the project root — the directory holding `package.json` |
| 78 | and `vite.config.*`. It defaults to `"."`, so set it only when the app |
| 79 | does not sit next to `alchemy.run.ts`: |
| 80 | |
| 81 | ```typescript |
| 82 | export const Website = AWS.Website.SolidStart("Website", { |
| 83 | rootDir: "./app", |
| 84 | }); |
| 85 | ``` |
| 86 | |
| 87 | ## Add it to the Stack |
| 88 | |
| 89 | Yield the site from your Stack and return its URL: |
| 90 | |
| 91 | ```typescript |
| 92 | // alchemy.run.ts |
| 93 | import * as Alchemy from "alchemy"; |
| 94 | import * as Effect from "effect/Effect"; |
| 95 | |
| 96 | export default Alchemy.Stack( |
| 97 | "MySolidStartSite", |
| 98 | { |
| 99 | providers: AWS.providers(), |
| 100 | state: AWS.state(), |
| 101 | }, |
| 102 | Effect.gen(function* () { |
| 103 | const site = yield* Website; |
| 104 | return { url: site.url }; |
| 105 | }), |
| 106 | ); |
| 107 | ``` |
| 108 | |
| 109 | ## Add environment variables |
| 110 | |
| 111 | The server function's environment is configured under `env`: |
| 112 | |
| 113 | ```diff lang="typescript" |
| 114 | // alchemy.run.ts |
| 115 | export const Website = AWS.Website.SolidStart("Website", { |
| 116 | + env: { |
| 117 | + API_BASE: "https://api.example.com", |
| 118 | + }, |
| 119 | }); |
| 120 | ``` |
| 121 | |
| 122 | `env` values are set on the server on deploy and injected into |
| 123 | the dev server's process environment under `alchemy dev`, so server |
| 124 | code reads the same values in both modes. Sibling props tune the |
| 125 | server itself (`timeout`, `maxConcurrencyPerEnvironment`, |
| 126 | `maxEnvironments`). |
| 127 | |
| 128 | An output from another resource only exists once that resource is |
| 129 | yielded, so declare the site inside the Stack generator instead: |
| 130 | |
| 131 | ```typescript |
| 132 | // alchemy.run.ts |
| 133 | Effect.gen(function* () { |
| 134 | const api = yield* Api; |
| 135 | |
| 136 | const site = yield* AWS.Website.SolidStart("Website", { |
| 137 | env: { API_BASE: api.url }, |
| 138 | }); |
| 139 | |
| 140 | return { url: site.url }; |
| 141 | }); |
| 142 | ``` |
| 143 | |
| 144 | ## Read the environment in server code |
| 145 | |
| 146 | The SolidStart server is a plain Node server, so API routes, |
| 147 | server functions, and SSR read the environment from `process.env`: |
| 148 | |
| 149 | ```typescript |
| 150 | // src/routes/api/hello.ts |
| 151 | export function GET() { |
| 152 | return new Response(process.env.API_BASE ?? "unset"); |
| 153 | } |
| 154 | ``` |
| 155 | |
| 156 | Values prefixed `VITE_` are inlined into the client bundle at build |
| 157 | time instead, as `import.meta.env.VITE_*` — set those in the shell |
| 158 | that runs `alchemy deploy`, not in `env`. |
| 159 | |
| 160 | ## Prerendering |
| 161 | |
| 162 | Nitro renders the routes you list at build time into `.output/public`. |
| 163 | They are uploaded to S3 and served from the edge with no Lambda |
| 164 | invocation: |
| 165 | |
| 166 | ```typescript |
| 167 | export const Website = AWS.Website.SolidStart("Website", { |
| 168 | nitro: { prerender: { routes: ["/", "/about"] } }, |
| 169 | }); |
| 170 | ``` |
| 171 | |
| 172 | `prerender` takes the routes to render (`routes`) and can crawl links |
| 173 | from prerendered pages to prerender them too (`crawlLinks`). The |
| 174 | `nitro` prop is the home for all nitro options (route rules, storage, |
| 175 | ...) — the integration owns the plugin instance, so they cannot live |
| 176 | in `vite.config.ts`. Values must be JSON-serializable; `preset` is |
| 177 | owned by the deploy target, and a foreign preset is a hard error. |
| 178 | |
| 179 | ## Run on a Lambda Function |
| 180 | |
| 181 | Web Functions run in a subset of Regions. Elsewhere, set |
| 182 | `server: "function"`: the app builds with the `/solidstart/aws` deploy target |
| 183 | and runs on a Lambda Function behind a streaming Function URL, tuned with |
| 184 | `memorySize`, `architecture`, and `runtime`: |
| 185 | |
| 186 | ```diff lang="typescript" |
| 187 | // alchemy.run.ts |
| 188 | export const Website = AWS.Website.SolidStart("Website", { |
| 189 | + server: "function", |
| 190 | + memorySize: 2048, |
| 191 | }); |
| 192 | ``` |
| 193 | |
| 194 | ## Local dev |
| 195 | |
| 196 | ```sh |
| 197 | bun alchemy dev |
| 198 | ``` |
| 199 | |
| 200 | `alchemy dev` runs SolidStart's own Vite dev server (native HMR) |
| 201 | instead of deploying anything; `site.url` is the dev server's |
| 202 | `http://localhost:<port>` address and no AWS resources are created. |
| 203 | Nitro plays no part in dev. Wrap the site in `Alchemy.remote()` to |
| 204 | deploy the real infrastructure even during dev. |
| 205 | |
| 206 | SolidStart's plugin resolves its app root and route directory from |
| 207 | the process working directory, so one `alchemy dev` process serves |
| 208 | one SolidStart app at a time. |
| 209 | |
| 210 | :::caution[Run `alchemy dev` under Node] |
| 211 | SolidStart 2's dev server answers requests through `srvx/node`, which |
| 212 | returns the string `[object Object]` instead of the rendered document |
| 213 | when it runs under Bun. Production builds are unaffected — only the |
| 214 | dev server is. Until that upstream gap closes, run `alchemy dev` with |
| 215 | Node (`npx alchemy dev`) for SolidStart projects. |
| 216 | ::: |
| 217 | |
| 218 | ## Custom domain |
| 219 | |
| 220 | ```typescript |
| 221 | // alchemy.run.ts |
| 222 | export const Website = AWS.Website.SolidStart("Website", { |
| 223 | domain: { name: "app.example.com" }, |
| 224 | }); |
| 225 | ``` |
| 226 | |
| 227 | The certificate is created in `us-east-1` (required by CloudFront), |
| 228 | DNS validation records and the alias record are managed through |
| 229 | Route 53. The hosted zone is inferred from the hostname — pass |
| 230 | `hostedZoneId` to pin it when several zones could match. |
| 231 | |
| 232 | ## Share a distribution with Router |
| 233 | |
| 234 | CloudFront distributions take minutes to create. To serve several |
| 235 | sites (or a site plus an API) from one distribution, attach the site |
| 236 | to an [`AWS.Website.Router`](/aws/frontend/static-site#compose-sites-with-a-router): |
| 237 | |
| 238 | The site takes the Router's outputs, so both are yielded inside the |
| 239 | Stack generator: |
| 240 | |
| 241 | ```typescript |
| 242 | // alchemy.run.ts |
| 243 | Effect.gen(function* () { |
| 244 | const router = yield* AWS.Website.Router("FrontDoor", { |
| 245 | domain: { name: "example.com" }, |
| 246 | }); |
| 247 | |
| 248 | const site = yield* AWS.Website.SolidStart("Website", { |
| 249 | domain: { router }, |
| 250 | }); |
| 251 | |
| 252 | return { url: site.url }; |
| 253 | }); |
| 254 | ``` |