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: Vinext |
| 3 | description: Deploy vinext to Cloudflare Workers with the shared Website API, native local development, and KV-backed caching. |
| 4 | sidebar: |
| 5 | order: 9.6 |
| 6 | --- |
| 7 | |
| 8 | import PackageManagers from "../../../../components/PackageManagers.astro"; |
| 9 | |
| 10 | export const devDeps = "@alchemy.run/frontend-frameworks"; |
| 11 | |
| 12 | `Cloudflare.Website.Vinext` builds a [vinext](https://vinext.dev) App Router |
| 13 | application with Vite and deploys its React Server Components Worker and client |
| 14 | assets. Alchemy injects its Cloudflare Vite plugin, prerenders routes, and seeds |
| 15 | the managed KV cache. There is no Wrangler configuration or OpenNext build. |
| 16 | |
| 17 | ## Install |
| 18 | |
| 19 | Keep `vinext`, `react`, `react-dom`, and `react-server-dom-webpack` in your |
| 20 | application dependencies, and install `vite` and `@vitejs/plugin-rsc` for |
| 21 | building. Add the Alchemy integration: |
| 22 | |
| 23 | <PackageManagers dev={devDeps} /> |
| 24 | |
| 25 | ## Configure vinext |
| 26 | |
| 27 | ```typescript |
| 28 | // vite.config.ts |
| 29 | import { defineConfig } from "vite"; |
| 30 | import vinext from "vinext"; |
| 31 | |
| 32 | export default defineConfig({ |
| 33 | plugins: [vinext({ prerender: true })], |
| 34 | }); |
| 35 | ``` |
| 36 | |
| 37 | Alchemy injects its Cloudflare runtime and KV cache plugins. No Alchemy plugin |
| 38 | is required in `vite.config.ts`; leave `@cloudflare/vite-plugin` out as well. |
| 39 | Deployment settings belong on `Cloudflare.Website.Vinext`. |
| 40 | |
| 41 | ## Declare the Website |
| 42 | |
| 43 | ```typescript |
| 44 | // alchemy.run.ts |
| 45 | import * as Cloudflare from "@alchemy.run/cloudflare"; |
| 46 | |
| 47 | export const Website = Cloudflare.Website.Vinext("Website", { |
| 48 | rootDir: ".", |
| 49 | }); |
| 50 | |
| 51 | export type WebsiteEnv = Cloudflare.InferEnv<typeof Website>; |
| 52 | ``` |
| 53 | |
| 54 | ## Add the Worker entry |
| 55 | |
| 56 | The default entry is `worker/index.ts`. Set `main` to use a different path. |
| 57 | |
| 58 | ```typescript |
| 59 | // worker/index.ts |
| 60 | import handler from "vinext/server/fetch-handler"; |
| 61 | import type { WebsiteEnv } from "../alchemy.run"; |
| 62 | |
| 63 | export default { |
| 64 | fetch(request: Request, env: WebsiteEnv, ctx: ExecutionContext) { |
| 65 | return handler.fetch(request, env, ctx); |
| 66 | }, |
| 67 | }; |
| 68 | ``` |
| 69 | |
| 70 | This Worker entry is Cloudflare-specific. The other platforms generate a |
| 71 | production server or Lambda entry instead. |
| 72 | |
| 73 | ## Add it to the Stack |
| 74 | |
| 75 | ```typescript |
| 76 | import * as Alchemy from "alchemy"; |
| 77 | import * as Effect from "effect/Effect"; |
| 78 | |
| 79 | export default Alchemy.Stack( |
| 80 | "MyVinextSite", |
| 81 | { providers: Cloudflare.providers(), state: Cloudflare.state() }, |
| 82 | Effect.gen(function* () { |
| 83 | const site = yield* Website; |
| 84 | return { url: site.url }; |
| 85 | }), |
| 86 | ); |
| 87 | ``` |
| 88 | |
| 89 | ## Add environment variables |
| 90 | |
| 91 | ```typescript |
| 92 | export const Website = Cloudflare.Website.Vinext("Website", { |
| 93 | env: { GREETING: "Hello from vinext on Cloudflare!" }, |
| 94 | }); |
| 95 | ``` |
| 96 | |
| 97 | Server components, route handlers, and server actions read bindings through |
| 98 | `import { env } from "cloudflare:workers"`. Public framework variables can be |
| 99 | compiled into browser assets; keep secrets out of public keys. |
| 100 | |
| 101 | ## Configure the data cache |
| 102 | |
| 103 | Alchemy provisions `VINEXT_KV_CACHE`, enables Workers Cache, and binds |
| 104 | `CF_VERSION_METADATA`. Keep these managed bindings out of `env`. |
| 105 | |
| 106 | The resource automatically injects the KV adapter for ISR and `"use cache"`. Deployment uploads prerendered HTML and React Server |
| 107 | Components payloads into KV. During prerendering, where the binding is absent, |
| 108 | the adapter falls back to memory. Redis and S3 adapters are for other platforms. |
| 109 | |
| 110 | ## Local development |
| 111 | |
| 112 | ```sh |
| 113 | bun alchemy dev |
| 114 | ``` |
| 115 | |
| 116 | The Website runs the native Vite development server with HMR and local Worker |
| 117 | bindings. Other resources declared in the Stack retain their own provider |
| 118 | behavior. Apply `.pipe(Alchemy.remote())` to deploy the Website during development. |
| 119 | |
| 120 | ## Custom domains |
| 121 | |
| 122 | ```typescript |
| 123 | const site = yield* Cloudflare.Website.Vinext("Web", { |
| 124 | domain: "app.example.com", |
| 125 | }); |
| 126 | ``` |
| 127 | |
| 128 | Use a zone managed by your Cloudflare account; see |
| 129 | [Frontends](/cloudflare/frontend/frontends) for shared Website options. |
| 130 | |
| 131 | ## Where next |
| 132 | |
| 133 | - [vinext API reference](/providers/cloudflare/website/vinext). |
| 134 | - [Runnable example](https://github.com/alchemy-run/alchemy/tree/main/examples/cloudflare-website-vinext), including server actions, D1, and KV. |
| 135 | - [vinext on AWS](/aws/frontend/vinext), [Fly](/fly/frontend/vinext), |
| 136 | [Hetzner](/hetzner/frontend/vinext), [Railway](/railway/frontend/vinext), and |
| 137 | [Prisma](/prisma/frontend/vinext). |