Next.js + Supabase
Next.js Supabase Cursor rules
Rules pinned to Next.js 16 and Supabase SSR — the APIs that actually work with React 19. Same advice in all four agent file formats, snippet-checked.
Verified on 2026-11-04 · zip downloads · version-pinned
Verified for
- Next.js 16.4.5
- React 19.3
- @supabase/ssr 0.12.7
- @supabase/supabase-js current, installed alongside @supabase/ssr
Last tested 2026-11-04
Snippet-checked
doc-verified; TypeScript snippets pass tsc --strict (TypeScript 5, @types/react 19) with zod and the Supabase client stubbed
The rules
Stops the mistakes agents still make on this stack: sync params, treating middleware.ts as dead code, renaming an Edge interceptor to proxy.ts, the deprecated auth-helpers package, and trusting getSession() for authorization. Includes the proxy session-refresh sample. Rules match the Next.js 16 upgrade guide, the November 2026 security release (16.4.5), and Supabase SSR docs as of November 2026.
Related fixes
Who it's for
People vibe-coding a Next.js App Router app with Supabase Auth, Postgres, and RLS.
Install
Copy one file, or download the zip for that tool. The zip already uses the path below.
Cursor
.cursor/rules/nextjs-supabase.mdc
- Download the Cursor zip, or copy the .mdc file from this page.
- Save it as .cursor/rules/nextjs-supabase.mdc.
- Commit it. Cursor reads project rules from that folder.
Claude Code
CLAUDE.md
- Download the Claude zip, or copy CLAUDE.md.
- Put CLAUDE.md in the project root.
- Claude Code reads it as project memory on the next session.
AGENTS.md
AGENTS.md
- Download the Agents zip, or copy AGENTS.md.
- Put AGENTS.md in the project root.
- Agents that read AGENTS.md, including Codex and Cursor, pick it up there.
GitHub Copilot
.github/copilot-instructions.md
- Download the Copilot zip, or copy the instructions file.
- Save it as .github/copilot-instructions.md.
- Copilot reads repository instructions from that path.
Questions
- What does the Next.js + Supabase pack do?
- Stops the mistakes agents still make on this stack: sync params, treating middleware.ts as dead code, renaming an Edge interceptor to proxy.ts, the deprecated auth-helpers package, and trusting getSession() for authorization. Includes the proxy session-refresh sample. Rules match the Next.js 16 upgrade guide, the November 2026 security release (16.4.5), and Supabase SSR docs as of November 2026.
- Who is the Next.js + Supabase pack for?
- People vibe-coding a Next.js App Router app with Supabase Auth, Postgres, and RLS.
- Which versions does the Next.js + Supabase pack pin?
- Next.js: 16.4.5 React: 19.3 @supabase/ssr: 0.12.7 @supabase/supabase-js: current, installed alongside @supabase/ssr Last tested 2026-11-04.
- What does the first section of the Next.js + Supabase pack say?
- Pinned on 2026-11-04: Next.js 16.4.5 (App Router, Nov 2026 security release), React 19.3, `@supabase/ssr` 0.12.7, `@supabase/supabase-js`. Do not generate Pages Router or `@supabase/auth-helpers-nextjs`. On the Supabase client, pass `getAll` and `setAll` only. Do not pass `get`, `set`, or `remove`. The first section, Routing and components, says: Server Components are the default. Add `"use client"` only when the file uses state, effects, browser APIs, or event handlers. Push that file to a leaf component. `params` and `searchParams` are Promises. Always `await` them. Synchronous access typechecks in places and crashes at runtime. `cookies()`, `headers()`, and `draftMode()` from `next/headers` are async. `await` them. On Next.js 16 the Supabase session refresh belongs in `proxy.ts`, and the export is `proxy`. `proxy.ts` runs on the Node.js runtime. That runtime cannot be set to Edge. Setting `runtime` in `proxy.ts` throws. `middleware.ts` is deprecated and still runs, including on the Edge runtime. Keep it only when the interceptor must stay on Edge, and export `middleware`. Do not rename that file to `proxy.ts`. Do not treat `middleware.ts` as dead code. A file Next never calls will not refresh the session. Do not ship both `proxy.ts` and `middleware.ts`. Next.js uses one interceptor. New `create-next-app` projects enable Cache Components. Do not cache a response that depends on a cookie or a user. Authenticated reads stay dynamic.
- When was the Next.js + Supabase pack last tested, and how?
- Next.js + Supabase was last tested on 2026-11-04. doc-verified; TypeScript snippets pass tsc --strict (TypeScript 5, @types/react 19) with zod and the Supabase client stubbed.
Other free packs
Same four files, pinned to a different stack.
- AGENTS.md template for vibe coding
Vibe Coder Starter
Safe defaults for any AI coding agent in any stack. Tool-agnostic rules for Cursor, Claude Code, AGENTS.md, and Copilot so agents stay scoped and stop guessing.
- Cursor rules for Unity 6
Unity 6
Unity 6 C# rules that refuse obsolete 2022 / UNet patterns. Input System defaults, current APIs, four files ready for Cursor, Claude, AGENTS.md, and Copilot.
- Godot 4 CLAUDE.md
Godot 4
GDScript 2.0 rules that reject Godot 3 APIs. Written for Godot 4.x, parse-tested where it counts, shipped for every major AI coding tool.
Plain text
The same pack as markdown, plus each rules file on its own URL.
Files
.cursor/rules/nextjs-supabase.mdc
---
description: Next.js 16.4 App Router with @supabase/ssr 0.12.7. Async params, proxy.ts on Node, Edge stays middleware.ts, getClaims, RLS.
globs: "{app,src,lib,components}/**/*.{ts,tsx},proxy.ts,middleware.ts,**/*.sql"
alwaysApply: false
---
# Next.js 16.4 + Supabase SSR
Pinned on 2026-11-04: Next.js 16.4.5 (App Router, Nov 2026 security release), React 19.3, `@supabase/ssr` 0.12.7, `@supabase/supabase-js`. Do not generate Pages Router or `@supabase/auth-helpers-nextjs`. On the Supabase client, pass `getAll` and `setAll` only. Do not pass `get`, `set`, or `remove`.
## Routing and components
- Server Components are the default. Add `"use client"` only when the file uses state, effects, browser APIs, or event handlers. Push that file to a leaf component.
- `params` and `searchParams` are Promises. Always `await` them. Synchronous access typechecks in places and crashes at runtime.
- `cookies()`, `headers()`, and `draftMode()` from `next/headers` are async. `await` them.
- On Next.js 16 the Supabase session refresh belongs in `proxy.ts`, and the export is `proxy`. `proxy.ts` runs on the Node.js runtime. That runtime cannot be set to Edge. Setting `runtime` in `proxy.ts` throws.
- `middleware.ts` is deprecated and still runs, including on the Edge runtime. Keep it only when the interceptor must stay on Edge, and export `middleware`. Do not rename that file to `proxy.ts`. Do not treat `middleware.ts` as dead code. A file Next never calls will not refresh the session.
- Do not ship both `proxy.ts` and `middleware.ts`. Next.js uses one interceptor.
- New `create-next-app` projects enable Cache Components. Do not cache a response that depends on a cookie or a user. Authenticated reads stay dynamic.
```tsx
export default async function Page({
params,
}: {
params: Promise<{ id: string }>
}) {
const { id } = await params
return <h1>{id}</h1>
}
```
## Supabase clients
- Install `@supabase/supabase-js` and `@supabase/ssr`. Never import `@supabase/auth-helpers-nextjs`.
- Env names from the current docs: `NEXT_PUBLIC_SUPABASE_URL` and `NEXT_PUBLIC_SUPABASE_PUBLISHABLE_KEY`. The secret key and the service-role key stay server-only, with no `NEXT_PUBLIC_` prefix.
- Browser code uses `createBrowserClient`. Server Components, Server Actions, and Route Handlers use `createServerClient` with `cookies.getAll` / `cookies.setAll`.
- `setAll` receives a second argument, the cache headers. Apply `Cache-Control`, `Expires`, and `Pragma` on the response. Skipping them lets a CDN store one user's session cookie and serve it to someone else.
- In a Server Component, `setAll` cannot write cookies. Catch and ignore that error. The write happens in `proxy.ts`, or in the `middleware.ts` you kept for Edge.
- Put the server client in `lib/supabase/server.ts` and the refresh helper in `lib/supabase/proxy.ts`. The root `proxy.ts` only calls that helper. If the app lives in `src/`, put `proxy.ts` in `src/` and the helpers in `src/lib/supabase/`.
## Auth
- To decide whether a request is allowed, call `supabase.auth.getClaims()`. It verifies the JWT (locally against JWKS on asymmetric keys, which new projects use).
- Use `getUser()` only when you need the current user row from the Auth server. Use `getSession()` only to read the raw token, and never trust `session.user` for authorization.
- The refresh helper calls `getClaims()` immediately after `createServerClient`. Do not run other code between those two calls. It copies cookies onto both the request and the response, applies the cache headers, and returns the response that `setAll` last built. An earlier response does not carry the refreshed cookies, so the user is signed out on the next request.
- Pages and actions still call `getClaims()` themselves. Proxy is not the only lock.
- Sign-in and sign-out go through Server Actions, not a client `fetch` to your own route.
## Database
- Every table in `public` has RLS enabled. Add policies only for the operations clients should be allowed to do, because any operation without a policy is denied. A table without RLS is readable and writable through the Data API under the default grants.
- Select named columns. Do not `.select("*")` and do not return the whole row to the client.
- Validate mutation input with Zod, then check `getClaims()`, then write. Return a typed `{ ok, error }` object, not a thrown string.
```ts
const { data, error } = await supabase
.from("notes")
.select("id, title, created_at")
.eq("user_id", claims.sub)
```
The server client the action imports, and the refresh helper `proxy.ts` calls. `claims` in the query above is `data.claims` from `getClaims()`.
```ts
import { createServerClient } from "@supabase/ssr"
import { cookies } from "next/headers"
export async function createClient() {
const cookieStore = await cookies()
return createServerClient(
process.env.NEXT_PUBLIC_SUPABASE_URL!,
process.env.NEXT_PUBLIC_SUPABASE_PUBLISHABLE_KEY!,
{
cookies: {
getAll() {
return cookieStore.getAll()
},
setAll(cookiesToSet, _headers) {
try {
cookiesToSet.forEach(({ name, value, options }) =>
cookieStore.set(name, value, options),
)
} catch {
// setAll from a Server Component cannot write cookies.
// proxy.ts (or Edge middleware.ts) refreshes the session.
}
},
},
},
)
}
```
```ts
import { createServerClient } from "@supabase/ssr"
import { NextResponse, type NextRequest } from "next/server"
export async function updateSession(request: NextRequest) {
let supabaseResponse = NextResponse.next({ request })
const supabase = createServerClient(
process.env.NEXT_PUBLIC_SUPABASE_URL!,
process.env.NEXT_PUBLIC_SUPABASE_PUBLISHABLE_KEY!,
{
cookies: {
getAll() {
return request.cookies.getAll()
},
setAll(cookiesToSet, headers) {
cookiesToSet.forEach(({ name, value }) => request.cookies.set(name, value))
supabaseResponse = NextResponse.next({ request })
cookiesToSet.forEach(({ name, value, options }) =>
supabaseResponse.cookies.set(name, value, options),
)
Object.entries(headers).forEach(([key, value]) =>
supabaseResponse.headers.set(key, value),
)
},
},
},
)
await supabase.auth.getClaims()
return supabaseResponse
}
```
```ts
import { type NextRequest } from "next/server"
import { updateSession } from "@/lib/supabase/proxy"
export async function proxy(request: NextRequest) {
return await updateSession(request)
}
export const config = {
matcher: [
"/((?!_next/static|_next/image|favicon.ico|.*\\.(?:svg|png|jpg|jpeg|gif|webp)$).*)",
],
}
```
On an Edge-only project, the same `updateSession` call is exported as `middleware` from `middleware.ts`. Do not also add `proxy.ts`.
## Server Actions and forms
- Put `"use server"` at the top of an actions file, or inside the function body. Do not export non-async values from a `"use server"` file.
- Every action re-checks auth with `getClaims()`. An action is a public HTTP endpoint even if no button calls it.
- After a write the signed-in user must see on the next render, call `updateTag("notes")` from `next/cache` inside the Server Action. `updateTag` throws outside a Server Action.
- In a Route Handler, call `revalidateTag(tag, "max")` for stale-while-revalidate, or `revalidateTag(tag, { expire: 0 })` when the next read must wait. The single-argument `revalidateTag(tag)` is deprecated. `revalidatePath` still invalidates one path. Do not force a full `router.refresh()` from the client to fix stale data.
```ts
"use server"
import { updateTag } from "next/cache"
import { z } from "zod"
import { createClient } from "@/lib/supabase/server"
const NoteInput = z.object({ title: z.string().min(1).max(200) })
export async function createNote(formData: FormData) {
const parsed = NoteInput.safeParse({ title: formData.get("title") })
if (!parsed.success) return { ok: false, error: "Invalid title" }
const supabase = await createClient()
const { data, error } = await supabase.auth.getClaims()
if (error || !data) return { ok: false, error: "Not signed in" }
const { error: insertError } = await supabase
.from("notes")
.insert({ title: parsed.data.title, user_id: data.claims.sub })
if (insertError) return { ok: false, error: insertError.message }
updateTag("notes")
return { ok: true }
}
```
## Deploying on Vercel
- Set `NEXT_PUBLIC_SUPABASE_URL` and `NEXT_PUBLIC_SUPABASE_PUBLISHABLE_KEY` in the Vercel project for Preview and Production.
- Keep the Supabase secret key out of the client bundle. Search the build for `NEXT_PUBLIC_` names before shipping.
- Add every deployed URL (production and preview pattern) to Supabase Auth's redirect allow list, or OAuth and magic links fail.
## Done checklist
1. `npx tsc --noEmit` and `next build` pass, or you said why they could not run.
2. New tables have RLS enabled plus policies, in a migration file.
3. No `getSession()` is used for authorization, and no `select("*")` was added.
4. Session refresh calls `getClaims()` from `proxy.ts`, or from `middleware.ts` only if Edge is required, and returns the response `setAll` last built.
5. A Server Action that must show its own write calls `updateTag`. No single-argument `revalidateTag(tag)`.
Changelog
Placeholder. This is the first published cut, last verified on 2026-11-04. Version notes will replace this block when the pack is revised.