clerk-astro-patterns
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseAstro Patterns
Astro模式
SDK: v3+. Requires Astro 4.15+.
@clerk/astroSDK: v3+。要求Astro版本不低于4.15。
@clerk/astroWhat Do You Need?
你需要什么?
| Task | Reference |
|---|---|
| Configure middleware | 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 |
| 任务 | 参考文档 |
|---|---|
| 配置中间件 | references/middleware.md |
| 保护SSR页面 | references/ssr-pages.md |
| 在孤岛组件中使用Clerk | references/island-components.md |
| API路由鉴权 | references/api-routes.md |
| 在Astro中搭配React使用Clerk | 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 which is populated by the middleware
Astro.locals.auth() - Static pages () — Clerk middleware skips them; use client-side hooks in islands
export const prerender = true - Islands — React/Vue/Svelte components; use and other hooks from
useAuth()@clerk/astro/react
Request → clerkMiddleware() → SSR page → Astro.locals.auth()
↓
Island (.client) → useAuth() hookAstro每个页面有两种渲染模式:SSR和静态预渲染。Clerk在两种模式下的工作逻辑不同:
- SSR页面 —— 使用由中间件填充的
Astro.locals.auth() - 静态页面()—— Clerk中间件会跳过这类页面;请在孤岛组件中使用客户端钩子
export const prerender = true - 孤岛组件 —— React/Vue/Svelte组件;使用提供的
@clerk/astro/react和其他钩子useAuth()
请求 → clerkMiddleware() → SSR页面 → Astro.locals.auth()
↓
孤岛(.client) → useAuth()钩子Setup
环境配置
astro.config.mjs
astro.config.mjs
ts
import { defineConfig } from 'astro/config'
import clerk from '@clerk/astro'
export default defineConfig({
integrations: [clerk()],
output: 'server',
})ts
import { defineConfig } from 'astro/config'
import clerk from '@clerk/astro'
export default defineConfig({
integrations: [clerk()],
output: 'server',
})src/middleware.ts
src/middleware.ts
ts
import { clerkMiddleware, createRouteMatcher } from '@clerk/astro/server'
const isProtectedRoute = createRouteMatcher(['/dashboard(.*)'])
export const onRequest = clerkMiddleware((auth, context, next) => {
if (isProtectedRoute(context.request) && !auth().userId) {
return auth().redirectToSignIn()
}
return next()
})ts
import { clerkMiddleware, createRouteMatcher } from '@clerk/astro/server'
const isProtectedRoute = createRouteMatcher(['/dashboard(.*)'])
export const onRequest = clerkMiddleware((auth, context, next) => {
if (isProtectedRoute(context.request) && !auth().userId) {
return auth().redirectToSignIn()
}
return next()
})SSR Page Auth
SSR页面鉴权
astro
---
const { userId, orgId } = Astro.locals.auth()
if (!userId) return Astro.redirect('/sign-in')
---
<h1>Dashboard</h1>astro
---
const { userId, orgId } = Astro.locals.auth()
if (!userId) return Astro.redirect('/sign-in')
---
<h1>Dashboard</h1>Common Pitfalls
常见问题
| Symptom | Cause | Fix |
|---|---|---|
| Missing middleware | Add |
| Auth works in dev but not production | | Set |
| Static page has no auth | Prerendered pages skip middleware | Use |
| Island not reactive to sign-in | Missing | Add |
| 现象 | 原因 | 解决方法 |
|---|---|---|
| 缺少中间件配置 | 在 |
| 鉴权在开发环境正常但生产环境失效 | 全局配置了 | 将受保护页面的 |
| 静态页面无鉴权能力 | 预渲染页面会跳过中间件处理 | 设置 |
| 孤岛组件无法响应登录状态变化 | 缺少 | 给孤岛组件添加 |
Import Map
导入映射
| What | Import From |
|---|---|
| |
| |
Astro components ( | |
| 内容 | 导入来源 |
|---|---|
| |
| |
Astro组件( | |
Env Variables
环境变量
undefinedundefined.env
.env
PUBLIC_CLERK_PUBLISHABLE_KEY=pk_...
CLERK_SECRET_KEY=sk_...
Astro uses `PUBLIC_` prefix for client-exposed variables (not `NEXT_PUBLIC_`).PUBLIC_CLERK_PUBLISHABLE_KEY=pk_...
CLERK_SECRET_KEY=sk_...
Astro对客户端暴露的变量使用`PUBLIC_`前缀(而非`NEXT_PUBLIC_`)。See Also
相关参考
- - Initial Clerk install
clerk-setup - - Custom flows & appearance
clerk-custom-ui - - B2B organizations
clerk-orgs
- - Clerk初始安装
clerk-setup - - 自定义流程与外观
clerk-custom-ui - - B2B组织管理
clerk-orgs