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
176 changes: 166 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,171 @@
# sveltekit-saas

[CMSaasStarter](https://github.com/scosman/CMSaasStarter) — Steve Cosman's
SvelteKit SaaS template: a pre-rendered marketing site, a blog, search,
pricing, Supabase Auth, an account dashboard, Stripe billing and email — on
**Cloudflare Workers + Static Assets** through
[`@sveltejs/adapter-cloudflare`](https://svelte.dev/docs/kit/adapter-cloudflare),
Steve Cosman's [CMSaasStarter](https://github.com/scosman/CMSaasStarter) — a
SvelteKit SaaS template: a pre-rendered marketing site, a blog with an RSS
feed and search, a pricing page, sign-up and sign-in with Supabase Auth, an
account dashboard with profile, email, password and subscription settings,
Stripe Checkout and the billing portal, a contact form, and welcome and admin
email through Resend — running on **Cloudflare Workers + Static Assets**
through [`@sveltejs/adapter-cloudflare`](https://svelte.dev/docs/kit/adapter-cloudflare),
with a **Supabase** project per environment provisioned by Terraform through
[Orun](https://github.com/sourceplane/orun), and packaged, once it is built,
as an **Orun baseline**.
[Orun](https://github.com/sourceplane/orun) and its schema applied by
migrations.

Being built in reviewed steps; the plan and its status are in
[`specs/epics/sveltekit-saas`](specs/epics/sveltekit-saas/README.md).
The app is upstream's, page for page. What changed is where it runs and where
its settings come from: one Worker serves the pages SvelteKit pre-renders
from its static assets and renders the rest; the Supabase project's URL and
anon key are rendered into the Worker's config per environment and inlined
into its build; every secret — the service-role key, Stripe's and Resend's —
is a Worker secret, read at request time. Stripe and Resend are optional: the
site goes live without them and says what to connect. See
[`specs/epics/sveltekit-saas`](specs/epics/sveltekit-saas/README.md) for the
design, the milestones and the as-built status.

CMSaasStarter is © 2023 Steve Cosman, under the MIT License.
## What is here

| Surface | Where |
|---|---|
| The marketing pages, the blog, pricing, search, contact | `apps/web/src/routes/(marketing)` |
| Sign-up, sign-in, password reset, the OAuth callback | `apps/web/src/routes/(marketing)/login`, `…/auth/callback` |
| The account dashboard and its settings, the profile step, billing | `apps/web/src/routes/(admin)/account` |
| The site's name, origin and description | `apps/web/src/config.ts` |
| The pricing plans (with their Stripe price ids) | `apps/web/src/routes/(marketing)/pricing/pricing_plans.ts` |
| Email (Resend) and its templates, precompiled at build | `apps/web/src/lib/mailer.ts`, `apps/web/src/lib/emails/` |
| Stripe, connected or not | `apps/web/src/lib/server/stripe.ts` |
| The health route the deploy's smoke reads | `apps/web/src/routes/api/health/+server.ts` |
| Workers build, bindings and deploy contract | `apps/web/svelte.config.js`, `vite.config.ts`, `wrangler.template.jsonc` + `wiring.fixture.json` (→ the committed `wrangler.jsonc`), `component.yaml` |
| The Supabase project and its Auth (stage, prod) | `infra/terraform/supabase` |
| The schema (upstream's migrations) and the runner that applies them | `packages/db`, run by `infra/db-migrate` |
| The wrangler renderer, the build's vars, the Worker-secrets helper | `tooling/wire` |
| Tests | `tests/web` (the Worker's config and secrets, then the app's own suite), `tests/db` (the migrations on a Supabase-shaped Postgres) |

## Getting started

```bash
pnpm install
pnpm run wire:fixture # render apps/web/wrangler.jsonc from the offline fixture
pnpm typecheck
pnpm lint
pnpm test
pnpm build # vite build → apps/web/.svelte-kit/cloudflare (the Worker and its assets)
```

To run it against a Supabase project of your own (a free one, or the stage
project a bootstrap created):

```bash
cd apps/web
cp .env.example .env # PUBLIC_SUPABASE_URL and PUBLIC_SUPABASE_ANON_KEY
cp .dev.vars.example .dev.vars # PRIVATE_SUPABASE_SERVICE_ROLE (and Stripe, Resend: optional)
pnpm dev # http://localhost:5173
```

`pnpm build` and `pnpm preview` (wrangler dev over the build) read the project
from `wrangler.jsonc` instead, so render it with your project for a local
build of your own, or leave the fixture for the offline checks. A project of
your own needs the schema: run the migrations in `packages/db/src/migrations`
in order in its SQL editor, or let a bootstrap's db-migrate apply them.

## Stripe

Billing is off until an environment has a Stripe secret key:

1. Create the products and prices in Stripe and put their ids in
`apps/web/src/routes/(marketing)/pricing/pricing_plans.ts`
(`stripe_price_id`, `stripe_product_id`), as upstream's README describes.
2. Store the secret key as the product's Orun secret `STRIPE_API_KEY` for the
environment (`orun secrets set STRIPE_API_KEY --env stage`, or the
console's Secrets) and redeploy it (Orunbase's Deploy button, or Run
workflow on `main`).

The build hands the key to the Worker as a secret (`tooling/wire/worker-secrets.mjs`,
the value over stdin, never in a bundle); the billing page then opens Stripe
Checkout and the billing portal. Until then it says Stripe is not connected,
and checkout and the portal send visitors there. Removing the Orun secret
does not remove the Worker's copy: `wrangler secret delete
PRIVATE_STRIPE_API_KEY --name <worker>` too.

## Email

Welcome and admin emails go through [Resend](https://resend.com) once an
environment has `RESEND_API_KEY`, `ADMIN_EMAIL` (where admin notices go) and
`FROM_ADMIN_EMAIL` (a sender on a domain verified in Resend) as Orun secrets;
redeploy after setting them. Without them no email is sent, as upstream
behaves.

Supabase Auth's own email (confirmation, password reset) is the project's: a
new project's built-in SMTP mails only its organization's members, so the
Terraform root turns email confirmation off (`authEmailConfirmation:
"false"` in `infra/terraform/supabase/component.yaml`) and sign-up signs the
user straight in. Give the project SMTP of its own (Supabase → Authentication
→ SMTP), then set it to `"true"`; the next apply turns confirmation on.

## OAuth providers

The sign-in page offers email and password. To add GitHub (upstream's
default), create a GitHub OAuth app whose callback is
`https://<project-ref>.supabase.co/auth/v1/callback`, turn the provider on in
the environment's Supabase project (Authentication → Providers), and add
`"github"` to `oauthProviders` in `apps/web/src/routes/(marketing)/login/login_config.ts`.

## Custom domain

Each environment answers on its workers.dev URL. To serve one on a domain of
your own, attach the domain to the Worker in Cloudflare (Workers → the
Worker → Domains & Routes), then set `SITE_URL` under that environment's
`env:` in `intent.yaml` and merge: the next apply points the project's Auth
at it, and the next deploy builds the sitemap, the feed and the emails on it.
`BASE_DOMAIN` in `intent.yaml` holds the placeholder `<repo>.example.com`
until then.

## How it maps to adapter-auto and Vercel

| Upstream | Here |
|---|---|
| `@sveltejs/adapter-auto` | `@sveltejs/adapter-cloudflare` 7: one Worker, the pre-rendered pages and assets beside it, in one `wrangler deploy` |
| A Supabase project made by hand; `supabase db push` | `infra/terraform/supabase` per environment; upstream's two migrations applied by `infra/db-migrate` over the Management API before the Worker deploys |
| `PUBLIC_SUPABASE_URL`, `PUBLIC_SUPABASE_ANON_KEY` in `.env` | rendered into `wrangler.jsonc` from the root's wiring document, and handed to `vite build` from there (`tooling/wire/with-vars.mjs`) |
| `PRIVATE_SUPABASE_SERVICE_ROLE`, `PRIVATE_STRIPE_API_KEY` through `$env/static/private` (inlined at build) | Worker secrets read through `$env/dynamic/private`; the service-role key published by the root, Stripe's and Resend's optional Orun secrets |
| `new Stripe(key)` at module load | created per request with Stripe's fetch client; without a key the billing page says to connect Stripe |
| `handlebars.compile` per email | templates precompiled at build, rendered with the Handlebars runtime (Workers forbid code generation) |
| `WebsiteBaseUrl = "https://saasstarter.work"` | `PUBLIC_SITE_URL`: the environment's `SITE_URL`, or its workers.dev URL |
| The welcome email from `no-reply@saasstarter.work` | from `FROM_ADMIN_EMAIL`, the owner's own verified sender |
| GitHub sign-in on by default | email and password; GitHub once its OAuth app is set up (above) |
| `patch-package` for a SvelteKit 2.55 bug | not needed on SvelteKit 2.70 |
| Deploy on push (Vercel) | Orun applies the project, migrates it and deploys `<repo>-web-{stage,prod}` on merge to `main`, smoke-tested through `/api/health` |

## How the project reaches the Worker

```
infra/terraform/supabase ──apply──► WIRING_SUPABASE (url, anon key) · SUPABASE_SERVICE_ROLE_KEY
· SUPABASE_PROJECT_REF · SUPABASE_DB_PASSWORD · SUPABASE_DB_URL
▼
infra/db-migrate dependsOn supabase (enforced on main)
apply: packages/db's runner → POST api.supabase.com/v1/projects/<ref>/database/query, each pending migration once
▼
apps/web dependsOn supabase + db-migrate (enforced on main) · secretEnv WIRING_SUPABASE_<ENV>
build: tooling/wire/live.mjs → wrangler.jsonc vars → vite build (the project inlined)
→ worker-secrets.mjs: the service-role key, Stripe and Resend as Worker secrets
smoke: / renders to </html>; /api/health counts profiles with the service-role key
```

`wrangler.jsonc` is committed as the fixture render of
`wrangler.template.jsonc`, because the deploy lane's `verify-app-structure`
step checks the config exists before it builds.

## CI

`ci.yml` is the Orun pipeline: `orun plan --changed`, then one job per lane.
A pull request runs the verify lanes (the packages type-checked, linted,
tested and built, both test suites, the Supabase root planned, the migrations
planned offline, the Worker built and dry-deployed per environment); a merge
applies the project, migrates it, then 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.

## Licence

CMSaasStarter is © 2023 Steve Cosman, under the MIT License
([`apps/web/LICENSE`](apps/web/LICENSE)). The port keeps it.
42 changes: 38 additions & 4 deletions specs/epics/sveltekit-saas/IMPLEMENTATION-STATUS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,42 @@ As built, kept apart from the plan.

| Milestone | State | Pull request |
|---|---|---|
| SV0 — the spec | in progress | — |
| SV1 — the app on Workers | planned | — |
| SV2 — the Supabase project and its schema | planned | — |
| SV3 — ready to instantiate | planned | — |
| SV0 — the spec | ✅ shipped | [#1](https://github.com/sourceplane/sveltekit-saas/pull/1) |
| SV1 — the app on Workers | ✅ shipped | [#2](https://github.com/sourceplane/sveltekit-saas/pull/2) |
| SV2 — the Supabase project and its schema | ✅ shipped | [#3](https://github.com/sourceplane/sveltekit-saas/pull/3) |
| SV3 — ready to instantiate | in progress | — |
| SV4 — a baseline | planned | — |

## Departures from upstream (scosman/CMSaasStarter at `2e61406`)

Everything not listed here is upstream's, file for file.

| Where | Upstream | Here | Why |
|---|---|---|---|
| `svelte.config.js` | `adapter-auto` | `@sveltejs/adapter-cloudflare` 7, `platformProxy` on `wrangler.jsonc` | Workers + Static Assets |
| `package.json` | `adapter-auto`, `patch-package` | `adapter-cloudflare`, `wrangler`; no `patch-package` (the SvelteKit 2.55 bug it patched does not reproduce on 2.70); `fuse.js ~7.0.0` and `prettier ~3.4.2` / `prettier-plugin-svelte ~3.3.2`, the versions upstream's lockfile resolved | newer fuse.js breaks the search page's types; newer prettier reformats upstream's files |
| scripts | `vite build`, `svelte-check`, `vitest` | each run through `tooling/wire/with-vars.mjs`, which hands `wrangler.jsonc`'s vars to the command | `$env/static/public` is inlined at build: the build reads the project it deploys against |
| `hooks.server.ts`, `lib/mailer.ts`, the Stripe routes | `$env/static/private` (inlined at build) | `$env/dynamic/private` (request time) | secrets are Worker secrets, never in a bundle |
| `hooks.server.ts` | `createClient(url, serviceRole)` | the same, or — with no key set — a client whose every use fails naming the key | public pages keep working on a local run without `.dev.vars` |
| Stripe routes | `new Stripe(key)` at module load | `lib/server/stripe.ts`: per request, Stripe's fetch client; no key → the billing page says to connect Stripe, checkout and the portal send the visitor there | Workers have no Node `http`; the site goes live before Stripe is connected |
| `lib/mailer.ts` | `handlebars.compile` per email | `lib/emails/templates.ts`: templates precompiled at build by a Vite plugin (`vite.config.ts`), rendered with `handlebars/runtime` | Workers forbid generating code from strings |
| `config.ts` | `WebsiteBaseUrl = "https://saasstarter.work"` | from `PUBLIC_SITE_URL`: the environment's `SITE_URL` or its workers.dev URL; `http://localhost:5173` locally | the product's own origin, per environment |
| `account/api/+page.server.ts` | welcome email from `no-reply@saasstarter.work`, company name `"SaaS Starter"` | from `PRIVATE_FROM_ADMIN_EMAIL` (or `PRIVATE_ADMIN_EMAIL`), skipped without one; `WebsiteName` | Resend sends only from a verified domain of the owner's |
| `login/login_config.ts` | `oauthProviders = ["github"]` | `[]` | a provider fails until its OAuth app is set up in the project (README, "OAuth providers") |
| `routes/api/health/+server.ts` | — | new: counts `profiles` with the service-role key | the deploy's smoke |
| `routes/(admin)/account/(menu)/billing` | — | `stripeConnected` in the load, a notice on the page | Stripe is optional |
| `supabase/migrations/*.sql` | applied by `supabase db push` | `packages/db/src/migrations/010_initial`, `020_add_unsubscribed_to_profiles`, byte for byte, applied by `infra/db-migrate` | Orun applies the schema per environment |
| `database_migration.sql`, `checks.sh`, `.github/`, the docs pages | — | not ported | the repository's own tooling replaces them |

## Proven offline

- `pnpm install --frozen-lockfile`, `wire:fixture`, `typecheck`, `lint`, `test`, `build` green.
- The built Worker (487 KiB gzipped) under `wrangler dev`: every marketing page, the blog, the feed, the sitemap and the search index; `/account` → `/login`; an unknown path is a 404; `/api/health` 503 without the service-role key.
- The migrations through the runner into PGlite with Supabase's roles, `auth` and `storage` stood in: applied once, a second apply nothing; the profile trigger, row-level security, the bucket.
- `terraform validate` on the Supabase root; `orun plan` for a merge: supabase → db-migrate → web.

## Not proven here

The project's apply, the migrations against a real project (risk SV-A), the
service-role key's publication and the live deploy need a linked workspace
(risk SV-H).
2 changes: 1 addition & 1 deletion specs/epics/sveltekit-saas/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ The port keeps the marketing site, the blog with its RSS feed, the build-time se

| Field | Value |
|-------|-------|
| Status | SV0 in progress |
| Status | SV0–SV2 shipped (#1–#3); SV3 in progress |
| Cluster | **SV** (SV0–SV4) |
| Owner(s) | `apps/web` (the SvelteKit app) · `tests/web` · `infra/terraform/supabase` · `packages/db` + `infra/db-migrate` · `tests/db` |
| Builds on | `sveltekit-boilerplate` (a SvelteKit app on adapter-cloudflare, one Worker with Static Assets), `supabase-starter` (the `supabase` root, its Auth settings and wiring, the Supabase stand-in its e2e runs against), `saas-starter` (the migration runner over the Management API and `db-migrate`), `cirrus-baseline` (the `@@wiring(...)@@` deploy-time render) |
Expand Down
Loading