Skip to content
Open menu

Architecture

Request lifecycle, route groups, data layer rules, the billing adapter contract, and failure modes.

Nexo Starter is a single Next.js 16 application using the App Router. There is no separate API service and no background worker process in the default setup.

Request lifecycle

A request enters through the Next.js server. Middleware runs first and handles locale negotiation and session refresh; it does not perform authorization decisions. The matched route then runs as either a server component or a route handler.

Server components call the data layer directly. Mutations go through server actions or route handlers under app/api. Every mutation resolves the session, checks the caller's role in the target team, writes through the data layer, and appends an audit log entry in the same transaction.

Route handlers under app/api/webhooks are the exception: they authenticate by signature, not session, and are excluded from locale middleware.

Route groups

app/
  (marketing)/     public pages, pricing, docs
  (auth)/          sign-in, magic-link callback
  (app)/           authenticated product surface
    settings/
    teams/
    billing/
  (admin)/         admin console, role-gated
  api/             route handlers, webhooks

Route groups carry layouts, not URL segments. The (app) layout resolves the active team and redirects unauthenticated users to sign-in. The (admin) layout checks for the admin role and returns 404 rather than 403 for non-admins, so the console's existence is not disclosed.

Data layer rules

All database access goes through db/. Three rules apply:

  • No queries in components. Components call functions exported from db/queries or db/mutations. This keeps tenant scoping in one place.
  • Tenant scope is explicit. Every team-scoped query takes a teamId argument. There is no ambient "current team" in the data layer; the caller passes it. This makes cross-tenant leaks a type error rather than a runtime surprise.
  • Transactions wrap multi-step writes. A mutation that touches more than one table, including the audit log, runs inside db.transaction.

Drizzle migrations live in drizzle/ and are generated, never hand-edited after they have shipped.

Billing adapter contract

Billing is defined by an interface in lib/billing/types.ts. Stripe and Paddle each implement it. The interface covers checkout session creation, subscription reads, cancellation, and webhook event normalization into a shared event shape. Application code depends on the interface, never on a provider SDK.

Provider-specific fields do not leak past the adapter. If you need a new capability, extend the interface and implement it in both adapters, or accept that the feature is provider-specific and document it.

Where to add a feature

For a typical feature:

  1. Add or change the schema in db/schema, then pnpm db:generate.
  2. Write the query or mutation in db/, scoped by teamId.
  3. Add a server action or route handler that resolves the session and checks the role.
  4. Build the UI under the relevant route group.
  5. Add unit tests next to the logic, not the markup.

Failure modes

  • Missing AUTH_SECRET fails at startup. This is intentional.
  • Webhook retries are handled by idempotency keys; see the Billing article. A duplicate delivery is a no-op.
  • Email and storage outages degrade rather than block. Magic-link sign-in falls back to console output when no provider is set; uploads return a clear error when storage is unconfigured.
  • Migration drift surfaces on boot when the schema version does not match. Run migrations before starting the server in production.