Skip to main content
ZICQ

Skills ZICQ category:Agent Workflows clerk-react-router-patterns

Clerk React Router Patterns

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

14715 installs

Official URL:skills.sh

What this skill does

Intro in this page language first. The official description stays in its original wording; we do not rewrite SKILL.md.

What it does

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

When to use it

The official description does not include a separate “Use when”. Per the spec, agents activate this skill when the task matches keywords in that description.

How agents load it

Per Agent Skills progressive disclosure: name and description load at startup (~100 tokens); the full SKILL.md body loads when the skill activates; scripts/, references/, and assets/ load only as needed. This file's sections: React Router Patterns; What Do You Need?; React Router v7 vs v8; Minimal Setup; 1. vite.config.ts (v8 only — REQUIRED); 2. root.tsx.

File analysis

File analysis: besides SKILL.md, the body references references/loaders-actions.md, references/protected-routes.md, references/ssr-auth.md. Those resources load on demand.

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

· License:MIT · allowed-tools:WebFetch

Source category:skills.sh agent-skill

SKILL.md & Agent activation

Official spec ↗
name
clerk-react-router-patterns
description
'React Router v7/v8 patterns with Clerk — rootAuthLoader, getAuth in loaders,
allowed-tools
WebFetchExperimental field; support depends on the client and does not grant permissions by itself.
License
MIT
  1. DiscoverThe client exposes names and descriptions to the agent.
  2. ActivateYour request or the task context selects the skill and loads its instructions.
  3. Load resourcesReferenced scripts, documentation and assets are used when needed.
Files referenced by the instructions · 3
  • references/loaders-actions.md
  • references/protected-routes.md
  • references/ssr-auth.md

These paths are extracted from the text. Check the upstream package to verify the files exist.

Invocation syntax and available tools depend on your Agent client. Client integration guide ↗

Install this skill

Skills CLI ↗

Choose the target agent and installation scope, keep referenced package files, then verify the skill appears in the client's catalog.

This skill references supporting files. Retrieve the complete directory from the source; copying SKILL.md alone may leave missing dependencies.

Ask your Agent to install

Copy these instructions to a compatible agent and confirm the target directory matches your client.

Install the agent skill "clerk-react-router-patterns" into my project. The full SKILL.md and official description are at https://zicq.com/en/skills/skl-45e02e51ea521240-Clerk-React-Router-Patterns.html
Save it as .cursor/skills/clerk-react-router-patterns/SKILL.md or .claude/skills/clerk-react-router-patterns/SKILL.md and keep the frontmatter name and description exactly as-is.
This skill also ships scripts/, references/, or assets/ — fetch the whole folder from https://github.com/clerk/skills instead of creating only a SKILL.md.

Full package on GitHub ↗

Install from the terminal · Skills CLI

Requires Node.js and npx. First inspect the repository's skill list to confirm the name.

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

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

The CLI lets you choose the agent interactively. The default scope is the project; use -g for user scope. Confirm package availability with the discovery command, then use npx skills list to inspect installed skills.

Readable layout
--- 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)

Related skills

Agent Workflows

Skill Creator

Guide for creating effective skills. This skill should be used when users want to create a new skill (or update an existing skill) that exte…

Agent Workflows

Clawdhub

Use the ClawdHub CLI to search, install, update, and publish agent skills from clawdhub.com. Use when you need to fetch new skills on the fl…

Agent Workflows

Agent Team Orchestration

Orchestrate multi-agent teams with defined roles, task lifecycles, handoff protocols, and review workflows. Use when: (1) Setting up a team …

Agent Workflows

Superpowers

Spec-first, TDD, subagent-driven software development workflow. Use when: (1) building any new feature or app — triggers brainstorm → plan →…