跳到主内容
智客 ZICQ

技能库 智客分类:Agent 工作流 clerk-react-router-patterns

Clerk React Router Patterns

'React Router v7/v8 模式与 Clark —— rootAuthLoader,在加载器中获取Auth,

14715 安装量

官方网址:skills.sh

技能介绍

先看中文介绍;官方 description 原文单独保留,不改写 SKILL.md。

做什么

'React Router v7/v8 模式与 Clark —— rootAuthLoader,在加载器中获取Auth,

何时用

官方 description 未单独写出 Use when。按规范,代理会在用户任务与这段 description 的关键词匹配时激活本技能。

代理如何加载

按 Agent Skills 渐进披露:启动时只加载 name 与 description(约 100 token);任务匹配后才读入整份 SKILL.md 正文;scripts/、references/、assets/ 仅在需要时再读。 本文件正文结构:React Router Patterns、What Do You Need?、React Router v7 vs v8、Minimal Setup、1. vite.config.ts (v8 only — REQUIRED)、2. root.tsx。

文件分析

文件分析:除 SKILL.md 外,正文引用了 references/loaders-actions.md、references/protected-routes.md、references/ssr-auth.md,属于带资源的技能包,这些文件按需再读。

官方 description(原文)

'React Router v7/v8 patterns with Clerk — rootAuthLoader, getAuth in loaders,

React Router PatternsWhat Do You Need?React Router v7 vs v8Minimal Setup1. vite.config.ts (v8 only — REQUIRED)2. root.tsx3. react-router.config.ts (v7 only)Mental ModelAuth in LoadersAuth in ActionsClient ComponentsOrg Switching

· 许可:MIT · allowed-tools:WebFetch

来源分类:skills.sh agent-skill

SKILL.md 与 Agent 调用

官方规范 ↗
name
clerk-react-router-patterns
description
'React Router v7/v8 patterns with Clerk — rootAuthLoader, getAuth in loaders,
allowed-tools
WebFetch实验字段,支持情况取决于客户端;字段声明本身不会授予工具权限。
许可
MIT
  1. 发现技能客户端向 Agent 提供名称与描述目录。
  2. 匹配与调用用户指定或任务匹配后,载入 SKILL.md 指令。
  3. 按需加载按步骤读取参考文档、使用脚本与素材。
指令中引用的文件 · 3
  • references/loaders-actions.md
  • references/protected-routes.md
  • references/ssr-auth.md

以下路径提取自原文;文件是否齐全请以来源仓库中的完整目录为准。

具体调用语法与可用工具以目标 Agent 客户端为准。 查看调用机制说明 ↗

安装这个技能

Skills CLI ↗

先选择目标 Agent 和安装范围,保留技能包的附属文件,安装后检查客户端能否发现该技能。

该技能引用了附属文件,请从来源获取完整目录;仅复制 SKILL.md 可能缺少依赖。

交给 Agent 安装

复制安装指令给支持 Agent Skills 的代理,确认其中的目标目录与客户端匹配。

把 Agent Skill「clerk-react-router-patterns」安装到我的项目:SKILL.md 原文与官方 description 见 https://zicq.com/zh/skills/skl-45e02e51ea521240-Clerk-React-Router-Patterns.html
请存为 .cursor/skills/clerk-react-router-patterns/SKILL.md 或 .claude/skills/clerk-react-router-patterns/SKILL.md,frontmatter 的 name 与 description 保持原样,不要改写。
该技能还带 scripts/、references/、assets/ 等文件,请从 https://github.com/clerk/skills 取完整目录,不要只建一个 SKILL.md。

GitHub 完整包 ↗

终端安装 · Skills CLI

需要 Node.js 与 npx。先查看仓库技能列表,确认实际名称。

npx skills add 'https://github.com/clerk/skills' --list

npx skills add 'https://github.com/clerk/skills' --skill 'clerk-react-router-patterns'

CLI 会交互选择目标 Agent,默认安装到项目;用户级安装使用 -g。先通过查看命令核对仓库内容,再用 npx skills list 检查已安装技能。

阅读排版
--- name: clerk-react-router-patterns description: 'React Router v7/v8 patterns with Clerk — rootAuthLoader, getAuth in loaders, clerkMiddleware, protected routes, SSR user data, org switching. Triggers on: react-router auth, rootAuthLoader, getAuth loader, react-router protected route, loader authentication, SSR auth react-router, useNavigate may be used only in the context of a Router.' license: MIT allowed-tools: WebFetch metadata: author: clerk version: 1.1.0 --- # React Router Patterns SDK: `@clerk/react-router` v3.5+. Supports React Router v7.9+ and v8. ## What Do You Need? | Task | Reference | |------|-----------| | Auth in loaders and actions | references/loaders-actions.md | | Protected routes and redirects | references/protected-routes.md | | SSR user data and session | references/ssr-auth.md | ## React Router v7 vs v8 Check the installed `react-router` major version before scaffolding — the config differs: | | v7.9+ | v8+ | |--|--|--| | Middleware API | Opt-in: set `future: { v8_middleware: true }` in `react-router.config.ts` | Always on — do NOT set the flag (v8 removed it) | | `ssr.noExternal` workaround (below) | Not needed | **Required** | ## Minimal Setup ### 1. vite.config.ts (v8 only — REQUIRED) React Router v8 ships development/production conditional exports. In `react-router dev`, Vite externalizes `@clerk/react-router` for SSR, so Node resolves the production build of react-router while the app code gets the development build — two module instances, two Router contexts. Every request then fails during SSR with: ``` Error: useNavigate() may be used only in the context of a component. ``` **`npm ls react-router` shows a single copy — that does NOT rule this out.** The duplication is per export condition, not per installed copy. Do not chase duplicate installs; add the workaround (upstream issue: https://github.com/remix-run/react-router/issues/15232): ```ts import { reactRouter } from '@react-router/dev/vite' import { defineConfig } from 'vite' export default defineConfig({ plugins: [reactRouter()], ssr: { noExternal: ['@clerk/react-router'], }, }) ``` ### 2. root.tsx ```tsx import { Outlet } from 'react-router' import { rootAuthLoader, clerkMiddleware } from '@clerk/react-router/server' import { ClerkProvider } from '@clerk/react-router' import type { Route } from './+types/root' export const middleware: Route.MiddlewareFunction[] = [clerkMiddleware()] export async function loader(args: Route.LoaderArgs) { return rootAuthLoader(args) } export default function App({ loaderData }: Route.ComponentProps) { return ( ) } ``` There is no `ClerkApp` HOC in `@clerk/react-router` (that was the `@clerk/remix` API). Render `` inside the default export and pass it the root route's `loaderData`. ### 3. react-router.config.ts (v7 only) ```ts import type { Config } from '@react-router/dev/config' export default { future: { v8_middleware: true, }, } satisfies Config ``` On v8, omit the `future` block entirely — the flag no longer exists. > **Required**: `rootAuthLoader` must be called in `root.tsx`'s loader. Without it, `getAuth` throws in nested loaders. ## Mental Model React Router v7/v8 uses a middleware + loader pipeline. Clerk plugs into both layers: - **Middleware** (`clerkMiddleware()`) — runs on every request, attaches auth to context - **`rootAuthLoader`** — required in `root.tsx` to pass Clerk state to the client - **`getAuth(args)`** — called inside any loader/action to get the current user ``` Request → clerkMiddleware() → rootAuthLoader → page loader → component ↓ ↓ ↓ attaches auth injects state getAuth(args) to context to response reads context ``` ## Auth in Loaders ```tsx import { getAuth } from '@clerk/react-router/server' import type { Route } from './+types/dashboard' export async function loader(args: Route.LoaderArgs) { const { userId } = await getAuth(args) if (!userId) throw redirect('/sign-in') const data = await fetchUserData(userId) return { data } } ``` ## Auth in Actions ```tsx import { getAuth } from '@clerk/react-router/server' export async function action(args: Route.ActionArgs) { const { userId, orgId } = await getAuth(args) if (!userId) throw new Response('Unauthorized', { status: 401 }) const formData = await args.request.formData() await saveData(userId, orgId, formData) return redirect('/dashboard') } ``` ## Client Components ```tsx import { useAuth, useUser } from '@clerk/react-router' export function Profile() { const { userId, isSignedIn } = useAuth() const { user } = useUser() if (!isSignedIn) return null return

{user?.firstName}

} ``` ## Org Switching ```tsx import { OrganizationSwitcher } from '@clerk/react-router' export function Nav() { return } ``` ```tsx export async function loader(args: Route.LoaderArgs) { const { userId, orgId } = await getAuth(args) if (!userId) throw redirect('/sign-in') if (!orgId) throw redirect('/select-org') return { data: await fetchOrgData(orgId) } } ``` ## Common Pitfalls | Symptom | Cause | Fix | |---------|-------|-----| | `useNavigate() may be used only in the context of a ` thrown from ClerkProvider during SSR in dev (v8) | Vite dev SSR externalizes `@clerk/react-router`, which then loads react-router's production build while the app uses the development build — two Router contexts. A single copy in `npm ls` does not rule this out. | Add `ssr: { noExternal: ['@clerk/react-router'] }` to `vite.config.ts`. Do NOT downgrade to v7 | | Build error: `ClerkApp` is not exported | `ClerkApp` does not exist in `@clerk/react-router` | Use `` in root.tsx's default export | | `clerkMiddleware() not detected` | Missing middleware (or on v7, missing `v8_middleware` future flag) | Export `middleware = [clerkMiddleware()]` from root route; on v7 also set `future: { v8_middleware: true }` | | Unknown future flag error/warning (v8) | `v8_middleware` flag left in `react-router.config.ts` after upgrading | Remove the `future.v8_middleware` entry — middleware is always on in v8 | | `getAuth` returns empty userId | `rootAuthLoader` not called | Call `rootAuthLoader(args)` in `root.tsx` loader | | Infinite redirect loop | Redirect target is also protected | Exclude `/sign-in` from protection check | | `redirect` not working in action | Using `Response` instead of `throw redirect()` | Use `throw redirect('/path')` from `react-router` | ## Import Map | What | Import From | |------|-------------| | `getAuth` | `@clerk/react-router/server` | | `rootAuthLoader` | `@clerk/react-router/server` | | `clerkMiddleware` | `@clerk/react-router/server` | | `ClerkProvider` | `@clerk/react-router` | | `useAuth`, `useUser` | `@clerk/react-router` | | `OrganizationSwitcher` | `@clerk/react-router` | ## See Also - `clerk-setup` - Initial Clerk install - `clerk-custom-ui` - Custom flows & appearance - `clerk-orgs` - B2B organizations ## Docs [React Router SDK](https://clerk.com/docs/react-router/getting-started/quickstart)

相关技能

Agent 工作流

Skill Creator

创造有效技能指南。 当用户想创造出新的技能(或更新现有的技能),以专业知识,工作流程,或工具集成来扩展克洛德的能力时,应该使用这种技能.

Agent 工作流

Clawdhub

使用ClawdHub CLI搜索,安装,更新并发布从taladhub.com的代理技能. 需要获取苍蝇上的新技能时使用,将安装的技能同步到最新版本或特定版本,或者发布 npm-instainddhub CLI 的新/更新的技能文件夹.

Agent 工作流

Agent Team Orchestration

管弦乐团多代理团队,任务设定周期,交接协议,审查工作流程. 使用时间: (1)建立2+特派员队伍,具有不同专业,(2)确定任务路线和生命周期(收录框_ spec_建设_审查_完成),(3)在特派员之间制定交接协议,(4)建立审查和质量关口,(5)管理特派员之间的交流和文物共享.

Agent 工作流

Superpowers

Spec-first,TDD,子代理驱动的软件开发工作流程. 当:(1)构建任何新功能或应用——触发脑暴_计划_子代理执行回路,(2)调试出一个bug或测试失败——触发系统性的根起过程,(3)用户说"让我们构建","帮助我计划","我想添加X",或"这个被打破",(4)完成一个功…