From 0730e451688c7ca96089e18e618c80064c3f08fa Mon Sep 17 00:00:00 2001 From: Rahul Varghese Date: Thu, 15 Oct 2026 01:48:05 +0530 Subject: [PATCH] PS3: ready to instantiate The as-built status names the PRs, the departures from upstream (Workers KV behind the Upstash surface in lib/redis.ts, /admin force-dynamic, the Cloudflare Web Analytics beacon for Speed Insights, extractSubdomain exported, the root domain inlined at build from BASE_DOMAIN, the monorepo wrapper, the KV root's wiring keys), the pinned versions with their reasons, what was verified (wire/typecheck/lint/test/build locally, wrangler dev with a local KV through the create and delete flows, terraform validate, Checks on #2 and #3) and what was not (the KV apply, a live deploy and tenant hostnames on a real zone, which need the product linked to a workspace with a Cloudflare connection). The epic is completed; the README status reads Shipped. Orun-Task: PS-4 Co-Authored-By: Claude Fable 5.1 --- specs/epics/README.md | 2 +- .../IMPLEMENTATION-STATUS.md | 114 +++++++++++++++++- specs/epics/platforms-starter-kit/README.md | 4 +- specs/epics/platforms-starter-kit/epic.yaml | 2 +- .../implementation-plan.md | 8 +- 5 files changed, 117 insertions(+), 13 deletions(-) diff --git a/specs/epics/README.md b/specs/epics/README.md index 2c26419..9374c9e 100644 --- a/specs/epics/README.md +++ b/specs/epics/README.md @@ -7,6 +7,6 @@ folder is the adoption; the merge creates the epic in the workspace. | Epic | Cluster | Status | What it is | |------|---------|--------|------------| -| [`platforms-starter-kit/`](./platforms-starter-kit/) | **PS** | Draft | Vercel's Platforms Starter Kit, equivalent in code and function, on Cloudflare Workers with tenants in Workers KV | +| [`platforms-starter-kit/`](./platforms-starter-kit/) | **PS** | ✅ Shipped | Vercel's Platforms Starter Kit, equivalent in code and function, on Cloudflare Workers with tenants in Workers KV | As-built ≠ intent: what shipped lives in each epic's `IMPLEMENTATION-STATUS.md`. diff --git a/specs/epics/platforms-starter-kit/IMPLEMENTATION-STATUS.md b/specs/epics/platforms-starter-kit/IMPLEMENTATION-STATUS.md index 297f721..d4e0555 100644 --- a/specs/epics/platforms-starter-kit/IMPLEMENTATION-STATUS.md +++ b/specs/epics/platforms-starter-kit/IMPLEMENTATION-STATUS.md @@ -5,11 +5,115 @@ the code departed from `design.md` or from the upstream template. | Milestone | State | PR | |---|---|---| -| PS0 — the spec | | | -| PS1 — the platform on Workers | | | -| PS2 — the KV namespace from Terraform | | | -| PS3 — ready to instantiate | | | +| PS0 — the spec | ✅ Shipped | [#1](https://github.com/sourceplane/platforms-starter-kit/pull/1) | +| PS1 — the platform on Workers | ✅ Shipped | [#2](https://github.com/sourceplane/platforms-starter-kit/pull/2) | +| PS2 — the KV namespace from Terraform | ✅ Shipped | [#3](https://github.com/sourceplane/platforms-starter-kit/pull/3) | +| PS3 — ready to instantiate | ✅ Shipped | [#4](https://github.com/sourceplane/platforms-starter-kit/pull/4) | ## Departures from the upstream template -_None yet._ +### `lib/redis.ts` is Workers KV behind the Upstash client's surface + +Upstream: `new Redis({ url: KV_REST_API_URL, token: KV_REST_API_TOKEN })` +from `@upstash/redis`. Here: an object with `get / set / del / keys / mget` +over `getCloudflareContext().env.KV`, the same `subdomain:` keys and +JSON records. `keys()` accepts only `*` (KV lists by prefix) and +follows the list cursor; `mget` is `get` per key. `@upstash/redis` is gone +from the dependencies. `lib/subdomains.ts` and `app/actions.ts` are +byte-for-byte upstream. + +### `export const dynamic = 'force-dynamic'` on `app/admin/page.tsx` + +Upstream's admin page is rendered on demand only because every Upstash +command is a `cache: 'no-store'` fetch, which opts the route out of static +rendering. A KV binding is not a fetch, so without this line `next build` +would pre-render the page — with no binding, or with an empty list that +`revalidatePath` could never refresh through the read-only static-assets +cache. The build now reports `ƒ /admin`. + +### Cloudflare Web Analytics instead of `@vercel/speed-insights` + +`components/analytics.tsx` renders the beacon script when +`NEXT_PUBLIC_CF_BEACON_TOKEN` is set at build time; otherwise nothing. +`@vercel/speed-insights` and the unused `@vercel/analytics` are gone from +the dependencies; `app/layout.tsx` mounts `` +where `` was. + +### `extractSubdomain` is exported from `proxy.ts` + +One word (`export`) so `tests/web` can exercise the routing decision as a +pure function. The body is upstream's, Vercel preview branch included. + +### `NEXT_PUBLIC_ROOT_DOMAIN` is a build-time value, not a wrangler var + +`next build` inlines it into the server, proxy and client bundles (a probe +build left no `process.env.NEXT_PUBLIC_ROOT_DOMAIN` read in `.next/server`), +so the component's `buildCommand` sets it from the environment's +`BASE_DOMAIN` in `intent.yaml` and `wrangler.template.jsonc` carries no +copy. Locally it is `.env.local` for `next dev` and the code's +`localhost:3000` default for `wrangler dev` over a plain build. The Upstash +`KV_REST_API_*` variables the template read no longer exist. + +### `next.config.ts` + +`output: "standalone"` + `outputFileTracingRoot` (what `@opennextjs/cloudflare` +bundles), and `initOpenNextCloudflareForDev()` so `next dev` reaches the KV +binding declared in the rendered `wrangler.jsonc`. The upstream comment +block about Vercel Speed Insights headers went with the script. + +### The monorepo wrapper + +`package.json` is `@app/web` with the Workers build script; `next` is +pinned to `16.3.8` (below, Versions); `@cloudflare/workers-types` and +`wrangler` are dev dependencies; `cloudflare-env.d.ts` declares +`CloudflareEnv.KV`; `eslint.config.mjs` is the workspace's shared config +(upstream ships no lint setup). `tests/web/tsconfig.json` turns +`noUncheckedIndexedAccess` off because it compiles upstream's `proxy.ts`, +which indexes `split()` results. + +### `.gitignore`: `!.dev.vars.example` + +The skeleton's `.dev.vars.*` pattern swallowed the committed example; the +negation keeps it tracked while every real `.dev.vars` stays ignored. + +### The KV root's wiring keys + +The factory's copy of the baseline root emitted `app_kv_kv_id` / +`app_kv_kv_title` (a doubled `kv` from the rename of the baseline's +idempotency namespace). This repository's root emits `app_kv_id` / +`app_kv_title`, the names `wrangler.template.jsonc` and `wiring.fixture.json` +use, and its comments and locals speak of the tenant store. + +## Verified + +- Local: `pnpm run wire:fixture`, `pnpm typecheck`, `pnpm lint` (two + upstream `any` warnings, no errors), `pnpm test` (19 tests: the adapter + over a fake namespace with cursor paging, `lib/subdomains.ts`, + `extractSubdomain`, the proxy's rewrite and redirect, `cn`), `pnpm build` + green (`○ /`, `○ /_not-found`, `ƒ /admin`, `ƒ /s/[subdomain]`, proxy + bundled as Node.js middleware). +- `wrangler dev` over `.open-next/worker.js` with a local KV: `/` 200 + (landing), `Host: nope.localhost:3000` 404 (not-found page), `/admin` 200, + `/admin` on a tenant host 307 → `/`, CSS chunk 200. The create flow + driven in a browser (the emoji picker and the form need JavaScript; a + no-JS POST to the static landing page is served from the prerender cache) + wrote `subdomain:acme`, after which `Host: acme.localhost:3000` is 200 + with the emoji, `/s/acme` is 200 and `/admin` lists it; delete from + `/admin` emptied the namespace and the tenant host is 404 again, with + `revalidatePath('/admin')` a silent no-op. +- `terraform init -backend=false && terraform validate` green on + `infra/terraform/cloudflare-kv` (cloudflare 4.52.9, external 2.4.2). +- CI: `Checks` green on #2 and #3 (install, wire, typecheck, lint, test, + build). +- Not verified: the KV apply and a live deploy, which need the product + linked to an Orun workspace with a Cloudflare connection, and tenant + hostnames on a real zone (a wildcard record and a Worker route). + +## Versions + +| Package | Version | Why | +|---|---|---| +| next | 16.3.8 | newest inside `@opennextjs/cloudflare` 1.20.11's peer range; 16.4.x fails at worker boot (`preview-props.json` manifest) | +| @opennextjs/cloudflare | ^1.20.11 | static-assets incremental cache + `populateCache local`; `initOpenNextCloudflareForDev` | +| wrangler | ^4.151.0 | compatibility_date 2026-10-01; local KV for `wrangler dev` | +| @cloudflare/workers-types | ^5.20261014.1 | `KVNamespace` for `CloudflareEnv.KV` (the 4.x line is no longer published) | diff --git a/specs/epics/platforms-starter-kit/README.md b/specs/epics/platforms-starter-kit/README.md index 87a7c61..52d3b48 100644 --- a/specs/epics/platforms-starter-kit/README.md +++ b/specs/epics/platforms-starter-kit/README.md @@ -8,7 +8,7 @@ The upstream code stays the code: the proxy, the pages, the server actions, `lib | Field | Value | |-------|-------| -| Status | Draft | +| Status | ✅ Shipped — PS0–PS3 landed (#1, #2, #3, #4) | | Cluster | **PS** (PS0–PS3) | | Owner(s) | `apps/web` (the app) · `tests/web` (adapter and proxy tests) · `infra/terraform/cloudflare-kv` (the namespace) | | Builds on | `cirrus-baseline` conventions — the `cloudflare-kv` Terraform root and the `@@wiring(...)@@` per-environment render; intent, components, `checks.yml` gate, gated `ci.yml` | @@ -16,7 +16,7 @@ The upstream code stays the code: the proxy, the pages, the server actions, `lib | Changes | Adds the Workers build, the monorepo wrapper, `lib/redis.ts` over KV, tests, the KV root and the deploy contract; swaps Vercel Speed Insights for the Cloudflare Web Analytics beacon; makes `/admin` explicitly dynamic | | Decisions locked | (1) OpenNext on Workers + Static Assets. (2) Workers KV, not a Durable Object per tenant: the template's store is a key-value map with a prefix scan, and that is what KV is. (3) The adapter keeps the Upstash surface; app code is untouched. (4) `wrangler.jsonc` is rendered per environment from the wiring document; never committed. (5) The root domain is one knob — the environment's `BASE_DOMAIN` in `intent.yaml`, inlined by `next build`. | | Gate | PS1 is the first user-visible rung. PS2 needs a workspace with a Cloudflare connection to apply; it is verified by `terraform validate` and `plan`. | -| Shipped as | | +| Shipped as | `main` after [#4](https://github.com/sourceplane/platforms-starter-kit/pull/4); verified locally under `wrangler dev` with a local KV, `terraform validate` on the root, deploy gated until a workspace is linked | ## Read order diff --git a/specs/epics/platforms-starter-kit/epic.yaml b/specs/epics/platforms-starter-kit/epic.yaml index bfdfd34..99489e3 100644 --- a/specs/epics/platforms-starter-kit/epic.yaml +++ b/specs/epics/platforms-starter-kit/epic.yaml @@ -9,7 +9,7 @@ metadata: spec: title: "Vercel's Platforms Starter Kit on Cloudflare Workers, tenants in Workers KV" summary: "The Vercel platforms template — a multi-tenant app where every tenant is a subdomain routed by a Next.js proxy, created from a landing form with an emoji and managed from an admin page, with the tenant records in Upstash Redis — runs on Cloudflare Workers + Static Assets through @opennextjs/cloudflare with Workers KV standing in for Redis behind the same client surface, the namespace provisioned by Terraform through Orun and bound per environment, inside the Cirrus baseline's repository conventions. It is the Cloudflare-only multi-tenant port the coverage analysis ranks first." - state: backlog + state: completed milestones: - name: "PS0 — the spec" exitCriteria: diff --git a/specs/epics/platforms-starter-kit/implementation-plan.md b/specs/epics/platforms-starter-kit/implementation-plan.md index 98a7eef..c9f540d 100644 --- a/specs/epics/platforms-starter-kit/implementation-plan.md +++ b/specs/epics/platforms-starter-kit/implementation-plan.md @@ -3,7 +3,7 @@ Milestones land in order; each task is one pull request on an `orun/PS-n-` branch with an `Orun-Task: PS-n` trailer. -## PS0 — the spec +## PS0 — the spec ✅ This doc set, `epic.yaml` and the task contracts, merged to `main`. @@ -11,7 +11,7 @@ This doc set, `epic.yaml` and the task contracts, merged to `main`. - the five documents and `epic.yaml` are on `main` - `tasks/PS-1..PS-4.TaskContract.yaml` exist -## PS1 — the platform on Workers +## PS1 — the platform on Workers ✅ `apps/web` is the upstream template with the Workers build around it and `lib/redis.ts` speaking the Upstash surface over the `KV` binding; @@ -24,7 +24,7 @@ This doc set, `epic.yaml` and the task contracts, merged to `main`. - `wrangler dev` answers `/` with the landing page (200), an unknown tenant host (`Host: nope.localhost:3000`) with the not-found page (404), and after a tenant is created through the landing form, `Host: .localhost:3000` with the tenant page (200) and `/admin` with it listed - `/admin` is built as a dynamic route (`ƒ`), never pre-rendered -## PS2 — the KV namespace from Terraform +## PS2 — the KV namespace from Terraform ✅ `infra/terraform/cloudflare-kv` provisions a namespace per environment and publishes `{app_kv_id, app_kv_title}` as the `WIRING_CLOUDFLARE_KV` @@ -36,7 +36,7 @@ document; `apps/web/component.yaml` depends on it and declares the - the root's wiring keys match the template's tokens (`wire:fixture` renders the same keys) - the component's `secretEnv` names match the root's `secretOutputs` -## PS3 — ready to instantiate +## PS3 — ready to instantiate ✅ README (run, test, preview, deploy, the wildcard domain), `IMPLEMENTATION-STATUS.md` with PRs and departures, epic `completed`.