How tangsy/edge-next-starter works
Purpose and Runtime
This is a production-oriented full-stack starter for Next.js App Router workloads deployed as a Cloudflare Worker through vinext/Vite. It provides edge-compatible application patterns for authentication, D1 persistence, KV caching/rate limiting, R2 uploads, Stripe billing, GDPR, and observability.
Worker bootstrap and deployment bindings are centered on worker/index.ts, wrangler.toml, and wrangler.prod.toml.
Application Entry Points
The UI is locale-scoped App Router content, rooted by app/layout.tsx and app/[locale]/layout.tsx; locale routing/request configuration lives in i18n/routing.ts and i18n/request.ts. Primary product screens include dashboard, profile, billing, pricing, and upload under app/[locale]/.
HTTP endpoints enter through route handlers in app/api/, including health, registration, auth, users/posts, uploads, GDPR, monitoring, and Stripe. The API boundary is designed for Edge Runtime execution rather than a separate backend service.
Authentication and User Lifecycle
better-auth is integrated behind auth libraries and API routes: lib/auth/index.ts, lib/auth/session.ts, and app/api/auth/[...all]/route.ts. Registration has a dedicated endpoint at app/api/register/route.ts, while server-side UI auth actions are in app/actions/auth.ts.
User data moves from handlers/services to repositories and D1. User-facing account pages are app/[locale]/login/page.tsx, app/[locale]/register/page.tsx, and app/[locale]/profile/page.tsx. Google OAuth and credential authentication are intended external identity paths.
API, Domain, and Persistence Boundary
Route handlers should use the common API layer for middleware, database access, rate limiting, and response conventions: lib/api/middleware.ts, lib/api/database.ts, and lib/api/response.ts. Domain orchestration is separated into services such as services/auth.service.ts and services/upload.service.ts.
Repositories isolate database operations from HTTP/domain code: repositories/user.repository.ts, repositories/post.repository.ts, and repositories/subscription.repository.ts. Prisma/D1 access is established by lib/db/client.ts; durable schema evolution is represented in prisma/schema.prisma and ordered SQL migrations under migrations/.
Cloudflare Data Services and Upload Flow
D1 is the relational source of truth. KV supports cache and likely request limiting through lib/cache/client.ts, lib/rate-limiter/index.ts, and app/api/rate-limit/route.ts. R2 object operations are isolated in lib/r2/client.ts.
Uploads flow from UI/API to an upload service and R2: app/[locale]/upload/page.tsx, app/api/upload/route.ts, and services/upload.service.ts. Signed URL support in lib/signed-url/index.ts provides a boundary for controlled object access.
Billing, Webhooks, and Compliance
Stripe client/configuration and webhook business logic live in lib/stripe/client.ts, lib/stripe/config.ts, and lib/stripe/webhook-handlers.ts. Checkout, portal, subscription state changes, and inbound webhook verification are exposed under app/api/stripe/; billing UI is under app/[locale]/billing/.
Billing persistence is modeled through customer, payment, invoice, subscription, and webhook-event repositories. GDPR export/delete endpoints (app/api/gdpr/export/route.ts, app/api/gdpr/delete/route.ts) provide explicit privacy-management control paths.
Cross-Cutting Controls and Operations
Environment validation is centralized in lib/config/env.ts. Security and traffic controls span CORS/CSRF, API middleware, and KV-backed limits: lib/security/cors.ts, lib/security/csrf.ts, and lib/api/rate-limit.ts. Audit logging, structured logging, analytics, and alerting are separate adapters in lib/audit/logger.ts, lib/analytics/index.ts, and lib/monitoring/alerting.ts.
GitHub Actions run CI and promote branches to Cloudflare environments: .github/workflows/ci.yml, .github/workflows/deploy-test.yml, and .github/workflows/deploy-prod.yml.
Main entry points: route.ts Route handlers · user.repository.ts Repositories · index.ts Worker bootstrap · auth.service.ts Domain services
Components 24
App Router UI
Locale-scoped product interface.
- Locale App Router · Layouts and product pages including dashboard, profile, billing, and upload.
app/[locale]/layout.tsx - Locale routing
i18n/routing.ts - Account screens & actions · Login, registration, profile, and server-side auth actions.
app/actions/auth.ts
Edge API
Route handlers execute in the Worker runtime.
- Worker bootstrap · Deploys the vinext/Vite Next.js application with bindings.
worker/index.ts - Route handlers · Health, users, posts, upload, GDPR, monitoring, and product APIs.
app/api/health/route.ts - API middleware layer · Applies middleware, database access, rate limits, and response conventions.
lib/api/middleware.ts - Auth endpoints · better-auth catch-all and dedicated registration endpoint.
app/api/auth/[...all]/route.ts - Stripe endpoints · Checkout, portal, subscription, and webhook endpoints.
app/api/stripe/webhook/route.ts - GDPR endpoints · Exports or deletes personal data.
app/api/gdpr/export/route.ts
Domain & Platform
Shared application capabilities and orchestration.
- Auth & sessions · Authentication and session boundary.
lib/auth/index.ts - Domain services · Orchestrates authentication and upload workflows.
services/auth.service.ts - Repositories · Isolates user, post, subscription, and billing database operations.
repositories/user.repository.ts - Prisma D1 client
lib/db/client.ts - Cache & rate limits · KV-backed caching and request limiting.
lib/rate-limiter/index.ts - R2 object access · Object operations and controlled signed access.
lib/r2/client.ts - Stripe integration · Client configuration and verified webhook business logic.
lib/stripe/webhook-handlers.ts - Audit & monitoring · Audit logs, analytics, alerting, and monitoring.
lib/audit/logger.ts
Cloudflare Data
Bound Worker storage bindings.
- D1 database · Relational source of truth, defined by Prisma schema and migrations.
prisma/schema.prisma - KV
lib/cache/client.ts - R2
External Services
- Google OAuth
- Stripe
Delivery
- CI/CD workflows · Validates and promotes deployments to Cloudflare environments.
.github/workflows/ci.yml - Cloudflare deployment · Worker bindings and production deployment configuration.
wrangler.prod.toml
Connections 23
- CI/CD workflows → Cloudflare deployment: deploys
- Cloudflare deployment → Worker bootstrap: configures
- Worker bootstrap → Locale App Router: serves
- Worker bootstrap → Route handlers: routes requests
- Locale routing → Locale App Router: localizes
- Account screens & actions → Auth & sessions: authenticates via
- Locale App Router → Route handlers: calls
- Route handlers → API middleware layer: uses
- Route handlers → Domain services: invokes
- Auth endpoints → Auth & sessions: uses
- Stripe endpoints → Stripe integration: uses
- GDPR endpoints → Repositories: exports or deletes
- API middleware layer → Cache & rate limits: enforces
- Domain services → Repositories: persists through
- Domain services → R2 object access: uploads through
- Repositories → Prisma D1 client: uses
- Prisma D1 client → D1 database: reads and writes
- Cache & rate limits → KV: uses
- R2 object access → R2: stores objects
- Auth & sessions → Google OAuth: OAuth
- Stripe integration → Stripe: checkout and webhooks
- Stripe integration → Repositories: records billing state
- Route handlers → Audit & monitoring: emits telemetry