File: start-clerk-basic.md | Updated: 11/15/2025
Search...
+ K
Auto
Docs Examples GitHub Contributors
Docs Examples GitHub Contributors
Docs Examples GitHub Contributors
Docs Examples Github Contributors
Docs Examples Github Contributors
Docs Examples Github Contributors
Docs Examples Github Contributors
Docs Examples Github Contributors
Maintainers Partners Support Learn StatsBETA Discord Merch Blog GitHub Ethos Brand Guide
Documentation
Framework
React
Version
Latest
Search...
+ K
Menu
Getting Started
Guides
Examples
Tutorials
Framework
React
Version
Latest
Menu
Getting Started
Guides
Examples
Tutorials
React Example: Start Clerk Basic
==============================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================
Code ExplorerCode
Interactive SandboxSandbox
.vscode
public
src
components
routes
_authed
__root.tsx
_authed.tsx
index.tsx
styles
utils
routeTree.gen.ts
router.tsx
start.ts
.env
.gitignore
.prettierignore
README.md
package.json
postcss.config.mjs
tsconfig.json
vite.config.ts
tsx
/// <reference types="vite/client" />
import {
ClerkProvider,
SignInButton,
SignedIn,
SignedOut,
UserButton,
} from '@clerk/tanstack-react-start'
import { TanStackRouterDevtools } from '@tanstack/react-router-devtools'
import { createServerFn } from '@tanstack/react-start'
import { auth } from '@clerk/tanstack-react-start/server'
import * as React from 'react'
import {
HeadContent,
Link,
Outlet,
Scripts,
createRootRoute,
} from '@tanstack/react-router'
import { DefaultCatchBoundary } from '~/components/DefaultCatchBoundary.js'
import { NotFound } from '~/components/NotFound.js'
import appCss from '~/styles/app.css?url'
const fetchClerkAuth = createServerFn({ method: 'GET' }).handler(async () => {
const { userId } = await auth()
return {
userId,
}
})
export const Route = createRootRoute({
beforeLoad: async () => {
const { userId } = await fetchClerkAuth()
return {
userId,
}
},
head: () => ({
meta: [\
{\
charSet: 'utf-8',\
},\
{\
name: 'viewport',\
content: 'width=device-width, initial-scale=1',\
},\
],
links: [\
{ rel: 'stylesheet', href: appCss },\
{\
rel: 'apple-touch-icon',\
sizes: '180x180',\
href: '/apple-touch-icon.png',\
},\
{\
rel: 'icon',\
type: 'image/png',\
sizes: '32x32',\
href: '/favicon-32x32.png',\
},\
{\
rel: 'icon',\
type: 'image/png',\
sizes: '16x16',\
href: '/favicon-16x16.png',\
},\
{ rel: 'manifest', href: '/site.webmanifest', color: '#fffff' },\
{ rel: 'icon', href: '/favicon.ico' },\
],
}),
errorComponent: (props) => {
return (
<RootDocument>
<DefaultCatchBoundary {...props} />
</RootDocument>
)
},
notFoundComponent: () => <NotFound />,
component: RootComponent,
})
function RootComponent() {
return (
<ClerkProvider>
<RootDocument>
<Outlet />
</RootDocument>
</ClerkProvider>
)
}
function RootDocument({ children }: { children: React.ReactNode }) {
return (
<html>
<head>
<HeadContent />
</head>
<body>
<div className="p-2 flex gap-2 text-lg">
<Link
to="/"
activeProps={{
className: 'font-bold',
}}
activeOptions={{ exact: true }}
>
Home
</Link>{' '}
<Link
to="/posts"
activeProps={{
className: 'font-bold',
}}
>
Posts
</Link>
<div className="ml-auto">
<SignedIn>
<UserButton />
</SignedIn>
<SignedOut>
<SignInButton mode="modal" />
</SignedOut>
</div>
</div>
<hr />
{children}
<TanStackRouterDevtools position="bottom-right" />
<Scripts />
</body>
</html>
)
}
/// <reference types="vite/client" />
import {
ClerkProvider,
SignInButton,
SignedIn,
SignedOut,
UserButton,
} from '@clerk/tanstack-react-start'
import { TanStackRouterDevtools } from '@tanstack/react-router-devtools'
import { createServerFn } from '@tanstack/react-start'
import { auth } from '@clerk/tanstack-react-start/server'
import * as React from 'react'
import {
HeadContent,
Link,
Outlet,
Scripts,
createRootRoute,
} from '@tanstack/react-router'
import { DefaultCatchBoundary } from '~/components/DefaultCatchBoundary.js'
import { NotFound } from '~/components/NotFound.js'
import appCss from '~/styles/app.css?url'
const fetchClerkAuth = createServerFn({ method: 'GET' }).handler(async () => {
const { userId } = await auth()
return {
userId,
}
})
export const Route = createRootRoute({
beforeLoad: async () => {
const { userId } = await fetchClerkAuth()
return {
userId,
}
},
head: () => ({
meta: [\
{\
charSet: 'utf-8',\
},\
{\
name: 'viewport',\
content: 'width=device-width, initial-scale=1',\
},\
],
links: [\
{ rel: 'stylesheet', href: appCss },\
{\
rel: 'apple-touch-icon',\
sizes: '180x180',\
href: '/apple-touch-icon.png',\
},\
{\
rel: 'icon',\
type: 'image/png',\
sizes: '32x32',\
href: '/favicon-32x32.png',\
},\
{\
rel: 'icon',\
type: 'image/png',\
sizes: '16x16',\
href: '/favicon-16x16.png',\
},\
{ rel: 'manifest', href: '/site.webmanifest', color: '#fffff' },\
{ rel: 'icon', href: '/favicon.ico' },\
],
}),
errorComponent: (props) => {
return (
<RootDocument>
<DefaultCatchBoundary {...props} />
</RootDocument>
)
},
notFoundComponent: () => <NotFound />,
component: RootComponent,
})
function RootComponent() {
return (
<ClerkProvider>
<RootDocument>
<Outlet />
</RootDocument>
</ClerkProvider>
)
}
function RootDocument({ children }: { children: React.ReactNode }) {
return (
<html>
<head>
<HeadContent />
</head>
<body>
<div className="p-2 flex gap-2 text-lg">
<Link
to="/"
activeProps={{
className: 'font-bold',
}}
activeOptions={{ exact: true }}
>
Home
</Link>{' '}
<Link
to="/posts"
activeProps={{
className: 'font-bold',
}}
>
Posts
</Link>
<div className="ml-auto">
<SignedIn>
<UserButton />
</SignedIn>
<SignedOut>
<SignInButton mode="modal" />
</SignedOut>
</div>
</div>
<hr />
{children}
<TanStackRouterDevtools position="bottom-right" />
<Scripts />
</body>
</html>
)
}
