clerk-astro-patterns

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Astro Patterns

Astro模式

SDK:
@clerk/astro
v3+. Requires Astro 4.15+.
SDK:
@clerk/astro
v3+。要求Astro版本不低于4.15。

What Do You Need?

你需要什么?

TaskReference
Configure middlewarereferences/middleware.md
Protect SSR pagesreferences/ssr-pages.md
Use Clerk in island componentsreferences/island-components.md
Auth in API routesreferences/api-routes.md
Use Clerk with React in Astroreferences/astro-react.md
任务参考文档
配置中间件references/middleware.md
保护SSR页面references/ssr-pages.md
在孤岛组件中使用Clerkreferences/island-components.md
API路由鉴权references/api-routes.md
在Astro中搭配React使用Clerkreferences/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
Astro每个页面有两种渲染模式:SSR和静态预渲染。Clerk在两种模式下的工作逻辑不同:
  • SSR页面 —— 使用由中间件填充的
    Astro.locals.auth()
  • 静态页面
    export const prerender = true
    )—— Clerk中间件会跳过这类页面;请在孤岛组件中使用客户端钩子
  • 孤岛组件 —— 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

常见问题

SymptomCauseFix
Astro.locals.auth
is undefined
Missing middlewareAdd
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 authPrerendered pages skip middlewareUse
export const prerender = false
or move to island
Island not reactive to sign-inMissing
client:load
directive
Add
client:load
to the island component
现象原因解决方法
Astro.locals.auth
未定义
缺少中间件配置
src/middleware.ts
中添加
clerkMiddleware
鉴权在开发环境正常但生产环境失效全局配置了
output: 'static'
将受保护页面的
output
设置为
server
hybrid
静态页面无鉴权能力预渲染页面会跳过中间件处理设置
export const prerender = false
或将鉴权逻辑移到孤岛组件中
孤岛组件无法响应登录状态变化缺少
client:load
指令
给孤岛组件添加
client:load
属性

Import Map

导入映射

WhatImport From
clerkMiddleware
,
createRouteMatcher
@clerk/astro/server
useAuth
,
useUser
,
UserButton
@clerk/astro/react
Astro components (
<SignIn>
, etc.)
@clerk/astro/components
内容导入来源
clerkMiddleware
,
createRouteMatcher
@clerk/astro/server
useAuth
,
useUser
,
UserButton
@clerk/astro/react
Astro组件(
<SignIn>
等)
@clerk/astro/components

Env Variables

环境变量

undefined
undefined

.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

相关参考

  • clerk-setup
    - Initial Clerk install
  • clerk-custom-ui
    - Custom flows & appearance
  • clerk-orgs
    - B2B organizations
  • clerk-setup
    - Clerk初始安装
  • clerk-custom-ui
    - 自定义流程与外观
  • clerk-orgs
    - B2B组织管理

Docs

官方文档