Some checks failed
Test examples / Test Examples (20) (push) Has been cancelled
Test examples / Test Examples (22) (push) Has been cancelled
Lock Threads / action (push) Has been cancelled
Trigger Release / start (push) Has been cancelled
Stale issue handler / stale (push) Has been cancelled
Update Font Data / create-pull-request (push) Has been cancelled
build-and-deploy / deploy-target (push) Has been cancelled
build-and-deploy / build (push) Has been cancelled
build-and-deploy / stable - aarch64-unknown-linux-musl - node@16 (push) Has been cancelled
build-and-deploy / stable - x86_64-unknown-linux-musl - node@16 (push) Has been cancelled
build-and-deploy / stable - aarch64-unknown-linux-gnu - node@16 (push) Has been cancelled
build-and-deploy / stable - x86_64-unknown-linux-gnu - node@16 (push) Has been cancelled
build-and-deploy / stable - aarch64-pc-windows-msvc - node@16 (push) Has been cancelled
build-and-deploy / stable - x86_64-pc-windows-msvc - node@16 (push) Has been cancelled
build-and-deploy / stable - aarch64-apple-darwin - node@16 (push) Has been cancelled
build-and-deploy / stable - x86_64-apple-darwin - node@16 (push) Has been cancelled
build-and-deploy / build-wasm (nodejs) (push) Has been cancelled
build-and-deploy / build-wasm (web) (push) Has been cancelled
build-and-deploy / Deploy preview tarball (push) Has been cancelled
build-and-deploy / Potentially publish release (push) Has been cancelled
build-and-deploy / publish-turbopack-npm-packages (push) Has been cancelled
build-and-deploy / Deploy examples (push) Has been cancelled
build-and-deploy / thank you, build (push) Has been cancelled
build-and-deploy / Upload Turbopack Bytesize metrics to Datadog (push) Has been cancelled
Rspack Next.js development integration tests / Rspack integration tests (push) Has been cancelled
Rspack Next.js production integration tests / Rspack integration tests (push) Has been cancelled
Turbopack Next.js development integration tests / Next.js integration tests (push) Has been cancelled
Turbopack Next.js production integration tests / Next.js integration tests (push) Has been cancelled
Update Rspack test manifest / Update and upload Rspack development test manifest (push) Has been cancelled
Update Rspack test manifest / Update and upload Rspack production test manifest (push) Has been cancelled
Upload bundler test manifests to areweturboyet.com / Upload test results (push) Has been cancelled
Update React / create-pull-request (push) Has been cancelled
test-e2e-project-reset-cron / reset-test-project (push) Has been cancelled
Notify about the top 15 issues/PRs/feature requests (most reacted) in the last 90 days / run (push) Has been cancelled
151 lines
3.8 KiB
Plaintext
151 lines
3.8 KiB
Plaintext
---
|
|
title: Prerender Error with Next.js
|
|
---
|
|
|
|
## Why This Error Occurred
|
|
|
|
While prerendering a page during `next build`, an error occurred. This can happen for various reasons, including:
|
|
|
|
1. Incorrect file structure or non-page files in the `pages/` directory
|
|
2. Expecting props to be populated which are not available during prerendering
|
|
3. Using browser-only APIs in components without proper checks
|
|
4. Incorrect configuration in `getStaticProps` or `getStaticPaths`
|
|
|
|
## Possible Ways to Fix It
|
|
|
|
### 1. Ensure correct file structure and use App Router for colocation
|
|
|
|
#### Pages Router
|
|
|
|
In the Pages Router, only special files are allowed to generate pages. You cannot colocate other files (e.g., components, styles) within the `pages` directory.
|
|
|
|
Correct structure:
|
|
|
|
```txt
|
|
.
|
|
├── components/
|
|
│ └── Header.js
|
|
├── pages/
|
|
│ ├── about.js
|
|
│ └── index.js
|
|
└── styles/
|
|
└── globals.css
|
|
```
|
|
|
|
#### App Router (Next.js 13+)
|
|
|
|
The App Router allows [colocation](/docs/app/getting-started/project-structure#colocation) of pages and other files in the same folder. This provides a more intuitive project structure.
|
|
|
|
Example folder structure:
|
|
|
|
```txt
|
|
.
|
|
└── app/
|
|
├── about/
|
|
│ └── page.tsx
|
|
├── blog/
|
|
│ ├── page.tsx
|
|
│ └── PostCard.tsx
|
|
├── layout.tsx
|
|
└── page.tsx
|
|
```
|
|
|
|
### 2. Handle undefined props and missing data
|
|
|
|
#### Pages Router
|
|
|
|
For the Pages Router, use conditional checks and return appropriate props or a 404 page:
|
|
|
|
```jsx
|
|
export async function getStaticProps(context) {
|
|
const data = await fetchData(context.params.id)
|
|
if (!data) {
|
|
return {
|
|
notFound: true,
|
|
}
|
|
}
|
|
return {
|
|
props: { data },
|
|
}
|
|
}
|
|
```
|
|
|
|
### 3. Handle fallback in dynamic routes
|
|
|
|
If you're using `fallback: true` or `fallback: 'blocking'` in `getStaticPaths`, ensure your page component can handle the loading state:
|
|
|
|
```jsx
|
|
import { useRouter } from 'next/router'
|
|
|
|
function Post({ post }) {
|
|
const router = useRouter()
|
|
|
|
if (router.isFallback) {
|
|
return <div>Loading...</div>
|
|
}
|
|
|
|
return (
|
|
<article>
|
|
<h1>{post.title}</h1>
|
|
<p>{post.content}</p>
|
|
</article>
|
|
)
|
|
}
|
|
```
|
|
|
|
### 4. Avoid exporting pages with server-side rendering
|
|
|
|
If you're using `next export` or `output: 'export'` in your `next.config.js`, ensure that none of your pages use `getServerSideProps`. Instead, use `getStaticProps` for data fetching:
|
|
|
|
```jsx
|
|
export async function getStaticProps() {
|
|
const res = await fetch('https://api.vercel.app/blog')
|
|
const data = await res.json()
|
|
|
|
return {
|
|
props: { data },
|
|
revalidate: 60,
|
|
}
|
|
}
|
|
```
|
|
|
|
### 5. Disable server-side rendering for components using browser APIs
|
|
|
|
If a component relies on browser-only APIs like `window`, you can disable server-side rendering for that component:
|
|
|
|
```jsx
|
|
import dynamic from 'next/dynamic'
|
|
|
|
const DynamicComponentWithNoSSR = dynamic(
|
|
() => import('../components/BrowserOnlyComponent'),
|
|
{ ssr: false }
|
|
)
|
|
|
|
export default function Page() {
|
|
return (
|
|
<div>
|
|
<h1>My page</h1>
|
|
<DynamicComponentWithNoSSR />
|
|
</div>
|
|
)
|
|
}
|
|
```
|
|
|
|
## Debugging
|
|
|
|
For additional debugging information when troubleshooting prerender errors, you can run:
|
|
|
|
```bash
|
|
next build --debug-prerender
|
|
```
|
|
|
|
This provides unminified stack traces with source maps, making it easier to pinpoint the exact component and route causing the issue.
|
|
|
|
## Additional Resources
|
|
|
|
- [Handling Errors in Next.js](/docs/app/getting-started/error-handling)
|
|
- [Data Fetching in Next.js](/docs/app/getting-started/fetching-data)
|
|
- [Debugging prerender errors](/docs/app/api-reference/cli/next#debugging-prerender-errors)
|
|
|
|
If you continue to experience issues after trying these solutions, consider checking your server logs for more detailed error messages or reaching out to the Next.js community for support.
|