Clerk + Next.js App Router
Purpose: enforce current, correct instructions for integrating Clerk in a Next.js App Router application.
Scope: all AI-generated Clerk-related advice and code should follow these guardrails.
1. Official integration overview
Use only the App Router approach from Clerk’s current documentation:
- Install
@clerk/nextjs@latest. - Create
src/proxy.ts(orproxy.tsat project root if there is nosrc) withclerkMiddleware()from@clerk/nextjs/server. - Wrap the app with
<ClerkProvider>inapp/layout.tsx(or an app-specific provider wrapper). - Use Clerk components such as
<SignInButton>,<SignUpButton>,<UserButton>,<SignedIn>, and<SignedOut>.
Reference quickstart: https://clerk.com/docs/nextjs/getting-started/quickstart
Quickstart sample
Install SDK:
npm install @clerk/nextjsEnvironment variables (.env.local):
NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY=YOUR_PUBLISHABLE_KEY
CLERK_SECRET_KEY=YOUR_SECRET_KEYCreate src/proxy.ts:
import { clerkMiddleware } from "@clerk/nextjs/server";
export default clerkMiddleware();
export const config = {
matcher: [
"/((?!_next|[^?]*\\.(?:html?|css|js(?!on)|jpe?g|webp|png|gif|svg|ttf|woff2?|ico|csv|docx?|xlsx?|zip|webmanifest)).*)",
"/(api|trpc)(.*)",
],
};Minimal app/layout.tsx pattern:
import { ClerkProvider } from "@clerk/nextjs";
export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
<ClerkProvider>
<html lang="en">
<body>{children}</body>
</html>
</ClerkProvider>
);
}2. Critical instructions for AI models
2.1 Always do the following
- Use
clerkMiddleware()from@clerk/nextjs/serverinproxy.ts. - Wrap the app with
<ClerkProvider>inapp/layout.tsx(directly or via a wrapper component). - Import Clerk features from
@clerk/nextjsor@clerk/nextjs/serveronly. - Use App Router conventions (
app/page.tsx,app/layout.tsx, etc.). - For server helpers such as
auth(), import from@clerk/nextjs/serverand use async/await correctly. - Store real keys only in
.env.localor deployment secrets (never in tracked files). - Use placeholders in examples (
YOUR_PUBLISHABLE_KEY,YOUR_SECRET_KEY).
2.2 Never do the following
- Do not reference
_app.tsxor Pages Router-only auth setup. - Do not suggest deprecated
authMiddleware(). - Do not recommend deprecated Clerk APIs or legacy environment variable patterns.
- Do not print real keys in code blocks, logs, markdown, or source files.
3. Outdated patterns to avoid
// Deprecated
import { authMiddleware } from "@clerk/nextjs";
// Pages Router pattern for modern App Router projects
function MyApp({ Component, pageProps }) {
return <Component {...pageProps} />;
}Any solution that resembles the above, or relies on pages/ auth flow, is incorrect for a modern Clerk App Router setup.
4. Verification checklist
Before returning any Clerk-related solution, verify:
clerkMiddleware()is configured inproxy.ts.- The app is wrapped with
<ClerkProvider>(directly or via wrapper). - Imports are from
@clerk/nextjsor@clerk/nextjs/serveronly. - App Router structure is used (not
_app.tsx/ Pages Router). - Examples contain only placeholder keys.
- Real keys are excluded from tracked files.
If any check fails, revise before finalizing.
5. Consequences of incorrect implementation
Using outdated Clerk methods (for example, authMiddleware(), _app.tsx, or old pages/ auth flow) can:
- Break authentication behavior.
- Cause App Router incompatibilities.
- Create server/client boundary errors.
- Increase maintenance and refactor effort.
6. Response template for AI
When answering Clerk + Next.js integration questions:
- Use the up-to-date App Router +
clerkMiddleware()pattern. - Avoid deprecated APIs or Pages Router instructions.
- Ensure verification checklist items are satisfied.