I added a private account page to a Next.js 16 app with Better Auth, and npm run build stopped with Error: Route "/account": Next.js encountered uncached or runtime data during prerendering. The page read the session at its top, and Next.js 16 doesn’t allow that there. Here’s what the error means, and the small change that fixes it.
The short answer: move the code that reads the session (or anything else from the request, such as headers() or cookies()) into its own component, and wrap that component in <Suspense>.
What I Was Doing
I was building the account page of the Bookshop, the example app of the Better Auth Tutorial. It’s a Next.js 16.4.6 app made with create-next-app’s recommended settings, which switch on Cache Components (cacheComponents: true in next.config.ts). I followed Better Auth’s Next.js guide and read the session at the top of the page:
app/account/page.tsx
import { headers } from "next/headers";
import { redirect } from "next/navigation";
import { auth } from "@/lib/auth";
export default async function AccountPage() {
const session = await auth.api.getSession({
headers: await headers(),
});
if (!session) {
redirect("/sign-in");
}
return (
<main className="mx-auto w-full max-w-3xl flex-1 px-6 py-12">
<h1 className="text-3xl font-semibold">Your account</h1>
<p className="mt-4">Name: {session.user.name}</p>
<p>Email: {session.user.email}</p>
</main>
);
}
What I Expected
The page to show the signed-in person’s name and email address, and npm run build to finish.
What I Got
The page itself worked: signed out, it sent me to the sign-in page, and signed in, it showed Lena Park’s details. But the Next.js indicator in the corner of the page turned red and said 1 Issue. Clicking it opened this:
Terminal showed the same error from the dev server:
Output (the lines that matter)
Error: Route "/account": Next.js encountered uncached data during prerendering.
`fetch(...)` or `connection()` accessed outside of `<Suspense>` prevents the route from being prerendered, blocking the page load and leading to a slower user experience.
Ways to fix this:
- [stream] Provide a placeholder with `<Suspense fallback={...}>` around the data access
- [cache] Cache the data access with `"use cache"` (does not apply to `connection()`)
- [block] Set `export const instant = false` to allow a blocking route
Learn more: https://nextjs.org/docs/messages/blocking-prerender-dynamic
at AccountPage (app/account/page.tsx:7:27)
5 | export default async function AccountPage() {
6 | const session = await auth.api.getSession({
> 7 | headers: await headers(),
| ^
8 | });
9 |
10 | if (!session) {
And the production build stopped with exit code 1:
Terminal
npm run build
Output (the lines that matter)
Error: Route "/account": Next.js encountered uncached or runtime data during prerendering.
`fetch(...)`, `cookies()`, `headers()`, `params`, `searchParams`, or `connection()` accessed outside of `<Suspense>` prevents the route from being prerendered, blocking the page load and leading to a slower user experience.
Ways to fix this:
- [stream] Provide a placeholder with `<Suspense fallback={...}>` around the data access
- [cache] For uncached data (`fetch`, database calls): cache the access with `"use cache"` (does not apply to `connection()`)
- [block] Set `export const instant = false` to allow a blocking route
Learn more: https://nextjs.org/docs/messages/blocking-prerender-dynamic
…
Error occurred prerendering page "/account". Read more: https://nextjs.org/docs/messages/prerender-error
Export encountered an error on /account/page: /account, exiting the build.
⨯ Next.js build worker exited with code: 1 and signal: null
What I Checked
The error’s own message points at the cause: headers() was “accessed outside of <Suspense>”. Next.js’s Caching docs list headers() and cookies() as runtime APIs, which need information that only exists when someone makes a request, and say that components using them should be wrapped in <Suspense>. The Authentication with Cache Components guide is even more direct: with Cache Components, reading the session outside a <Suspense> boundary is a build error.
auth.api.getSession reads the session cookie from the request’s headers, so the page broke that rule on its very first line.
Why It Happens
With Cache Components, Next.js prerenders a static shell of every page when it builds the app, and streams the parts that need the request into that shell later. A page that waits for headers() at its top level makes the whole page depend on the request, so there’s no shell left to prerender. Next.js stops the build rather than quietly serving a slower page.
The Fix
Split the page in two. The page itself shows what doesn’t depend on the visitor, and a separate component reads the session inside <Suspense>:
app/account/page.tsx
import { headers } from "next/headers";
import { redirect } from "next/navigation";
import { Suspense } from "react";
import { auth } from "@/lib/auth";
export default function AccountPage() {
return (
<main className="mx-auto w-full max-w-3xl flex-1 px-6 py-12">
<h1 className="text-3xl font-semibold">Your account</h1>
<Suspense fallback={<p className="mt-4">Loading your account…</p>}>
<AccountDetails />
</Suspense>
</main>
);
}
async function AccountDetails() {
const session = await auth.api.getSession({
headers: await headers(),
});
if (!session) {
redirect("/sign-in");
}
return (
<div className="mt-4">
<p>Name: {session.user.name}</p>
<p>Email: {session.user.email}</p>
</div>
);
}
AccountPageno longer reads anything from the request, so Next.js can prerender its heading.AccountDetailsis the same session check as before, moved into its ownasynccomponent.<Suspense>shows itsfallback, “Loading your account…”, untilAccountDetailsis ready.
Run the build again:
Terminal
npm run build
Output (the lines that matter)
Route (app)
┌ ○ /
├ ○ /_not-found
├ ◐ /account
├ ƒ /api/auth/[...all]
├ ○ /sign-in
└ ○ /sign-up
○ (Static) prerendered as static content
◐ (Partial Prerender) prerendered as static HTML with dynamic server-streamed content
It finishes, and /account is now a partial prerender: its heading is built ahead of time and the account details stream in for each visitor.
Another Fix: Let the Page Block
If a page should simply wait for the server, the dev overlay’s third suggestion, Block, also clears the error. Add this line to the page, below its imports:
app/account/page.tsx (excerpt)
export const instant = false;
I tried it on the original page, and the build finished too, with /account rendered on demand:
Output (the lines that matter)
Route (app)
┌ ○ /
├ ○ /_not-found
├ ƒ /account
├ ƒ /api/auth/[...all]
├ ○ /sign-in
└ ○ /sign-up
○ (Static) prerendered as static content
ƒ (Dynamic) server-rendered on demand
Next.js’s Instant navigation guide says instant = false opts the page out of these checks, and that navigation to it then waits on the server. The <Suspense> fix keeps the page fast, so the Bookshop uses that one.
After the Fix
The account page works as before, and the Next.js indicator shows no issue:
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
| Reading the session in a layout’s top level | Every page under the layout waits for the request | Read it in a component inside <Suspense>, as the Next.js guide shows |
Adding "use cache" to the component that calls headers() | Next.js refuses: a plain "use cache" function can’t read headers() or cookies() | Use <Suspense>, or "use cache: private" as the guide describes |
Troubleshooting
| Problem | Cause | Fix |
|---|---|---|
The error names another route, such as / | A component on that page, or its layout, reads headers(), cookies() or the session | Find it with the code frame in the dev overlay, then wrap it in <Suspense> |
The build output only says at body (<anonymous>) | The production build doesn’t show where the read happened | Open the page with npm run dev, or run next build --debug-prerender, as the error suggests |
FAQ
Is this a Better Auth bug?
No. Any code that reads the request at a page’s top level does the same under Cache Components, whatever library it calls. Better Auth’s getSession reads the headers, so it’s the line Next.js points at.
Why didn’t I see it until I opened the page?
The dev server checks a page when you open it, so the issue appears in the overlay then. npm run build checks every page, so it always finds it.
Does the fix make the page slower?
No. The build output marks /account as a partial prerender: the heading is built ahead of time, and only the account details wait for the request.
Conclusion
Next.js encountered uncached or runtime data during prerendering means a page reads request data, such as the Better Auth session, outside <Suspense>. Move that read into its own component inside <Suspense>, and the build passes with a fast, partly prerendered page.
Additional Resources
- Authentication with Cache Components in the Next.js documentation
- Caching: runtime APIs and streaming with
<Suspense> - Blocking prerender, the page the error links to
- Instant navigation:
export const instant = false - cacheComponents in the Next.js configuration reference
- Next.js integration in the Better Auth documentation
Related
Better Auth, Next.js and other names mentioned are trademarks of their respective owners. JD Bots isn’t affiliated with or endorsed by Better Auth, Inc. or Vercel.
