|
| 1 | +--- |
| 2 | +name: Custom Error Pages (App Router) |
| 3 | +slug: custom-error-pages-app-dir |
| 4 | +description: Create custom 5xx error pages using Next.js App Router |
| 5 | +framework: Next.js |
| 6 | +useCase: Edge Functions |
| 7 | +css: Tailwind |
| 8 | +deployUrl: https://vercel.com/new/clone?repository-url=https://github.com/vercel/examples/tree/main/cdn/custom-error-pages-app-dir&project-name=custom-error-pages-app-dir&repository-name=custom-error-pages-app-dir |
| 9 | +demoUrl: https://custom-error-pages-app-dir.vercel.app |
| 10 | +--- |
| 11 | + |
| 12 | +# Custom Error Pages (App Router) |
| 13 | + |
| 14 | +This example demonstrates how to create custom error pages for 5xx server errors using Next.js App Router. This feature is available for Enterprise customers. |
| 15 | + |
| 16 | +## Demo |
| 17 | + |
| 18 | +https://custom-error-pages-app-dir.vercel.app |
| 19 | + |
| 20 | +## How to Use |
| 21 | + |
| 22 | +You can choose from one of the following two methods to use this repository: |
| 23 | + |
| 24 | +### One-Click Deploy |
| 25 | + |
| 26 | +Deploy the example using [Vercel](https://vercel.com?utm_source=github&utm_medium=readme&utm_campaign=vercel-examples): |
| 27 | + |
| 28 | +[](https://vercel.com/new/clone?repository-url=https://github.com/vercel/examples/tree/main/cdn/custom-error-pages-app-dir&project-name=custom-error-pages-app-dir&repository-name=custom-error-pages-app-dir) |
| 29 | + |
| 30 | +### Clone and Deploy |
| 31 | + |
| 32 | +Execute [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app) with [pnpm](https://pnpm.io/installation) to bootstrap the example: |
| 33 | + |
| 34 | +```bash |
| 35 | +pnpm create next-app --example https://github.com/vercel/examples/tree/main/cdn/custom-error-pages-app-dir |
| 36 | +``` |
| 37 | + |
| 38 | +Next, run Next.js in development mode: |
| 39 | + |
| 40 | +```bash |
| 41 | +pnpm dev |
| 42 | +``` |
| 43 | + |
| 44 | +Deploy it to the cloud with [Vercel](https://vercel.com/new?utm_source=github&utm_medium=readme&utm_campaign=vercel-examples) ([Documentation](https://nextjs.org/docs/deployment)). |
| 45 | + |
| 46 | +## Structure |
| 47 | + |
| 48 | +``` |
| 49 | +app/ |
| 50 | +├── 500/ |
| 51 | +│ └── page.tsx # Generic server error (fallback for all 5xx) |
| 52 | +└── 504/ |
| 53 | + └── page.tsx # Specific gateway timeout page |
| 54 | +``` |
| 55 | + |
| 56 | +## How it works |
| 57 | + |
| 58 | +When deployed to Vercel, error pages are automatically detected and routes are generated: |
| 59 | + |
| 60 | +| Error | Destination | |
| 61 | +|-------|-------------| |
| 62 | +| 500 | `/500` (from `app/500/page.tsx`) | |
| 63 | +| 502 | `/500` (fallback) | |
| 64 | +| 503 | `/500` (fallback) | |
| 65 | +| 504 | `/504` (from `app/504/page.tsx`) | |
| 66 | +| 508 | `/500` (fallback) | |
| 67 | + |
| 68 | +## Learn more |
| 69 | + |
| 70 | +- [Custom Error Pages Documentation](https://vercel.com/docs/cdn/custom-error-pages) |
0 commit comments