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
119 changes: 102 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,114 @@ Supabase's [Slack clone](https://github.com/supabase/supabase/tree/master/exampl
— the Next.js example of a realtime team chat: channels, messages that
arrive live over Supabase Realtime, email and password sign-up through
Supabase Auth, and members, moderators and admins whose role rides in every
session through an Auth hook and is enforced by row-level security — on its
way to **Cloudflare Workers + Static Assets** through
session through an Auth hook and is enforced by row-level security — running
on **Cloudflare Workers + Static Assets** through
[`@opennextjs/cloudflare`](https://opennext.js.org/cloudflare), with the
Supabase project provisioned by Terraform and its schema applied by
migrations through [Orun](https://github.com/sourceplane/orun), and then an
**Orun baseline**.
Supabase project itself provisioned by Terraform and its schema applied by
migrations through [Orun](https://github.com/sourceplane/orun), and packaged
as an **Orun baseline** in SL4: one command will take a new product from
nothing to live on stage and prod, in its own repository, under its own
name, in ONE landing.

Being built, one pull request per milestone. The plan, the design and the
as-built status are in
[`specs/epics/supabase-slack-clone`](specs/epics/supabase-slack-clone/README.md).
The example's code is the code. What the port changes is what the Workers
runtime and a fresh, unattended deployment force: `next` pinned (upstream
says `latest`) and the two calls Next 16 and supabase-js 2 no longer accept
fixed, the build inlining the project's URL and publishable key from the
same wrangler config the Worker runs with (`next.config.ts`), upstream's two
migrations and its seed applied by db-migrate instead of `supabase db push`,
the access-token hook `supabase/config.toml` switched on set by that runner
through the Management API, each project created with its Auth pointed at
the Worker and email confirmation off (so anyone can sign up on the first
deploy, with no SMTP server), and a read-only `/api/health` only the Worker
can answer. What the baseline adds (SL4) is everything needed to make a copy
of it somebody else's — built the way
[supabase-starter](https://github.com/sourceplane/supabase-starter) builds a
product, in one landing. See
[`specs/epics/supabase-slack-clone`](specs/epics/supabase-slack-clone/README.md)
for the design, the milestones and the as-built status, including every
departure.

| Milestone | What it lands |
## What is here (the product)

| Surface | Where |
|---|---|
| The product's name | `apps/web/lib/site.ts` |
| Sign-in and sign-up, the channel pages, `/api/health` | `apps/web/pages` |
| The Supabase client, the data hooks and the Realtime subscriptions | `apps/web/lib/Store.js` |
| Workers build and deploy contract | `apps/web/next.config.ts`, `open-next.config.ts`, `wrangler.template.jsonc` → `wrangler.jsonc` (committed), `component.yaml` |
| The Supabase project (stage, prod), its Auth settings | `infra/terraform/supabase` |
| The schema — upstream's two migrations and its seed — the access-token hook's Auth config, and the runner | `packages/db`, applied by `infra/db-migrate` |
| Tests | `tests/web` (vitest), `tests/db` (vitest on PGlite) |

## Getting started

```bash
pnpm install
pnpm run wire:fixture # re-render apps/web/wrangler.jsonc from its template (committed; this changes nothing)
pnpm typecheck
pnpm lint
pnpm test
pnpm build # next build + opennextjs-cloudflare build → apps/web/.open-next
```

The build needs no Supabase project: without one it inlines the committed
`wrangler.jsonc`'s placeholder values, and nothing calls them.

## Running it locally

Against any Supabase project with the schema applied (its URL and
publishable key are in the dashboard → Project Settings → API):

1. Copy `apps/web/.env.example` to `apps/web/.env.local` and fill in the two
values; `next dev` and `pnpm build` read it. Then
`pnpm --filter @app/web dev` (http://localhost:3000).
2. Or the built Worker: the same two values in `apps/web/.dev.vars` (copy
`.dev.vars.example`), `pnpm build`, then `pnpm --filter @app/web preview`
(http://localhost:8787).

The schema comes from the runner the pipeline uses:
`pnpm --filter @saas/db build`, then
`SUPABASE_ACCESS_TOKEN=… SUPABASE_PROJECT_REF=… node packages/db/dist/runner/cli.js apply --env stage`
— every migration the project has not had, then the access-token hook.

## How it deploys

Merges to `main` converge through Orun (`.github/workflows/ci.yml`), per
environment in a fixed order — the Supabase project, then the migrations,
then the Worker — every edge enforced on a push to main and advisory on a
pull request. `stage` deploys first and `prod` once stage has converged;
`dev` only verifies. Each deploy is smoke-tested over HTTP, `/api/health`
included. The pipeline plans nothing while `intent.yaml` names the
placeholder workspace `ws_CHANGE_ME`, as this repository does.

## How it maps to Vercel and Supabase

| Upstream (Vercel + a Supabase project) | Here |
|---|---|
| SL0 — the spec | intent, CI, tooling, this doc set and the task contracts |
| SL1 — the chat app on Workers | `apps/web` built by OpenNext, `tests/web` |
| SL2 — the Supabase project and the schema | `infra/terraform/supabase`, `packages/db`, `infra/db-migrate`, `tests/db` |
| SL3 — ready to instantiate | the README, the as-built status |
| SL4 — a baseline | the card, the build document, the bootstrap, the tests |
| Next.js `latest` on Vercel, the pages router | Next 16.3.8, pinned, built by OpenNext into one Cloudflare Worker (`.open-next/worker.js`) + Static Assets |
| "Deploy with Vercel" and the Supabase integration, which create the project and set its env vars | `orun new --blueprint repo-blueprint.yaml` — a new repository, branded; `infra/terraform/supabase` creates one project per environment and publishes its URL and keys as orun secrets |
| `NEXT_PUBLIC_SUPABASE_URL` / `_PUBLISHABLE_KEY` in the Vercel project | wrangler vars rendered per environment from the supabase root's `WIRING_SUPABASE`; `next build` inlines the same two from that config (`next.config.ts`) |
| `supabase db push` of `supabase/migrations`, and `seed.sql` | `packages/db/src/migrations` — `010_init`, `020_auth_hook`, `030_seed`, byte for byte — applied by the db-migrate component on every merge, before the app deploys |
| `[auth.hook.custom_access_token]` in `supabase/config.toml`, pushed with `supabase config push` | `packages/db/src/auth-config.ts`, set in the project's Auth by the same runner after the migrations (`PATCH /v1/projects/{ref}/config/auth`), so Auth never points at a function that does not exist yet |
| `site_url` and `enable_confirmations = true` in `config.toml` | The apply sets the Site URL and redirect allow-list to the Worker, and email confirmation off until the project has SMTP of its own (`authEmailConfirmation`), because the built-in email reaches only the organization's members |
| Preview branches, each with its own Supabase branch | A `dev` environment that verifies every pull request (the app built and dry-deployed, the migrations' checksums verified offline); `stage` and `prod` each have a project |

The publishable (anon) key is a wrangler var, not a secret: it is shipped to
every browser by design, and row-level security is the boundary. No
service-role key is read anywhere.

## CI

| Workflow | What it runs | Ships to a product |
|---|---|---|
| `ci.yml` | the Orun pipeline, and the only workflow a product carries: `orun plan --changed`, then one job per lane. A pull request runs the verify lanes (the app built and dry-deployed per environment, the Supabase root planned, the migrations planned offline; no typecheck, lint or tests), except the bootstrap's own landings (`orun/BASE-*`), where `plan` alone runs and nothing waits for it — the landing was checked in the build box; a merge applies, migrates and deploys 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, so they deploy as a merge deploys them | yes |
| `tag.yml` | cuts a `baseline-vN` tag (manual) | no |

## Licence and credit

The app will be Supabase's [Slack clone example](https://github.com/supabase/supabase/tree/master/examples/slack-clone/nextjs-slack-clone),
The app is Supabase's [Slack clone example](https://github.com/supabase/supabase/tree/master/examples/slack-clone/nextjs-slack-clone),
by [Supabase](https://supabase.com). Its own `package.json` says MIT; the
[supabase/supabase](https://github.com/supabase/supabase) repository it lives
in is licensed under the Apache License 2.0, which `apps/web/LICENSE` will
carry.
in is licensed under the Apache License 2.0, which `apps/web/LICENSE` carries.
The migrations under `packages/db/src/migrations` are the example's own SQL,
under the same terms.
82 changes: 76 additions & 6 deletions specs/epics/supabase-slack-clone/IMPLEMENTATION-STATUS.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,37 @@ at `9eaae4cd2cf8e67f9aeadab044e9a14429331c13`.

| Milestone | Pull request | State |
|---|---|---|
| SL0 — the spec | [#1](https://github.com/sourceplane/supabase-slack-clone/pull/1) | in review |
| SL1 — the chat app on Workers | [#2](https://github.com/sourceplane/supabase-slack-clone/pull/2) | planned |
| SL2 — the Supabase project and the schema | [#3](https://github.com/sourceplane/supabase-slack-clone/pull/3) | planned |
| SL3 — ready to instantiate | [#4](https://github.com/sourceplane/supabase-slack-clone/pull/4) | planned |
| SL0 — the spec | [#1](https://github.com/sourceplane/supabase-slack-clone/pull/1) | ✅ merged |
| SL1 — the chat app on Workers | [#2](https://github.com/sourceplane/supabase-slack-clone/pull/2) | ✅ merged |
| SL2 — the Supabase project and the schema | [#3](https://github.com/sourceplane/supabase-slack-clone/pull/3) | ✅ merged |
| SL3 — ready to instantiate | [#4](https://github.com/sourceplane/supabase-slack-clone/pull/4) | in review |
| SL4 — a baseline | [#5](https://github.com/sourceplane/supabase-slack-clone/pull/5) | planned |

Nothing is built yet. Every departure from upstream is recorded here as
it lands.
The app builds and its tests pass; the migrations apply on a
Supabase-shaped Postgres in process. Nothing has been applied against a real
Supabase organization (risk SL-A); the baseline that bootstraps a product,
and the tiers that prove it end to end, are SL4.

## Departures from upstream

Every change to the example's own code, and why. Everything else under
`apps/web` — `lib/Store.js`, `lib/UserContext.js`, the components, the
styles, `public/`, the Tailwind and PostCSS config — is upstream's, byte for
byte.

| Where | Upstream | Here | Why |
|---|---|---|---|
| `package.json` | `next: latest`, React 18, `next build` | `next` 16.3.8 pinned, React 19, OpenNext's build, `@app/web` | A reproducible build; the Workers runtime |
| `components/Layout.js` | `<Link …><a className=…>{slug}</a></Link>` | the class on `<Link>`, the slug its child | Next ≥ 13 renders the anchor itself and refuses a nested `<a>` |
| `pages/_app.js` | `const { subscription } = supabase.auth.onAuthStateChange(…)` | `const { data: { subscription } } = …` | supabase-js 2 nests the subscription under `data`; upstream's unsubscribe threw on unmount |
| `pages/_app.js` | `export default function SupabaseSlackClone` | `ChatApp` | The upstream name spells this repository's slug in PascalCase, which no rule renames and the rebrand's sweep refuses in a product |
| `pages/_app.js`, `pages/index.js` | no `<title>`; no heading on the sign-in form | `<title>{SITE_NAME}</title>` on every page, `<h1>{SITE_NAME}</h1>` on the sign-in form, from `lib/site.ts` | The product's name has to live somewhere, once: it is the rebrand's seam |
| `pages/api/health.ts` | — | a read-only health check | The deploy's smoke and the bootstrap's probe (design §5) |
| `jsconfig.json`, `next.config.js` | JS config | `tsconfig.json` (`allowJs`, the same `~/*` paths), `next.config.ts`, `eslint.config.mjs` | The monorepo's typecheck and lint; the build reads the project from `wrangler.jsonc` |
| `supabase/migrations`, `supabase/seed.sql` | `supabase db push`, `seed.sql` | `packages/db/src/migrations/010_init`, `020_auth_hook`, `030_seed`, byte for byte, applied by db-migrate | One runner and one ledger per project; the seed's `#public` is where the app sends a signed-in user |
| `supabase/config.toml` `[auth.hook.custom_access_token]` | `supabase config push` | `packages/db/src/auth-config.ts`, set by the runner after the migrations | Auth must not point at a function that does not exist yet (risk SL-B) |
| `supabase/config.toml` `enable_confirmations = true` | confirmation on | off until the project has SMTP (`authEmailConfirmation`) | A fresh project's built-in email reaches only its organization's members (risk SL-H) |
| `README.md`, "Deploy with Vercel" | a Vercel clone button | Orun's pipeline (SL4: `orun new`) | A product on Cloudflare does not offer its visitors that |

## SL0 — the spec

Expand All @@ -25,3 +48,50 @@ manual-deploy contract — `workflow_dispatch` with `component`,
`environment` and `deploy_id`, the `run-name`, the dispatch planned as a
push to main, main only), `tag.yml`, the root manifests, `tooling/tsconfig`
and `tooling/eslint`, the discovery roots, the epic and the task contracts.

## SL1 — the chat app on Workers

`apps/web` as above, built by OpenNext into one Worker + Static Assets:
`next.config.ts` reads `NEXT_PUBLIC_SUPABASE_URL` and `_PUBLISHABLE_KEY`
from `wrangler.jsonc` for `next build` (production builds only — `next dev`
reads `.env.local`, as upstream); `wrangler.jsonc` is committed as the
fixture render of `wrangler.template.jsonc` (`pnpm run wire:fixture`).
`/api/health` asks Auth's `/auth/v1/settings` and the Data API's `channels`
with the publishable key and answers `{ok, auth, signup,
emailConfirmation, schema}` — 200 only when both answer, 503 with
`schema: "not migrated"` before the migrations. `tests/web`: the health
check (both answers, a refused key, no config, an unreachable project), the
data layer against a stand-in client, the Next config, the committed
wrangler config.

## SL2 — the Supabase project and the schema

- `infra/terraform/supabase`: supabase-starter's root, named for this
repository, publishing `WIRING_SUPABASE`, `SUPABASE_PROJECT_REF`,
`SUPABASE_DB_PASSWORD` and `SUPABASE_DB_URL`. The service-role key is not
an output.
- `packages/db` (`@saas/db`, the name the db-migrate composition builds):
the runner, `000_control`, upstream's three SQL files — checksums
`aa334acd…` (`010_init`), `4da15071…` (`020_auth_hook`), `4786f8af…`
(`030_seed`) — and `auth-config.ts`. `apply` applies what the ledger does
not hold, then `configureAuth`: `PATCH /v1/projects/{ref}/config/auth`
with `hook_custom_access_token_enabled: true` and
`hook_custom_access_token_uri:
pg-functions://postgres/public/custom_access_token_hook`, then a `GET`
that must read both back. Its output carries
`auth: {configured: [...]}`; a `plan` touches no Auth and says
`auth: {pending: [...]}`.
- `infra/db-migrate`: per environment, `dependsOn: supabase` enforced on
main and advisory on a pull request, its four secrets `secretEnv`
(fail-closed).
- `tests/db` (18 tests, PGlite with Supabase's roles, `auth` schema,
`extensions.uuid_generate_v4` and the publication stood in): the five
tables under RLS, the roles trigger, the seed, the hook's claims for each
role and its grants (denied to `anon` and `authenticated`), nothing for
`anon`, member, moderator and admin permissions, the publication and
`replica identity full`, upstream's checksums, and the Auth config.

## SL3 — ready to instantiate

The README (the upstream example first, the mapping to Vercel and
Supabase, how to run, test, preview and deploy) and this status.
2 changes: 1 addition & 1 deletion specs/epics/supabase-slack-clone/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ The upstream code stays the code: `lib/Store.js` (the client, the data hooks, th

| Field | Value |
|-------|-------|
| Status | In progress — SL0–SL1 shipped ([#1](https://github.com/sourceplane/supabase-slack-clone/pull/1), [#2](https://github.com/sourceplane/supabase-slack-clone/pull/2)), SL2 in review |
| Status | In progress — SL0–SL2 shipped ([#1](https://github.com/sourceplane/supabase-slack-clone/pull/1)–[#3](https://github.com/sourceplane/supabase-slack-clone/pull/3)), SL3 in review, SL4 next |
| Cluster | **SL** (SL0–SL4) |
| Owner(s) | `apps/web` (the app) · `tests/web` · `infra/terraform/supabase` (the project) · `packages/db` + `infra/db-migrate` (the schema) · `tests/db` |
| Builds on | `supabase-starter` — the shape of the repository and of the baseline, the `supabase` Terraform root, the stand-in; `sveltekit-saas` — the db-migrate lane on a Supabase project |
Expand Down
Loading