Skip to content
TypeScript
20.4 KB
Raw Blame
---
title: Add a React SPA
description: Ship a React single-page app from the same Stack as your Worker — built with Vite and deployed to Cloudflare in one command.
sidebar:
  label: React SPA
  order: 4
---

import { Code, Tabs, TabItem } from "@astrojs/starlight/components";
import PackageManagers from "../../../../components/PackageManagers.astro";

export const deps = `react react-dom`;
export const devDeps = `@types/react @types/react-dom @vitejs/plugin-react`;
export const typesDep = `@cloudflare/workers-types`;

This is the walkthrough for a plain React single-page app on
[`Cloudflare.Website.Vite`](/cloudflare/frontend/vite): build the
client assets, ship them to Cloudflare, and serve them through a
Worker so your frontend and backend share one URL surface and one
deploy. For the resource itself — what it does, its props, `memo`
semantics, and dev mode — see the
[Vite resource page](/cloudflare/frontend/vite).

Pick the path that fits where you're starting from:

1. [Set it up manually](#set-it-up-manually) — start from an
   empty project; the bare minimum is an `index.html` and an
   entry module.
2. [Use the `create-vite` template](#use-the-create-vite-template)
   — start from Vite's official React + TS scaffold.
3. [Deploy an existing Vite project](#deploy-an-existing-vite-project)
   — point Alchemy at a SPA you already have.

## Set it up manually

The bare minimum is **an `index.html` with a script tag** — no
`package.json` `build` script, no `main` entry; Alchemy runs Vite
for you, merging its Cloudflare integration on top of your
`vite.config.ts`.

A minimal React SPA looks like:

```
.
├── index.html          # entry HTML, references the client bundle
├── vite.config.ts      # your plugins — react() here
└── src/
    └── main.tsx        # client entry imported by index.html
```

### Install React

<PackageManagers add={deps} dev={devDeps} />

### Create vite.config.ts

Register the React plugin — Alchemy runs Vite with your config and
its plugins intact:

```typescript
// vite.config.ts
import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";

export default defineConfig({
  plugins: [react()],
});
```

### Create index.html

`index.html` just needs to load your entry module:

```html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>My App</title>
  </head>
  <body>
    <div id="root"></div>
    <script type="module" src="/src/main.tsx"></script>
  </body>
</html>
```

:::caution[index.html must be at rootDir]
Vite resolves `index.html` relative to its project root. Alchemy's
`rootDir` defaults to `process.cwd()` — so place `index.html`
next to your `alchemy.run.ts`, or set `rootDir` to the folder
that contains it. See Vite's
[project structure docs](https://vite.dev/guide/#index-html-and-project-root).
:::

### Create src/main.tsx

Mount a React component into the `#root` div:

```tsx
// src/main.tsx
import React from "react";
import ReactDOM from "react-dom/client";

function App() {
  return (
    <main>
      <h1>Hello from Alchemy + Vite</h1>
      <p>Edit <code>src/main.tsx</code> and redeploy.</p>
    </main>
  );
}

ReactDOM.createRoot(document.getElementById("root")!).render(
  <React.StrictMode>
    <App />
  </React.StrictMode>,
);
```

### Add it to the Stack

Yield `Cloudflare.Website.Vite("Website")` from your Stack:

```diff lang="typescript"
// alchemy.run.ts
import * as Alchemy from "alchemy";
import * as Cloudflare from "@alchemy.run/cloudflare";
import * as Effect from "effect/Effect";
import Worker from "./src/worker.ts";

export default Alchemy.Stack(
  "MyApp",
  {
    providers: Cloudflare.providers(),
    state: Cloudflare.state(),
  },
  Effect.gen(function* () {
    const worker = yield* Worker;
+    const web = yield* Cloudflare.Website.Vite("Website");
    return {
      url: worker.url,
+      webUrl: web.url,
    };
  }),
);
```

### Configure the deep-link fallback

If your SPA uses client-side routing (React Router, TanStack
Router), configure the not-found fallback explicitly so a deep
link like `/about` serves `index.html` instead of a 404 and your
router handles the path on the client:

```diff lang="typescript"
// alchemy.run.ts
-const web = yield* Cloudflare.Website.Vite("Website");
+const web = yield* Cloudflare.Website.Vite("Website", {
+  assets: {
+    notFoundHandling: "single-page-application",
+  },
+});
```

`assets` accepts the standard Cloudflare Worker static-assets
config; `single-page-application` not-found handling serves
`index.html` for unmatched paths so the client router takes over.
A site that ships a real error page uses
`notFoundHandling: "404-page"` instead (Cloudflare serves the
built `404.html`).

### Inject the Worker URL at build time

A pure SPA needs the backend's URL baked into the JS bundle at
build time — there's no server to resolve it on each request. Pass
`env` to `Cloudflare.Website.Vite` and any `VITE_`-prefixed entry is
inlined as `import.meta.env.<KEY>` during the Vite build, the same
as if you'd run `VITE_API_URL=https://… vite build`:

```diff lang="typescript"
// alchemy.run.ts
const worker = yield* Worker;
const web = yield* Cloudflare.Website.Vite("Website", {
+  env: {
+    VITE_API_URL: worker.url.as<string>(),
+  },
});
```

Then read it from the SPA:

```tsx
// src/main.tsx
const apiUrl = import.meta.env.VITE_API_URL;

await fetch(`${apiUrl}/api/hello`);
```

Only `VITE_`-prefixed keys are inlined into the bundle, and
`Output` values like `worker.url.as<string>()` resolve at deploy
time before the build runs. For the full semantics — `Redacted`
unwrapping, what happens to non-prefixed keys, redeploys on
env-only changes — see the
[Vite resource's environment section](/cloudflare/frontend/vite#environment).

To inject the site's *own* URL — for canonical links, OG tags, or
OAuth redirect URIs — use `Cloudflare.Worker.URL`. A site can't
reference its own `url` Output, so Alchemy resolves the URL before
the build and inlines it the same way:

```diff lang="typescript"
const web = yield* Cloudflare.Website.Vite("Website", {
  env: {
    VITE_API_URL: worker.url.as<string>(),
+    VITE_PUBLIC_URL: Cloudflare.Worker.URL,
  },
});
```

## Use the create-vite template

If you'd rather start from a real framework scaffold (React + TS
with HMR, ESLint, etc.), use Vite's official template:

<Tabs syncKey="pkgManager">
  <TabItem label="pnpm" icon="pnpm">
    <Code code={`pnpm create vite web --template react-ts\ncd web && pnpm install && cd ..`} lang="sh" />
  </TabItem>
  <TabItem label="bun" icon="bun">
    <Code code={`bun create vite@latest web -- --template react-ts\ncd web && bun install && cd ..`} lang="sh" />
  </TabItem>
  <TabItem label="npm" icon="npm">
    <Code code={`npm create vite@latest web -- --template react-ts\ncd web && npm install && cd ..`} lang="sh" />
  </TabItem>
  <TabItem label="yarn" icon="seti:yarn">
    <Code code={`yarn create vite web --template react-ts\ncd web && yarn install && cd ..`} lang="sh" />
  </TabItem>
</Tabs>

That drops a complete project into `./web/` with its own
`package.json`, `tsconfig.json`, and `vite.config.ts`.

### Point Cloudflare.Website.Vite at the subfolder

Since the SPA isn't at the project root, set `rootDir`:

```diff lang="typescript"
// alchemy.run.ts
Effect.gen(function* () {
  const worker = yield* Worker;
+  const web = yield* Cloudflare.Website.Vite("Website", {
+    rootDir: "./web",
+  });
  return {
    url: worker.url,
    webUrl: web.url,
  };
}),
```

`rootDir` defaults to `process.cwd()`, so you only set it when
your `index.html` isn't next to `alchemy.run.ts`.

## Deploy an existing Vite project

Already have a Vite SPA? Point `Cloudflare.Website.Vite` at it with
`rootDir` and you're done:

```ts
// alchemy.run.ts
const web = yield* Cloudflare.Website.Vite("Website", {
  rootDir: "./path/to/your/spa",
});
```

Your existing `vite.config.ts`, plugins, aliases, and `tsconfig`
are all preserved — Alchemy merges its Cloudflare integration on
top of your config (the [Vite resource page](/cloudflare/frontend/vite)
covers how). One thing to check first:

:::caution[Remove @cloudflare/vite-plugin if present]
Alchemy ships its own Cloudflare integration and is **not
compatible** with `@cloudflare/vite-plugin` — remove it from your
`vite.config.ts`:

```diff lang="typescript"
// vite.config.ts
import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
-import cloudflare from "@cloudflare/vite-plugin";

export default defineConfig({
-  plugins: [react(), cloudflare()],
+  plugins: [react()],
});
```
:::

## Deploy

```sh
bun alchemy deploy
```

Alchemy runs Vite on `rootDir`, uploads the assets, creates a
Worker that serves them, and prints the new `webUrl` stack output:

```
{
  url: "https://myapp-worker-dev-you.workers.dev",
  webUrl: "https://myapp-web-dev-you.workers.dev",
}
```

## Verify

Hit it with `curl`:

```sh
curl -s https://myapp-web-dev-you.workers.dev | head -5
# <!doctype html>
# <html lang="en">
#   <head>
#     <meta charset="UTF-8" />
#     ...
```

Or add an integration test alongside the existing ones:

```diff lang="typescript"
// test/integ.test.ts
import * as Cloudflare from "@alchemy.run/cloudflare";
import * as Test from "@alchemy.run/test/Bun";
import { expect } from "bun:test";
import * as Effect from "effect/Effect";
import * as HttpClient from "effect/http/HttpClient";
import Stack from "../alchemy.run.ts";

const { test, beforeAll, deploy } = Test.make({
  providers: Cloudflare.providers(),
  state: Cloudflare.state(),
});

const stack = beforeAll(deploy(Stack));

+test(
+  "Web SPA serves index.html",
+  Effect.gen(function* () {
+    const { webUrl } = yield* stack;
+
+    const response = yield* HttpClient.get(webUrl);
+    expect(response.status).toBe(200);
+    expect(yield* response.text).toContain("<!doctype html>");
+  }),
+);
```

## Local dev

```sh
bun alchemy dev
```

`alchemy dev` runs the whole Stack locally with hot reloading.
For a `Cloudflare.Website.Vite` worker that means Alchemy boots
Vite's dev server (HMR, instant module updates) behind Alchemy's
local proxy; each Worker gets its own local URL:

```
{
  url: "http://localhost:1337",
  webUrl: "http://localhost:1338",
}
```

Edit `src/main.tsx`, save, and the browser updates without a full
reload. Backend resources (R2 buckets, D1 databases, service
bindings) are the **real** cloud resources — only your application
code runs locally, so there's no emulation fidelity gap.

The build-time env injection works the same in dev — `VITE_`-prefixed
entries are inlined as `import.meta.env.<KEY>`, and `Output` values
like `worker.url.as<string>()` resolve before the dev server starts.

By default the dev server picks an available port. Set `dev.port`
only if you want the local URL to stay the same across runs:

```diff lang="typescript"
// alchemy.run.ts
const web = yield* Cloudflare.Website.Vite("Website", {
+  dev: { port: 3000 },
});
```

See [Local Development](/environments/local-development) for the full
model — how Workers run in `workerd`, how to attach a debugger, and
how `dev` differs from `deploy`.

## Use a different framework

This page is the plain React SPA path. Other frameworks have
their own landing pages, each flowing from the same
[`Cloudflare.Website.Vite` resource](/cloudflare/frontend/vite):

- [TanStack Start](/cloudflare/frontend/tanstack-start) — full-stack
  React with server routes and runtime bindings
- [React Router](/cloudflare/frontend/react-router) — SPA or React
  Server Components
- [Vue](/cloudflare/frontend/vue) — Vue 3 SPA
- [Foldkit](/cloudflare/frontend/foldkit) — client-only,
  server-rendered, or prerendered Foldkit
- [SolidStart](/cloudflare/frontend/solidstart) — SSR Solid

For frameworks whose build isn't driven by Vite alone, deploy the
built output with
[`Cloudflare.Website.StaticSite`](/cloudflare/frontend/static-site).
The [Frontend overview](/cloudflare/frontend/frontends) lays out
which resource fits which framework.

## Add a backend resource

Most SPAs need to talk to a real backing service — a bucket for
uploads, a database for state. Bindings give your Worker a typed
handle to those resources.

The walkthrough below uses a TanStack Start route handler because
a pure SPA only ships static assets — there's no server-side `env`
to call. The same `env` map and the same three call patterns work
in any Vite framework that exposes server routes (TanStack Start,
SolidStart, React Router).

Start with the simplest backend: an R2 bucket.

```typescript
// src/backend.ts
import * as Cloudflare from "@alchemy.run/cloudflare";

export const Bucket = Cloudflare.R2.Bucket("Bucket");
```

`Cloudflare.R2.Bucket("Bucket")` is a description, not a deploy.
Alchemy provisions the real bucket on the next run as soon as
something binds to it.

## Bind the bucket to your worker

Add the bucket to the Vite worker's `bindings` map:

```diff lang="typescript"
// alchemy.run.ts
+import { Bucket } from "./src/backend.ts";

const web = yield* Cloudflare.Website.Vite("Website", {
+  env: {
+    BUCKET: Bucket,
+  },
});
```

The key (`BUCKET`) is the field name on `env`; the value identifies
the resource. `env.BUCKET` is now typed as `Bucket` end to end.

## Pull in Cloudflare's binding types

`InferEnv` maps each binding to a Cloudflare runtime type
(`Bucket`, `Service`, `Namespace`, …) — those types live in
`@cloudflare/workers-types`. Add it to your `tsconfig.json` so the
TypeScript checker can find them:

<PackageManagers dev={typesDep} />

```diff lang="json"
// tsconfig.json
{
  "compilerOptions": {
    "types": [
      "bun",
-      "vite/client"
+      "vite/client",
+      "@cloudflare/workers-types"
    ]
  }
}
```

Without this you'll see `Cannot find name 'Bucket'` (or `Service`,
etc.) the moment you reference `env.BUCKET` inside a route handler.

## Option 1 — call the binding directly

Inside a server route, `env.BUCKET` is the standard Cloudflare R2
client. Use it as a regular `async`/`await` API:

```typescript
// src/routes/api.hello.ts
import { createFileRoute } from "@tanstack/react-router";
import { env } from "../env.ts";

export const Route = createFileRoute("/api/hello")({
  server: {
    handlers: {
      GET: async ({ request }) => {
        const key = new URL(request.url).searchParams.get("key");
        if (!key) return new Response("Missing 'key'", { status: 400 });

        // option 1 — use the async binding directly
        const object = await env.BUCKET.get(key);
        if (!object) return new Response("Not found", { status: 404 });
        return new Response(object.body);
      },
    },
  },
});
```

This is the most direct path — no extra workers, no extra code.
Reach for it when the route handler owns all the logic itself.

## Add an Effect-native Worker

When the work is shared across routes, deserves typed errors, or
needs Effect-native primitives (retries, layers, structured
concurrency), factor it into its own Worker:

```diff lang="typescript"
// src/backend.ts
import * as Cloudflare from "@alchemy.run/cloudflare";
+import * as Effect from "effect/Effect";
+import { HttpServerRequest } from "effect/http/HttpServerRequest";
+import * as HttpServerResponse from "effect/http/HttpServerResponse";

export const Bucket = Cloudflare.R2.Bucket("Bucket");
+
+export default class Backend extends Cloudflare.Worker<Backend>()(
+  "Backend",
+  { main: import.meta.url },
+  Effect.gen(function* () {
+    const bucket = yield* Cloudflare.R2.ReadWriteBucket(Bucket);
+
+    return {
+      hello: Effect.fn("Backend.hello")(function* (key: string) {
+        const object = yield* bucket.get(key);
+        if (object === null) return null;
+        return yield* object.text();
+      }),
+      fetch: Effect.gen(function* () {
+        const request = yield* HttpServerRequest;
+        const key = new URL(request.url, "http://x").searchParams.get("key");
+        if (!key) {
+          return HttpServerResponse.text("Missing 'key'", { status: 400 });
+        }
+        const object = yield* bucket.get(key);
+        if (object === null) {
+          return HttpServerResponse.text("Not found", { status: 404 });
+        }
+        return HttpServerResponse.stream(object.body);
+      }),
+    };
+  }).pipe(Effect.provide(Cloudflare.R2.ReadWriteBucketBinding)),
+) {}
```

`hello` is a typed RPC method — any worker bound to `Backend` can
call it across worker boundaries. `fetch` is the standard HTTP
handler. The R2 binding lives once on the Backend; the Vite worker
no longer needs to bind it directly unless it also wants to call
R2 itself.

## Bind the Backend as a service

Add `Backend` next to (or instead of) `BUCKET` in the Vite worker's
`env`:

```diff lang="typescript"
// alchemy.run.ts
-import { Bucket } from "./src/backend.ts";
+import Backend, { Bucket } from "./src/backend.ts";

const web = yield* Cloudflare.Website.Vite("Website", {
  env: {
    BUCKET: Bucket,
+    BACKEND: Backend,
  },
});
```

`env.BACKEND` is a Cloudflare service binding, typed for the wire
shape Alchemy emits — Effect/Stream return values come back as
encoded envelopes (more on that in option 3).

## Option 2 — call the worker's `fetch`

`env.BACKEND.fetch(input, init?)` is the standard Cloudflare
service-binding `fetch`. Use it like any other HTTP service:

```diff lang="typescript"
// src/routes/api.hello.ts
      GET: async ({ request }) => {
        const key = new URL(request.url).searchParams.get("key");
        if (!key) return new Response("Missing 'key'", { status: 400 });

-        // option 1 — use the async binding directly
-        const object = await env.BUCKET.get(key);
-        if (!object) return new Response("Not found", { status: 404 });
-        return new Response(object.body);
+        // option 2 — call the worker's fetch handler
+        return env.BACKEND.fetch(
+          `https://backend/?key=${encodeURIComponent(key)}`,
+        );
      },
```

The Backend already handles the R2 lookup behind its own `fetch`,
so the response flows straight through. Reach for this when the
backend logic is HTTP-shaped (REST endpoints, streaming responses)
and you don't want to duplicate it in the route handler.

## Wrap the binding for typed RPC

Effect-native workers serialize their results as wire envelopes:
`Effect.fail` becomes an `RpcErrorEnvelope`, `Stream` becomes an
encoded `ReadableStream`. `toRpcAsync` is the consumer-side
wrapper that auto-decodes those envelopes into a clean `Promise<T>`
API:

```diff lang="typescript"
// src/routes/api.hello.ts
import { createFileRoute } from "@tanstack/react-router";
+import * as Cloudflare from "@alchemy.run/cloudflare/Bridge";
+import type Backend from "../backend.ts";
import { env } from "../env.ts";

+const backend = Cloudflare.toRpcAsync<Backend>(env.BACKEND);
```

Error envelopes are thrown so `await` rejects; stream envelopes
unwrap to their raw `ReadableStream<Uint8Array>` body.
`Service.fetch` and `Service.connect` pass through unchanged so the
returned proxy is still a usable `Service` binding.

## Option 3 — call the worker's RPC method

Now `hello` is a normal typed `await`:

```diff lang="typescript"
// src/routes/api.hello.ts
      GET: async ({ request }) => {
        const key = new URL(request.url).searchParams.get("key");
        if (!key) return new Response("Missing 'key'", { status: 400 });

-        // option 2 — call the worker's fetch handler
-        return env.BACKEND.fetch(
-          `https://backend/?key=${encodeURIComponent(key)}`,
-        );
+        // option 3 — call the worker's RPC method
+        const value = await backend.hello(key);
+        if (value === null) return new Response("Not found", { status: 404 });
+        return new Response(value);
      },
```

RPC keeps you off the HTTP detour — the call is a typed function,
not a request, and the return value is the value itself (no
`.json()`, no status codes to plumb).

`toRpcAsync` is the async-consumer face of
[Schemaless RPC](/apis/schemaless) — the same typed methods `Backend`
exposes, decoded into `Promise`s instead of Effects.

:::tip[Calling RPC from another Effect-native Worker?]
Use [`Cloudflare.Workers.bindWorker(Backend)`](/providers/cloudflare/workers/worker)
instead of `toRpcAsync`. It returns the same shape with `Effect`
return values, preserving typed errors and decoded streams instead
of throwing them away.
:::

Your app now ships a Worker backend and a Vite frontend from the
same `alchemy.run.ts`, deploying together with one command. Next
you'll [run a Container](/cloudflare/compute/run-a-container) so each
Durable Object instance has its own long-lived process for
executing untrusted code or running binaries.