vercel / vercel/next.js

Custom error.tsx page is not rendered for dynamic routes using generateStaticParams / ISR in app router. Pages router 500.tsx is rendered instead

Open
#62,046 17 comments 24 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
JavaScript
Stars
142k
Forks
32.5k
Avg merge
2d 14h
Merged PRs (30d)
351

Description

Link to the code that reproduces this issue

https://github.com/bkrajewski94/next-isr-error-page-bug

To Reproduce
  1. Install dependencies with npm install
  2. Build the application with npm run build
  3. Start the application npm start
  4. Navigate to localhost:3000/test
Current vs. Expected behavior

If you use generateStaticParams inside a dynamic route like [slug]/page.tsx, none of the custom error.tsx/global-error.tsx pages will be displayed in case of an error. What you'll see instead is the NextJS inbuilt error page
image

In the below example I used generateStaticParams to implement ISR (on request static generation):

// src/app/[slug]/page.tsx

async function getData() {
  const res = await fetch("https://api.example.com/...");

  if (!res.ok) {
    // This will activate the closest `error.js` Error Boundary
    throw new Error("Failed to fetch data");
  }

  return res.json();
}

export default async function Page({ slug }: any) {
  const data = await getData();

  return (
    <main>
      <div>{slug}</div>
      <div>{JSON.stringify(data)}</div>
    </main>
  );
}

// error.tsx is not rendered when:
export function generateStaticParams() {
  return [];
}

// error.tsx is rendered correctly when instead of generateStaticParams I use:
// export const dynamic = "force-dynamic";

It only breaks if you build your application (works fine on devserver).
As you can imagine it can be a big problem when you'd like to e.g. render a custom error page, or log error to an external system inside the error boundary.

Provide environment information
Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 23.0.0: Fri Sep 15 14:41:43 PDT 2023; root:xnu-10002.1.13~1/RELEASE_ARM64_T6000
Binaries:
  Node: 20.10.0
  npm: 10.2.3
  Yarn: 1.22.21
  pnpm: 7.1.0
Relevant Packages:
  next: 14.1.1-canary.52 // Latest available version is detected (14.1.1-canary.52).
  eslint-config-next: 14.1.0
  react: 18.2.0
  react-dom: 18.2.0
  typescript: 5.3.3
Next.js Config:
  output: N/A
Which area(s) are affected? (Select all that apply)

App Router

Which stage(s) are affected? (Select all that apply)

next build (local), next start (local), Other (Deployed)

Additional context

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Clone the reproduction at https://github.com/bkrajewski94/next-isr-error-page-bug, install dependencies, then run npm run build and npm start before visiting localhost:3000/test. Compare the dynamic src/app/[slug]/page.tsx behavior with its error.tsx/global-error.tsx boundaries and the force-dynamic variant. Done means the custom error boundary is rendered after a production build when generateStaticParams is used.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, next.js, react
Domain
build-system, frontend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.