clerk-astro-patterns — Claude Code skill for Clerk in Astro
Walks your agent through Clerk inside Astro: middleware, protected SSR pages, API route auth and island components.
npx skills add clerk/skills --skill clerk-astro-patterns -a claude-codeWhat clerk-astro-patterns does
The skill covers how Clerk auth behaves across Astro's two rendering modes. SSR pages read Astro.locals.auth(), filled in by the middleware. Static pages with export const prerender = true skip that middleware entirely, so auth there lives in client-side hooks inside islands.
Inside are task-based references: middleware setup, SSR page protection, Clerk in island components, auth in API routes, and React in Astro. A pitfalls table maps symptoms to causes. Astro.locals.auth comes back undefined when src/middleware.ts has no clerkMiddleware; an island that ignores sign-in usually just lacks the client:load directive.
You need @clerk/astro v4+ and Astro 5+. Two env vars go in .env: PUBLIC_CLERK_PUBLISHABLE_KEY and CLERK_SECRET_KEY. Astro exposes client variables with the PUBLIC_ prefix, not NEXT_PUBLIC_. If your codebase still calls createRouteMatcher, that is worth checking before you upgrade: the function was removed in @clerk/astro 4.0, and those checks have to move into pages and API routes.
What it helps with
- protect an SSR page and redirect signed-out visitors
- check the user inside an API route and return 401
- wire Clerk into a React island component
- debug auth that works in dev but fails in production
- migrate off
createRouteMatcherto per-page checks
Example prompts for the agent
- Set up clerkMiddleware in src/middleware.ts and show me how to protect an SSR page
- Add a userId check to src/pages/api/data.ts and return 401 without a session
- Why does my island ignore sign-in? Check the client:load directive
How to install clerk-astro-patterns
1Skills CLI (Vercel)
npx skills add clerk/skills --skill clerk-astro-patterns -a claude-code2Manually: put the files in place
~/.claude/skills/clerk-astro-patterns/SKILL.mdCopy the whole skill folder; for one project use .claude/skills/clerk-astro-patterns/
1Skills CLI (Vercel)
npx skills add clerk/skills --skill clerk-astro-patterns -a codex2Manually: put the files in place
.agents/skills/clerk-astro-patterns/SKILL.mdFor all projects use ~/.agents/skills/
1Skills CLI (Vercel)
npx skills add clerk/skills --skill clerk-astro-patterns -a cursor2Manually: put the files in place
.cursor/skills/clerk-astro-patterns/SKILL.mdCursor also reads .agents/skills/ and .claude/skills/
1GitHub CLI
gh skill install clerk/skills clerk-astro-patterns2Manually: put the files in place
.github/skills/clerk-astro-patterns/SKILL.md1Skills CLI (Vercel)
npx skills add clerk/skills --skill clerk-astro-patterns -a gemini-cli2Platform CLI
gemini skills install https://github.com/clerk/skills --path skills/clerk-astro-patternsManually: put the files in place
.devin/skills/clerk-astro-patterns/SKILL.mdThe legacy .windsurf/skills/ path still works
Skills CLI (Vercel)
npx skills add clerk/skills --skill clerk-astro-patterns -a opencodeClaude.ai
Zip the folder and upload it in Settings → Capabilities → Skills
Step by step, with commands for each platform: How to install skills in Claude Code: five ways
Questions about clerk-astro-patterns
How do I install clerk-astro-patterns in Claude?
Run npx skills add clerk/skills --skill clerk-astro-patterns -a claude-code. The skill lands in ~/.claude/skills/clerk-astro-patterns/SKILL.md.
What does clerk-astro-patterns do?
It gives your agent ready patterns for Clerk in Astro: middleware, SSR page protection, API route auth and hooks in island components.
Which versions does the skill require?
@clerk/astro v4+ and Astro 5+. Since createRouteMatcher was removed in @clerk/astro 4.0, older checks need to move into pages and API routes.
Source text
SKILL.md · MIT
---
name: clerk-astro-patterns
description: 'Astro patterns with Clerk — middleware, SSR pages, island components,
API routes, static vs SSR rendering. Triggers on: astro clerk, clerk astro middleware,
astro protected page, clerk island component, astro API route auth, clerk astro
SSR.'
license: MIT
allowed-tools: WebFetch
metadata:
author: clerk
version: 1.0.0
---
# Astro Patterns
SDK: `@clerk/astro` v4+. Requires Astro 5+.
## What Do You Need?
| Task | Reference |
|------|-----------|
| Configure middleware (bare `clerkMiddleware()`, checks per page) | references/middleware.md |
| Protect SSR pages | references/ssr-pages.md |
| Use Clerk in island components | references/island-components.md |
| Auth in API routes | references/api-routes.md |
| Use Clerk with React in Astro | references/astro-react.md |
## Mental Model
Astro has two rendering modes per page: **SSR** and **static prerender**. Clerk works differently in each:
- **SSR pages** — use `Astro.locals.auth()` which is populated by the middleware
- **Static pages** (`export const prerender = true`) — Clerk middleware skips them; use client-side hooks in islands
- **Islands** — React/Vue/Svelte components; use `useAuth()` and other hooks from `@clerk/astro/react`
```
Request → clerkMiddleware() → SSR page → Astro.locals.auth()
↓
Island (.client) → useAuth() hook
```
## Setup
### astro.config.mjs
```ts
import { defineConfig } from 'astro/config'
import clerk from '@clerk/astro'
export default defineConfig({
integrations: [clerk()],
output: 'server',
})
```
### src/middleware.ts
```ts
import { clerkMiddleware } from '@clerk/astro/server'
export const onRequest = clerkMiddleware()
```
The middleware populates `Astro.locals.auth()`. Each page and API route checks for itself. `createRouteMatcher` was removed in `@clerk/astro` 4.0 (deprecated in 3.x). Don't add it. Existing uses must move to page and API-route checks before upgrading. See the [Astro `clerkMiddleware()` reference](https://clerk.com/docs/reference/astro/clerk-middleware).
## SSR Page Auth
```astro
---
const { userId, orgId } = Astro.locals.auth()
if (!userId) return Astro.redirect('/sign-in')
---
<h1>Dashboard</h1>
```
## API Route Auth
```ts
// src/pages/api/data.ts
import type { APIRoute } from 'astro'
export const GET: APIRoute = ({ locals }) => {
const { userId } = locals.auth()
if (!userId) return new Response('Unauthorized', { status: 401 })
return Response.json({ userId })
}
```
## Common Pitfalls
| Symptom | Cause | Fix |
|---------|-------|-----|
| `Astro.locals.auth` is undefined | Missing middleware | Add `clerkMiddleware` to `src/middleware.ts` |
| Auth works in dev but not production | `output: 'static'` globally | Set `output: 'server'` or `hybrid` for protected pages |
| Static page has no auth | Prerendered pages skip middleware | Use `export const prerender = false` or move to island |
| Island not reactive to sign-in | Missing `client:load` directive | Add `client:load` to the island component |
## Import Map
| What | Import From |
|------|-------------|
| `clerkMiddleware`, `clerkClient` | `@clerk/astro/server` |
| `useAuth`, `UserButton` (no `useUser`) | `@clerk/astro/react` |
| `$userStore`, `$authStore` | `@clerk/astro/client` |
| Astro components (`<SignIn>`, etc.) | `@clerk/astro/components` |
## Env Variables
```
# .env
PUBLIC_CLERK_PUBLISHABLE_KEY=pk_...
CLERK_SECRET_KEY=sk_...
```
Astro uses `PUBLIC_` prefix for client-exposed variables (not `NEXT_PUBLIC_`).
## See Also
- `clerk-setup` - Initial Clerk install
- `clerk-custom-ui` - Custom flows & appearance
- `clerk-orgs` - B2B organizations
## Docs
[Astro SDK](https://clerk.com/docs/astro/getting-started/quickstart)
© clerk · MIT · clerk/skills