Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 118 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,123 @@
Vercel's [v0 clone](https://github.com/vercel/v0-sdk/tree/main/examples/v0-clone)
— the v0 SDK's example app: a v0-style chat that builds apps through the
[v0 Platform API](https://v0.app/docs/api/platform), with a live preview served
from an isolated origin, Design Mode, file, ZIP and GitHub imports and
one-click deploys — as **two Cloudflare Workers** built by
[`@opennextjs/cloudflare`](https://opennext.js.org/cloudflare): the host app and
its preview proxy, each on its own origin, deployed per environment through
[Orun](https://github.com/sourceplane/orun), and packaged, once it is built, as
an **Orun baseline**.
from an isolated origin, Design Mode, file, ZIP and GitHub imports, version
restore and one-click deploys — running as **two Cloudflare Workers** built by
[`@opennextjs/cloudflare`](https://opennext.js.org/cloudflare): the host app
and its preview proxy, each on its own origin, deployed per environment
through [Orun](https://github.com/sourceplane/orun). It keeps no store of its
own: chats, files and previews are the v0 API's.

Being built in reviewed steps; the plan and its status are in
[`specs/epics/v0-clone`](specs/epics/v0-clone/README.md).
The v0 SDK and its example are © Vercel, Inc., under the Apache License 2.0
(`apps/web/LICENSE`, `apps/preview/LICENSE`). See
[`specs/epics/v0-clone`](specs/epics/v0-clone/README.md) for the design, the
milestones and the as-built status.

The v0 SDK and its example are © Vercel, Inc., under the Apache License 2.0.
> **Security warning — upstream's, read for Cloudflare.** The example has no
> user accounts. With `V0_API_KEY` stored, everyone who can reach the Workers
> shares that key's v0 workspace: they can read, create, change and delete
> chats, run generations at its expense and deploy with the Vercel token the
> key carries. Every server route passes through `authorizeProxyRequest`
> (`apps/web/lib/proxy.ts`, `apps/preview/lib/authorize.ts`), a same-origin
> check only — the seam where real session auth goes. Until it does, **leave
> `V0_API_KEY` unset** (each visitor then uses their own key, through the
> app's dialog) or put [Cloudflare Access](https://developers.cloudflare.com/cloudflare-one/applications/)
> in front of both Workers before you store one.

## What is here

| Surface | Where |
|---|---|
| The host app: the chat, the sidebar, the files, code and preview panes, the server routes over the v0 API | `apps/web` |
| The preview proxy: each chat's preview, fetched with the key and served into the host's iframe; the key route the host provisions | `apps/preview` |
| Where each app finds the other | `apps/web/lib/preview-proxy.ts`, `apps/preview/lib/origins.ts` |
| The v0 key (server, else the visitor's cookie) | `apps/web/lib/v0-client.ts`, `apps/preview/lib/v0-client.ts` |
| Workers build and deploy contract | each app's `next.config.mjs`, `open-next.config.ts`, `wrangler.jsonc`, `component.yaml` |
| Tests | `tests/web` (upstream's Design Mode tests, the host's routes), `tests/preview` (the key route, trusted hosts) |

## Getting started

```bash
pnpm install
pnpm typecheck
pnpm lint
pnpm test
pnpm build # next build + opennextjs-cloudflare build, for both apps
```

```bash
pnpm dev # both apps: the host on http://localhost:3000, the proxy on http://localhost:3001
```

Open the host app and save a v0 API key through its dialog
([v0.app → Settings → API keys](https://v0.app/chat/settings/keys); it needs
write access and generation credits). The key is validated, kept in an
HTTP-only cookie on the host's origin, and handed by your browser straight to
the preview proxy, which keeps its own copy in its own cookie. To serve the
built Workers instead: `pnpm --filter @app/preview preview` and
`pnpm --filter @app/web preview` (wrangler dev; `.dev.vars.example` in each
app).

## Connecting v0

Each app resolves its credential in this order: `V0_API_KEY`, then its own
cookie. Upstream's third, Vercel OIDC, has no Cloudflare equivalent.

- **Bring your own key (the default).** Leave `V0_API_KEY` unset. Every
visitor saves their own key through the dialog and spends their own credits.
- **One shared key.** Store it per environment as the Orun secret
`V0_API_KEY` and redeploy: both Workers' deploys push it as a Worker secret
(it never reaches a bundle), and the dialog turns read-only. Read the
security warning above first.

## How it maps to Vercel

| Vercel-side | Here |
|---|---|
| `apps/web`, a Vercel project | `apps/web`, the Worker `<repo>-web-<env>` (`.open-next/worker.js` + Static Assets) |
| `apps/preview-proxy`, a second project on another domain | `apps/preview`, the Worker `<repo>-preview-<env>` — its own hostname per environment |
| Related Projects telling each app the other's URL | each build is told the other's origin: `V0_PREVIEW_PROXY_URL` / `V0_CLONE_ORIGIN` from the environment when set (custom domains), else the sibling Worker's workers.dev URL (`component.yaml`'s `buildCommand`) |
| Vercel OIDC as the fallback credential | left out; `V0_API_KEY` (optional Orun secret → Worker secret) or the visitor's own key |
| Deployment Protection on both projects | Cloudflare Access on both Workers, before a shared key is stored |
| Deploy on push | Orun deploys the preview proxy, then the host, to stage → prod on merge to `main`, each smoke-tested |
| `bun test` | `vitest` in `tests/web` and `tests/preview` |

## Deploying

A merge to `main` deploys, per environment, `<repo>-preview-<env>` and then
`<repo>-web-<env>` (the host waits for its proxy), each checked by its
`smokeCommand`: the proxy's `/api/health`, then the host's page, its
`/api/health` and the proxy origin it was built with. Neither needs a v0 key
to pass.

**Deploy by hand:** Orunbase's Deploy button, or Actions → CI → Run workflow
on `main` with a component (`web`, `preview`, or empty for both) and an
environment, deploys exactly as a merge does.

### Custom domains

Attach a domain to each Worker in Cloudflare, then set the two origins under
the environment's `env:` in `intent.yaml` and redeploy both apps:

```yaml
env:
V0_PREVIEW_PROXY_URL: https://acme-previews.net # the host's iframe points here
V0_CLONE_ORIGIN: https://app.acme.com # the only origin the proxy lets provision its key
```

Both Workers' workers.dev hostnames share one registrable domain
(`<subdomain>.workers.dev`), so the preview is cross-origin to the host but
same-site with it. For production, give the preview proxy a domain on a
**different registrable domain** from the host's, as upstream recommends.

### Trusted preview hosts

The preview proxy registers its own hostname in the v0 team's trusted preview
hosts (at most five) the first time it is provisioned with a key or serves a
preview. Stage and prod take two; remove stale ones in v0's settings.

## CI

| Workflow | What it runs |
|---|---|
| `ci.yml` | the Orun pipeline: `orun plan --changed`, then one job per lane. A pull request verifies (both apps built and dry-deployed per environment, both test suites run); a merge deploys the preview proxy, then the host, stage → prod. It plans nothing while `intent.yaml` names the placeholder workspace `ws_CHANGE_ME`, as this repository does. A manual run (Orunbase's Deploy button, or Run workflow on `main`) plans the environment and components it names under the push-to-main trigger |
10 changes: 10 additions & 0 deletions apps/preview/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# apps/preview — the preview proxy

The v0 clone's preview origin (upstream's `apps/preview-proxy`): it fetches a
chat's preview from the v0 API with the key and serves it, path for path,
into the host app's iframe; `proxy.ts` keeps root-relative preview requests on
the chat's proxy path; `/api/api-key` lets only the host app's origin
provision this proxy's own cookie. Generated code runs here, never on the
host's origin. One Cloudflare Worker per environment (`<repo>-preview-<env>`).
Upstream: [vercel/v0-sdk `examples/v0-clone/apps/preview-proxy`](https://github.com/vercel/v0-sdk/tree/main/examples/v0-clone/apps/preview-proxy)
(Apache-2.0, `LICENSE`).
82 changes: 82 additions & 0 deletions apps/preview/component.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
apiVersion: sourceplane.io/v1
kind: Component
metadata:
name: preview
description: The v0 clone's preview proxy, on an origin of its own — it fetches each chat's preview with the v0 key and serves it into the host app's iframe (Next.js on Cloudflare Workers + Static Assets via @opennextjs/cloudflare, per environment)
spec:
type: cloudflare-workers-assets-turbo
lifecycle: production
owner: platform
# Its pages, attached to it in the workspace's catalog: what the Docs
# library shows for it, and what the catalog's docs and runbook checks read.
# Generated from what this manifest and its sources declare.
docs:
overview: docs/overview.md
pages:
- { path: docs/architecture.md, role: architecture, title: Architecture }
- { path: docs/runbook.md, role: runbook, title: Runbook }
domain: app
path: apps/preview
# Its own lane per environment, as the host's: a Next.js build each.
lane: isolated
# The same optional v0 key the host reads (apps/web/component.yaml): unset,
# the host app provisions each visitor's key into this proxy's own
# partitioned cookie; set, both Workers use it. A Worker SECRET, never in
# a bundle (buildCommand below).
optionalSecretEnv:
V0_API_KEY: "secret://v0-clone/v0-clone/{{ .environment }}/V0_API_KEY"
subscribe:
environments:
- name: dev
profile: verify
- name: stage
profile: verify
profileRules:
- profile: deploy
when:
triggerRef: github-push-main
- name: prod
profile: verify
profileRules:
- profile: deploy
when:
triggerRef: github-push-main
parameters:
nodeVersion: "22"
pnpmVersion: "10.12.1"

workspaceDir: ../..
appDir: .

turboFilter: "@app/preview"
wranglerConfig: wrangler.jsonc

workerEntrypoint: .open-next/worker.js
assetsDir: .open-next/assets

# Per-env worker name: v0-clone-preview-{dev,stage,prod} — a hostname of
# its own, so the preview is cross-origin to the host app.
workerName: v0-clone-preview
environmentAwareWorkerName: true

workersDevSubdomain: your-workers-subdomain

installCommand: pnpm install --frozen-lockfile
# The host app's origin, inlined into the build
# (NEXT_PUBLIC_V0_CLONE_ORIGIN, lib/origins.ts) — the only origin allowed
# to provision this proxy's key: the environment's V0_CLONE_ORIGIN when
# intent.yaml sets one (a custom domain), else the host Worker's
# workers.dev URL for this environment. The v0 key never reaches the
# build, and goes to the Worker as a SECRET when the environment has one,
# as the host's does.
buildCommand: env -u V0_API_KEY NEXT_PUBLIC_V0_CLONE_ORIGIN="${V0_CLONE_ORIGIN:-https://v0-clone-web-${ORUN_ENVIRONMENT}.your-workers-subdomain.workers.dev}" pnpm exec turbo run build --filter=@app/preview && node ../../tooling/wire/worker-secrets.mjs --secret V0_API_KEY --worker "v0-clone-preview-${ORUN_ENVIRONMENT}" --config wrangler.jsonc

# Smoke: /api/health answers as the preview proxy — which only this
# Worker can — naming the host app origin its build was given. Read-only,
# no v0 call; a preview itself needs a chat, which needs a key.
smokeCommand: "health=$(curl -sf \"${DEPLOYED_URL}/api/health\") && echo \"${health}\" | grep -q '\"app\":\"preview\"' && echo \"${health}\" | grep -q '\"ok\":true'"
labels:
team: platform
layer: delivery
surface: preview
runtime: cloudflare
20 changes: 20 additions & 0 deletions apps/preview/docs/architecture.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# preview — architecture

Built by the turbo pipeline from `apps/preview` and deployed with wrangler.

| | |
| ------------------- | ----------------------------------------------- |
| Worker config | `apps/preview/wrangler.jsonc` |
| Entry | `.open-next/worker.js` |
| Compatibility date | `2026-10-01` |
| Compatibility flags | `nodejs_compat`, `global_fetch_strictly_public` |

## Bindings

| Binding | Kind | Source |
| -------- | ------------- | ------------------- |
| `ASSETS` | Static assets | `.open-next/assets` |

## Secrets resolved by the lane (names only)

- `V0_API_KEY` (optional)
20 changes: 20 additions & 0 deletions apps/preview/docs/overview.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# preview

The v0 clone's preview proxy, on an origin of its own — it fetches each chat's preview with the v0 key and serves it into the host app's iframe (Next.js on Cloudflare Workers + Static Assets via @opennextjs/cloudflare, per environment)

A Cloudflare Worker with Static Assets (server rendering plus static files), deployed per environment by its CI lane.

| | |
| ------------ | ------------------------------------------------------------------------------------------------------------------ |
| Type | `cloudflare-workers-assets-turbo` |
| Source | `apps/preview` |
| Package | `@app/preview` |
| Environments | `dev`: `verify` · `stage`: `verify`, `deploy` on a push to `main` · `prod`: `verify`, `deploy` on a push to `main` |

## Depends on

- (none)

## Depended on by

- **web** — The v0 clone's host app — the chat UI, its server routes over the v0 Platform API and the preview iframe (Next.js on Cloudflare Workers + Static Assets via @opennextjs/cloudflare, per environment)
37 changes: 37 additions & 0 deletions apps/preview/docs/runbook.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# preview — runbook

## How it runs

Pull requests verify it; a merge to `main` deploys it to `stage`, `prod`.
CI plans what changed (`orun plan --changed`) and runs each lane with `orun run`;
the plan orders a component after everything it depends on.

## Local commands

```bash
pnpm --filter @app/preview dev
pnpm --filter @app/preview build
pnpm --filter @app/preview typecheck
pnpm --filter @app/preview lint
pnpm --filter @app/preview preview
```

## Verify

After a deploy the lane runs this smoke against the environment it
deployed (`ORUN_ENVIRONMENT`); `DEPLOYED_URL` overrides the URL:

```bash
health=$(curl -sf "${DEPLOYED_URL}/api/health") && echo "${health}" | grep -q '"app":"preview"' && echo "${health}" | grep -q '"ok":true'
```

## Rollback

Revert the commit on `main`. The next convergence applies the previous desired
state; nothing was changed out of band, so there is nothing else to undo.

## Common failures

- **The smoke fails right after a first deploy**: a new workers.dev route can
answer 4xx for a few seconds and the lane retries. A failure that persists
is a regression in the deployed code.
9 changes: 9 additions & 0 deletions apps/web/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# apps/web — the host app

The v0 clone's host: the chat UI, its server routes over the v0 Platform API
(`app/api/**`, each behind `authorizeProxyRequest`) and the preview iframe,
which points at the preview proxy (`../preview`) on its own origin. One
Cloudflare Worker per environment, built by `@opennextjs/cloudflare`
(`<repo>-web-<env>`). See the repository README for running, connecting v0
and deploying. Upstream: [vercel/v0-sdk `examples/v0-clone/apps/web`](https://github.com/vercel/v0-sdk/tree/main/examples/v0-clone/apps/web)
(Apache-2.0, `LICENSE`).
Loading
Loading