@devries-cameron20 / t3code Mirror · read-only
Fork 0

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, 06:00 UTC

main
mdx 208 lines 5.96 KB
Raw
1 ---
2 title: Vite
3 description: Deploy a Vite SPA to Railway with Railway.Website.Vite — static assets on a container Service, a *.up.railway.app URL (or your domain), and Vite's own dev server under alchemy dev.
4 sidebar:
5 order: 2
6 ---
7
8 import PackageManagers from "../../../../components/PackageManagers.astro";
9
10 export const devDeps = "@alchemy.run/frontend-frameworks";
11
12 `Railway.Website.Vite` deploys a [Vite](https://vite.dev) project to
13 Railway. It runs `vite build` and serves the output from a generated
14 Node static-file server on one
15 [`Railway.Service`](/railway/compute/services). Omit `project` and Alchemy creates a
16 [`Railway.Project`](/railway/compute/projects). Live `url` is
17 `https://{name}.up.railway.app`. Your `vite.config.*` loads natively
18 — plugins included. There is no framework server module.
19
20 React, Vue, and Solid client SPAs belong here, as do multi-page apps
21 that emit one HTML file per route. SSR frameworks that wrap Vite
22 deploy through their own composites —
23 [`Astro`](/railway/frontend/astro),
24 [`Nextjs`](/railway/frontend/nextjs),
25 [`Nuxt`](/railway/frontend/nuxt),
26 [`ReactRouter`](/railway/frontend/react-router),
27 [`SolidStart`](/railway/frontend/solidstart),
28 [`SvelteKit`](/railway/frontend/sveltekit),
29 [`TanStackStart`](/railway/frontend/tanstack-start),
30 [`Waku`](/railway/frontend/waku),
31 [`Octane`](/railway/frontend/octane).
32
33 ## Install
34
35 The build integration is not bundled with alchemy. Install
36 `@alchemy.run/frontend-frameworks`; the resource loads its `/vite`
37 and `/vite/node` exports from your project at deploy time. It is
38 only used at build time, so a dev dependency is enough:
39
40 <PackageManagers dev={devDeps} />
41
42 ## Configure Vite
43
44 Your `vite.config.*` file loads natively — React, Vue, or Solid
45 plugins, Tailwind, and any other non-serializable options work
46 exactly as they do outside Alchemy. Alchemy merges a programmatic
47 config *over* it via the `vite` prop — see
48 [Vite configuration](#vite-configuration) below.
49
50 ```typescript
51 // vite.config.ts
52 import { defineConfig } from "vite";
53 import react from "@vitejs/plugin-react";
54 import tailwindcss from "@tailwindcss/vite";
55
56 export default defineConfig({
57 plugins: [react(), tailwindcss()],
58 });
59 ```
60
61 Do not set a Vite SSR adapter. This composite never creates a
62 framework server module — it only ships the client build.
63
64 ## Declare the Website
65
66 Declare the site as a module-level const (rather than inline in the
67 Stack):
68
69 ```typescript
70 // alchemy.run.ts
71 import * as Railway from "@alchemy.run/railway";
72
73 export const Website = Railway.Website.Vite("Website");
74 ```
75
76 Pass `project` to put the Service in a Project you already have.
77 Omit it and Alchemy creates `Railway.Project("Project")` under the
78 site's namespace:
79
80 ```typescript
81 export const Site = Railway.Project("Site");
82
83 export const Website = Railway.Website.Vite("Website", {
84 project: Site,
85 });
86 ```
87
88 ## Add it to the Stack
89
90 Yield the site from your Stack and return its URL:
91
92 ```typescript
93 // alchemy.run.ts
94 import * as Alchemy from "alchemy";
95 import * as Effect from "effect/Effect";
96
97 export default Alchemy.Stack(
98 "MyViteSite",
99 {
100 providers: Railway.providers(),
101 state: Alchemy.localState(),
102 },
103 Effect.gen(function* () {
104 const site = yield* Website;
105 return { url: site.url };
106 }),
107 );
108 ```
109
110 On deploy, `url` is the generated `*.up.railway.app` hostname.
111 `service` and `project` are the backing resources; they are
112 `undefined` under `alchemy dev`.
113
114 See
115 [examples/railway-website-vite](https://github.com/alchemy-run/alchemy/tree/main/examples/railway-website-vite)
116 for the checked-in example.
117
118 ## Add environment variables
119
120 `env` is process environment — set on `process.env` before
121 `vite build` / `alchemy dev`, and on the Service at deploy. It is
122 not Worker bindings:
123
124 ```diff lang="typescript"
125 // alchemy.run.ts
126 export const Website = Railway.Website.Vite("Website", {
127 + env: {
128 + VITE_GREETING: "Hello from Alchemy!",
129 + },
130 });
131 ```
132
133 Vite inlines `VITE_*` into the client bundle at build time. Other
134 keys reach the hosted Node process but not the browser.
135
136 ## Read the environment
137
138 Client code reads inlined values from `import.meta.env`:
139
140 ```typescript
141 // src/main.ts
142 const greeting = import.meta.env.VITE_GREETING ?? "hello";
143 ```
144
145 ## Single-page vs multi-page
146
147 Plain Vite apps are typically SPAs, so `assets.notFoundHandling`
148 defaults to `"single-page-application"`: unmatched paths get
149 `index.html` with status 200 so client-side routes deep-link. Use
150 `"404-page"` for multi-page sites that ship a `404.html`:
151
152 ```typescript
153 export const Website = Railway.Website.Vite("Docs", {
154 assets: { notFoundHandling: "404-page" },
155 });
156 ```
157
158 ## Vite configuration
159
160 `rootDir` is the project root (the directory containing
161 `package.json`). Default `"."`.
162
163 `vite.outDir` and `vite.base` are serializable overrides merged
164 *over* the config file:
165
166 ```typescript
167 export const Website = Railway.Website.Vite("Website", {
168 rootDir: "./app",
169 vite: { outDir: "build", base: "/docs/" },
170 });
171 ```
172
173 `outDir` is relative to `rootDir` and defaults to the file's
174 `build.outDir` (Vite's default: `"dist"`). `base` is Vite's public
175 base path.
176
177 ## Local development
178
179 `alchemy dev` runs Vite's own dev server (native HMR) instead of
180 deploying; `site.url` is the local address and no Project or Service
181 is created. Wrap the site in
182 `Alchemy.remote()` to deploy the real infrastructure even during
183 dev.
184
185 ## Custom domain
186
187 `domain` is a hostname string. Alchemy attaches a
188 [`Railway.CustomDomain`](/railway/networking) (`targetPort` 3000)
189 and `url` becomes `https://{domain}`:
190
191 ```typescript
192 export const Website = Railway.Website.Vite("Website", {
193 domain: "app.example.com",
194 });
195 ```
196
197 ## Where next
198
199 - [`Railway.Website.Vite` API](/providers/railway/website/vite)
200 - [Astro](/railway/frontend/astro),
201 [Next.js](/railway/frontend/nextjs),
202 [Nuxt](/railway/frontend/nuxt),
203 [SvelteKit](/railway/frontend/sveltekit),
204 [Waku](/railway/frontend/waku),
205 [Octane](/railway/frontend/octane) — SSR frameworks
206 - [Projects](/railway/compute/projects),
207 [Services](/railway/compute/services),
208 [Custom domains](/railway/networking)