@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 181 lines 5.14 KB
Raw
1 ---
2 title: SvelteKit
3 description: Deploy a SvelteKit app to Fly with Fly.Website.SvelteKit — kit SSR on a Machine, prerendered assets baked into the image, and Kit's own dev server under alchemy dev.
4 sidebar:
5 order: 12
6 ---
7
8 import PackageManagers from "../../../../components/PackageManagers.astro";
9
10 export const devDeps = "@alchemy.run/frontend-frameworks";
11
12 `Fly.Website.SvelteKit` deploys a
13 [SvelteKit](https://svelte.dev/docs/kit) app to Fly. It builds the
14 app with SvelteKit's own Vite pipeline and an in-memory Node adapter.
15 Client assets and prerendered pages bake into the Machine image and
16 are served first; dynamic routes run in the kit handler on port 3000.
17 Alchemy creates a `Fly.App` if you omit `app`, a `Fly.Service` on
18 that App, and a shared IPv4 so `https://{app}.fly.dev` answers. Your
19 `vite.config.ts` loads natively; there is no `svelte.config.js` to
20 write (kit v3 dropped it) and no adapter to install.
21
22 ## Install
23
24 The build integration is not bundled with alchemy. Install
25 `@alchemy.run/frontend-frameworks`; the resource loads its
26 `/sveltekit` and `/sveltekit/node` exports from your project at
27 deploy time. It is only used at build time, so a dev dependency is
28 enough:
29
30 <PackageManagers dev={devDeps} />
31
32 ## Configure SvelteKit
33
34 Your project's `vite.config.ts` loads natively — your Vite plugins
35 and the kit options in your `sveltekit(...)` call all apply as usual.
36 Alchemy injects its Node adapter (replacing any adapter you declare,
37 with a warning), so a fresh SvelteKit project deploys as-is.
38 Deploy-specific kit overrides can also be passed via the `kit` prop,
39 which merges over your `sveltekit(...)` options — see
40 [Kit options](#kit-options) below. A project without a
41 `vite.config.*` works too: the resource falls back to a fully
42 programmatic build.
43
44 ## Declare the Website
45
46 Declare the site as a module-level const (rather than inline in the
47 Stack):
48
49 ```typescript
50 // alchemy.run.ts
51 import * as Fly from "@alchemy.run/fly";
52
53 export const Website = Fly.Website.SvelteKit("Website");
54 ```
55
56 Omit `app` to create a `Fly.App` under the site namespace. Pass an
57 existing App to share it with other resources.
58
59 ## Add it to the Stack
60
61 Yield the site from your Stack and return its URL:
62
63 ```typescript
64 // alchemy.run.ts
65 import * as Alchemy from "alchemy";
66 import * as Effect from "effect/Effect";
67
68 export default Alchemy.Stack(
69 "MySvelteKitSite",
70 {
71 providers: Fly.providers(),
72 state: Alchemy.localState(),
73 },
74 Effect.gen(function* () {
75 const site = yield* Website;
76 return { url: site.url };
77 }),
78 );
79 ```
80
81 Routes with `export const prerender = true` are baked into the image
82 and served as static files first; server routes (`+server.ts`) and
83 server load functions run in the kit handler on the Machine.
84
85 ## Add environment variables
86
87 Process environment is a top-level `env` map:
88
89 ```diff lang="typescript"
90 // alchemy.run.ts
91 export const Website = Fly.Website.SvelteKit("Website", {
92 + env: {
93 + GREETING: "Hello from alchemy",
94 + API_BASE: "https://api.example.com",
95 + },
96 });
97 ```
98
99 The values are copied onto `process.env` before the build (and the
100 dev server) and onto the Machine at deploy time, so server code
101 reads the same keys in both modes.
102
103 ## Read the environment in server code
104
105 The Kit server runs in Node on the Machine, so server routes read
106 the environment from `process.env` (or kit's `$env/dynamic/private`,
107 which is initialized from it):
108
109 ```typescript
110 // src/routes/+page.server.ts
111 export const load = () => {
112 return { greeting: process.env.GREETING ?? "hello" };
113 };
114 ```
115
116 ## Kit options
117
118 Since kit v3 there is no `svelte.config.js` — kit options live in the
119 `sveltekit(...)` call in your `vite.config.ts`, which loads natively:
120
121 ```typescript
122 // vite.config.ts
123 import { sveltekit } from "@sveltejs/kit/vite";
124 import tailwindcss from "@tailwindcss/vite";
125 import { defineConfig } from "vite";
126
127 export default defineConfig({
128 plugins: [
129 tailwindcss(),
130 sveltekit({
131 prerender: { handleHttpError: "warn" },
132 }),
133 ],
134 });
135 ```
136
137 The `kit` prop on the resource is a deploy-time override layer merged
138 over those options (the override wins). It must be JSON-serializable:
139
140 ```typescript
141 // alchemy.run.ts
142 export const Website = Fly.Website.SvelteKit("Website", {
143 kit: {
144 paths: { base: "/docs" },
145 },
146 });
147 ```
148
149 Don't set `kit.adapter` — Alchemy injects its Node adapter (an
150 adapter declared in your `sveltekit(...)` call is replaced with a
151 warning).
152
153 ## Local dev
154
155 ```sh
156 bun alchemy dev
157 ```
158
159 `alchemy dev` runs SvelteKit's own Vite dev server — Node SSR with
160 full HMR — instead of deploying; `site.url` is the local address and
161 no Fly resources are created (`app`, `service`, `ip`, and
162 `certificate` are `undefined`). Wrap the site in `Alchemy.remote()`
163 to deploy the live App even during dev.
164
165 ## Custom domain
166
167 ```typescript
168 const site = yield* Fly.Website.SvelteKit("Web", {
169 domain: "app.example.com",
170 });
171 ```
172
173 Alchemy requests an ACME [`Fly.Certificate`](/fly/networking) on the
174 App. Point DNS at the App first — v1 does not create records.
175 `url` becomes `https://app.example.com`.
176
177 ## Where next
178
179 - [`SvelteKit` API](/providers/fly/website/sveltekit)
180 - [Apps](/fly/compute/apps) and [Services](/fly/compute/services)
181 - [IPs & certificates](/fly/networking)