The best way to control Claude Code, Codex and more from any computer or phone

main
mdx 154 lines 4.37 KB
Raw
1 ---
2 title: Vite
3 description: Deploy a Vite SPA to Prisma Compute with Prisma.Website.Vite — static files served on Bun and Vite's native dev server locally.
4 sidebar:
5 order: 2
6 ---
7
8 import PackageManagers from "../../../../components/PackageManagers.astro";
9
10 export const devDeps = "@alchemy.run/frontend-frameworks @vercel/nft";
11
12 `Prisma.Website.Vite` runs [Vite](https://vite.dev)'s build and serves
13 its output with a generated static-file server on **Bun in Prisma
14 Compute**. The Node-target output is uploaded as `tar.gz`, not a Docker
15 image. There is no framework SSR handler, but the site still has a
16 Compute runtime.
17
18 React, Vue, and Solid client SPAs belong here, as do multi-page HTML
19 apps. Use the dedicated [Astro](/prisma/frontend/astro),
20 [React Router](/prisma/frontend/react-router),
21 [SolidStart](/prisma/frontend/solidstart),
22 [SvelteKit](/prisma/frontend/sveltekit), or
23 [TanStack Start](/prisma/frontend/tanstack-start) resource for SSR.
24
25 ## Install
26
27 The build integration is separate from alchemy. Install it in the Vite
28 project; the resource loads its `/vite` and `/vite/node` exports:
29
30 <PackageManagers dev={devDeps} />
31
32 ## Configure Vite
33
34 Your `vite.config.*` loads natively, including framework plugins and
35 Tailwind. No SSR adapter is needed:
36
37 ```typescript
38 // vite.config.ts
39 import { defineConfig } from "vite";
40 import react from "@vitejs/plugin-react";
41 import tailwindcss from "@tailwindcss/vite";
42
43 export default defineConfig({
44 plugins: [react(), tailwindcss()],
45 });
46 ```
47
48 ## Declare the Website
49
50 ```typescript
51 // alchemy.run.ts
52 import * as Prisma from "@alchemy.run/prisma";
53
54 export const Website = Prisma.Website.Vite("Website");
55 ```
56
57 Omit `project` to create a database-less Prisma project on live deploy.
58 Pass `project` to use one already declared in your Stack.
59
60 ## Add it to the Stack
61
62 ```typescript
63 // alchemy.run.ts
64 import * as Alchemy from "alchemy";
65 import * as Effect from "effect/Effect";
66
67 export default Alchemy.Stack(
68 "MyViteSite",
69 { providers: Prisma.providers(), state: Alchemy.localState() },
70 Effect.gen(function* () {
71 const site = yield* Website;
72 return { url: site.url };
73 }),
74 );
75 ```
76
77 `site.url` is the Compute endpoint on deploy and Vite's local address
78 in dev. See the [Vite example](https://github.com/alchemy-run/alchemy/tree/main/examples/prisma-website-vite).
79
80 ## Add environment variables
81
82 ```diff lang="typescript"
83 export const Website = Prisma.Website.Vite("Website", {
84 + env: {
85 + VITE_GREETING: "Hello from Alchemy!",
86 + },
87 });
88 ```
89
90 `env` is applied before build and dev, and passed to Compute. Vite
91 inlines `VITE_*` into the client bundle; other keys are not exposed to
92 the browser. Never put a secret in a `VITE_*` key.
93
94 ## Read the environment
95
96 ```typescript
97 // src/main.ts
98 const greeting = import.meta.env.VITE_GREETING ?? "hello";
99 ```
100
101 The browser sees the value captured at build time, not a request-time
102 lookup of Compute's environment.
103
104 ## Single-page vs multi-page
105
106 `assets.notFoundHandling` defaults to `"single-page-application"`:
107 unmatched paths return `index.html` with status 200 for client routing.
108 Use `"404-page"` for multi-page sites with a `404.html`:
109
110 ```typescript
111 export const Website = Prisma.Website.Vite("Docs", {
112 assets: { notFoundHandling: "404-page" },
113 });
114 ```
115
116 ## Vite configuration
117
118 `rootDir` is the directory containing `package.json`, default `"."`.
119 The serializable `vite` bag overrides the native config:
120
121 ```typescript
122 export const Website = Prisma.Website.Vite("Website", {
123 rootDir: "./app",
124 vite: { outDir: "build", base: "/docs/" },
125 });
126 ```
127
128 `outDir` is relative to `rootDir`; omitted, it follows the file's
129 `build.outDir` (Vite defaults to `dist`). `base` sets the public base path.
130
131 ## Local development
132
133 `bun alchemy dev` runs Vite's own server with HMR. The Website creates
134 no Prisma project, database, app, or domain. Use `.pipe(Alchemy.remote())`
135 to deploy the live path during dev instead.
136
137 ## Custom domain
138
139 ```typescript
140 export const Website = Prisma.Website.Vite("Website", {
141 domain: "app.example.com",
142 });
143 ```
144
145 This creates `Prisma.CustomDomain` and makes `site.url` the HTTPS hostname.
146 Configure the
147 returned DNS records yourself and verify provisioning status before
148 cutover; see [Custom domains](/prisma/frontend/websites#custom-domains).
149
150 ## Where next
151
152 - [Vite API](/providers/prisma/website/vite).
153 - [Websites](/prisma/frontend/websites) — the full framework family.
154 - [Compute apps](/prisma/compute/apps) — the underlying runtime.