The best way to control Claude Code, Codex and more from any computer or phone
| 1 | --- |
| 2 | title: React SPA |
| 3 | description: Deploy a React single-page app to Cloud Run with GCP.Website.Vite — scaffold with Vite, deep links answered by index.html, Vite's dev server under alchemy dev. |
| 4 | sidebar: |
| 5 | order: 3 |
| 6 | --- |
| 7 | |
| 8 | import { Code, Tabs, TabItem } from "@astrojs/starlight/components"; |
| 9 | import PackageManagers from "../../../../components/PackageManagers.astro"; |
| 10 | |
| 11 | export const devDeps = "@alchemy.run/frontend-frameworks"; |
| 12 | export const routerDep = "react-router-dom"; |
| 13 | |
| 14 | This walks a plain React single-page app onto Cloud Run with |
| 15 | [`GCP.Website.Vite`](/gcp/frontend/vite). Client-side routes work out |
| 16 | of the box: unknown paths answer with `index.html`. |
| 17 | |
| 18 | ## Install |
| 19 | |
| 20 | <PackageManagers dev={devDeps} /> |
| 21 | |
| 22 | Docker has to be running when you deploy. |
| 23 | |
| 24 | ## Scaffold the app |
| 25 | |
| 26 | Start from Vite's React + TypeScript template: |
| 27 | |
| 28 | <Tabs syncKey="pkgManager"> |
| 29 | <TabItem label="pnpm" icon="pnpm"> |
| 30 | <Code code={`pnpm create vite web --template react-ts\ncd web && pnpm install && cd ..`} lang="sh" /> |
| 31 | </TabItem> |
| 32 | <TabItem label="bun" icon="bun"> |
| 33 | <Code code={`bun create vite@latest web -- --template react-ts\ncd web && bun install && cd ..`} lang="sh" /> |
| 34 | </TabItem> |
| 35 | <TabItem label="npm" icon="npm"> |
| 36 | <Code code={`npm create vite@latest web -- --template react-ts\ncd web && npm install && cd ..`} lang="sh" /> |
| 37 | </TabItem> |
| 38 | <TabItem label="yarn" icon="seti:yarn"> |
| 39 | <Code code={`yarn create vite web --template react-ts\ncd web && yarn install && cd ..`} lang="sh" /> |
| 40 | </TabItem> |
| 41 | </Tabs> |
| 42 | |
| 43 | The app lands in `./web/` with its own `vite.config.ts`. Keep it as it |
| 44 | is. You don't need an adapter. |
| 45 | |
| 46 | ## Deploy the site |
| 47 | |
| 48 | Point `rootDir` at the app: |
| 49 | |
| 50 | ```typescript |
| 51 | // alchemy.run.ts |
| 52 | import * as Alchemy from "alchemy"; |
| 53 | import * as GCP from "@alchemy.run/gcp"; |
| 54 | import * as Effect from "effect/Effect"; |
| 55 | |
| 56 | export const Website = GCP.Website.Vite("Website", { rootDir: "./web" }); |
| 57 | |
| 58 | export default Alchemy.Stack( |
| 59 | "MyReactApp", |
| 60 | { providers: GCP.providers(), state: GCP.state() }, |
| 61 | Effect.gen(function* () { |
| 62 | const site = yield* Website; |
| 63 | return { url: site.url }; |
| 64 | }), |
| 65 | ); |
| 66 | ``` |
| 67 | |
| 68 | ```sh |
| 69 | bun alchemy deploy |
| 70 | ``` |
| 71 | |
| 72 | ## Add client-side routes |
| 73 | |
| 74 | Install React Router in the app: |
| 75 | |
| 76 | <PackageManagers add={routerDep} /> |
| 77 | |
| 78 | ```tsx |
| 79 | // web/src/main.tsx |
| 80 | import { StrictMode } from "react"; |
| 81 | import { createRoot } from "react-dom/client"; |
| 82 | import { BrowserRouter, Route, Routes } from "react-router-dom"; |
| 83 | import App from "./App.tsx"; |
| 84 | |
| 85 | createRoot(document.getElementById("root")!).render( |
| 86 | <StrictMode> |
| 87 | <BrowserRouter> |
| 88 | <Routes> |
| 89 | <Route path="/" element={<App />} /> |
| 90 | <Route path="/about" element={<h1>About</h1>} /> |
| 91 | </Routes> |
| 92 | </BrowserRouter> |
| 93 | </StrictMode>, |
| 94 | ); |
| 95 | ``` |
| 96 | |
| 97 | Deploy again and open `/about` directly. The server answers with |
| 98 | `index.html` and the router renders the page. |
| 99 | |
| 100 | ## Call an API |
| 101 | |
| 102 | Pass a Cloud Run service's URL to the build: |
| 103 | |
| 104 | ```diff lang="typescript" |
| 105 | // alchemy.run.ts |
| 106 | Effect.gen(function* () { |
| 107 | + const api = yield* Api; |
| 108 | - const site = yield* Website; |
| 109 | + const site = yield* GCP.Website.Vite("Website", { |
| 110 | + rootDir: "./web", |
| 111 | + env: { VITE_API_URL: api.uri }, |
| 112 | + }); |
| 113 | return { url: site.url }; |
| 114 | }), |
| 115 | ``` |
| 116 | |
| 117 | Read it in the app with `import.meta.env.VITE_API_URL`. The API needs |
| 118 | CORS headers for the site's origin. |
| 119 | |
| 120 | ## Local development |
| 121 | |
| 122 | `alchemy dev` runs Vite's dev server with hot reload. |
| 123 | |
| 124 | ## Where next |
| 125 | |
| 126 | - [Vite](/gcp/frontend/vite) — every option the resource takes. |
| 127 | - [Cloud Run](/gcp/compute/cloud-run) — build the API the app calls. |