sailfork / t3code-github / .repos/ alchemy-effect/ website/ src/ content/ docs/ gcp/ frontend/ vite-spa.mdx

import { Code, Tabs, TabItem } from "@astrojs/starlight/components";
import PackageManagers from "../../../../components/PackageManagers.astro";
export const devDeps = "@alchemy.run/frontend-frameworks";
export const routerDep = "react-router-dom";

This walks a plain React single-page app onto Cloud Run with GCP.Website.Vite. Client-side routes work out of the box: unknown paths answer with index.html.

Install

Docker has to be running when you deploy.

Scaffold the app

Start from Vite's React + TypeScript template:

<Code code={`pnpm create vite web --template react-ts\ncd web && pnpm install && cd ..`} lang="sh" /> <Code code={`bun create vite@latest web -- --template react-ts\ncd web && bun install && cd ..`} lang="sh" /> <Code code={`npm create vite@latest web -- --template react-ts\ncd web && npm install && cd ..`} lang="sh" /> <Code code={`yarn create vite web --template react-ts\ncd web && yarn install && cd ..`} lang="sh" />

The app lands in ./web/ with its own vite.config.ts. Keep it as it is. You don't need an adapter.

Deploy the site

Point rootDir at the app:

// alchemy.run.ts
import * as Alchemy from "alchemy";
import * as GCP from "@alchemy.run/gcp";
import * as Effect from "effect/Effect";

export const Website = GCP.Website.Vite("Website", { rootDir: "./web" });

export default Alchemy.Stack(
  "MyReactApp",
  { providers: GCP.providers(), state: GCP.state() },
  Effect.gen(function* () {
    const site = yield* Website;
    return { url: site.url };
  }),
);
bun alchemy deploy

Add client-side routes

Install React Router in the app:

// web/src/main.tsx
import { StrictMode } from "react";
import { createRoot } from "react-dom/client";
import { BrowserRouter, Route, Routes } from "react-router-dom";
import App from "./App.tsx";

createRoot(document.getElementById("root")!).render(
  <StrictMode>
    <BrowserRouter>
      <Routes>
        <Route path="/" element={<App />} />
        <Route path="/about" element={<h1>About</h1>} />
      </Routes>
    </BrowserRouter>
  </StrictMode>,
);

Deploy again and open /about directly. The server answers with index.html and the router renders the page.

Call an API

Pass a Cloud Run service's URL to the build:

// alchemy.run.ts
Effect.gen(function* () {
+  const api = yield* Api;
-  const site = yield* Website;
+  const site = yield* GCP.Website.Vite("Website", {
+    rootDir: "./web",
+    env: { VITE_API_URL: api.uri },
+  });
  return { url: site.url };
}),

Read it in the app with import.meta.env.VITE_API_URL. The API needs CORS headers for the site's origin.

Local development

alchemy dev runs Vite's dev server with hot reload.

Where next

  • Vite — every option the resource takes.
  • Cloud Run — build the API the app calls.