clerk-astro-patterns
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.
Security Assessment
Detected risks:
About clerk-astro-patterns
The `clerk-astro-patterns` skill provides developers with a structured approach to integrating Clerk authentication into Astro projects. It addresses the challenge of managing authentication across different rendering modes and component types in Astro applications. By offering middleware, SSR page protection, API route authentication, and integration with island components, this skill ensures secure user management while maintaining the performance benefits of Astro's hybrid rendering capabilities.
Key features include the setup and configuration of Clerk middleware to protect server-side rendered (SSR) pages, using `Astro.locals.auth()` for server-side authentication data, and client-side hooks like `useAuth()` for reactive island components. It also covers integration with React components within Astro islands, managing static vs SSR rendering, and handling API route authentication securely. The skill provides clear guidance on common pitfalls such as missing middleware, static page restrictions, and non-reactive islands, along with recommended fixes.
This skill is particularly useful for developers building web applications with Astro 4.15+ who require robust authentication and user management. It is suitable for teams implementing protected dashboards, user-specific content, or B2B features. Target users include front-end developers familiar with Astro and React, and anyone seeking to streamline authentication flows in hybrid SSR and static sites while adhering to best practices for middleware and client-side state management.
FAQ
Which versions of Astro and @clerk/astro are compatible with this skill?
The skill requires Astro version 4.15 or higher and @clerk/astro version 3 or higher.
How do I protect server-side rendered pages?
Use the `clerkMiddleware` in `src/middleware.ts` and access authentication data via `Astro.locals.auth()` within your SSR pages.
Can I use this skill with static prerendered pages?
Static prerendered pages skip the Clerk middleware, so you should use client-side hooks in island components or set `export const prerender = false` to enable server-side authentication.
What should I do if an island component is not reactive to sign-in changes?
Ensure the island component includes the `client:load` directive to enable reactive client-side behavior.
Which environment variables are required for Clerk?
You need `PUBLIC_CLERK_PUBLISHABLE_KEY` for client-side usage and `CLERK_SECRET_KEY` for server-side authentication, defined in a `.env` file.
All Files
13 filesInstall clerk-astro-patterns
Quick Setup:
- Copy the skill folder to
.claude/skills/ - Claude will automatically detect and use the skill
Repository
clerk/skills