Skip to content

Commit e1b16f3

Browse files
authored
[cdn] Adding examples for custom error pages (#1377)
### Description These examples demonstrate how customers can add error routes that will be picked up by vercel for platform errors. ### Type of Change - [x] New Example - [ ] Example updates (Bug fixes, new features, etc.) - [ ] Other (changes to the codebase, but not to examples) ### New Example Checklist - [ ] 🛫 `npm run new-example` was used to create the example - [x] 📚 The template wasn't used but I carefuly read the [Adding a new example](https://github.com/vercel/examples#adding-a-new-example) steps and implemented them in the example - [ ] 📱 Is it responsive? Are mobile and tablets considered?
1 parent 48c0d01 commit e1b16f3

38 files changed

+13888
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2+
3+
# Dependencies
4+
/node_modules
5+
/.pnp
6+
.pnp.js
7+
8+
# Testing
9+
/coverage
10+
11+
# Next.js
12+
/.next/
13+
/out/
14+
next-env.d.ts
15+
16+
# Production
17+
build
18+
dist
19+
20+
# Misc
21+
.DS_Store
22+
*.pem
23+
24+
# Debug
25+
npm-debug.log*
26+
yarn-debug.log*
27+
yarn-error.log*
28+
29+
# Local ENV files
30+
.env.local
31+
.env.development.local
32+
.env.test.local
33+
.env.production.local
34+
35+
# Vercel
36+
.vercel
37+
38+
# Turborepo
39+
.turbo
40+
41+
# typescript
42+
*.tsbuildinfo
43+
.env*.local
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
[![Deploy with Vercel](https://vercel.com/button)](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)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export default function ServerError() {
2+
return (
3+
<div className="flex min-h-screen flex-col items-center justify-center">
4+
<h1 className="text-4xl font-bold">500</h1>
5+
<p className="mt-4 text-lg text-gray-600">Internal Server Error</p>
6+
<p className="mt-2 text-sm text-gray-500">
7+
Something went wrong on our end. Please try again later.
8+
</p>
9+
<a href="/" className="mt-6 text-blue-600 hover:underline">
10+
Go back home
11+
</a>
12+
</div>
13+
);
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export default function GatewayTimeout() {
2+
return (
3+
<div className="flex min-h-screen flex-col items-center justify-center">
4+
<h1 className="text-4xl font-bold">504</h1>
5+
<p className="mt-4 text-lg text-gray-600">Gateway Timeout</p>
6+
<p className="mt-2 text-sm text-gray-500">
7+
The server took too long to respond. Please try again later.
8+
</p>
9+
<a href="/" className="mt-6 text-blue-600 hover:underline">
10+
Go back home
11+
</a>
12+
</div>
13+
);
14+
}
25.3 KB
Binary file not shown.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
@import "tailwindcss";
2+
3+
:root {
4+
--background: #ffffff;
5+
--foreground: #171717;
6+
}
7+
8+
@theme inline {
9+
--color-background: var(--background);
10+
--color-foreground: var(--foreground);
11+
--font-sans: var(--font-geist-sans);
12+
--font-mono: var(--font-geist-mono);
13+
}
14+
15+
@media (prefers-color-scheme: dark) {
16+
:root {
17+
--background: #0a0a0a;
18+
--foreground: #ededed;
19+
}
20+
}
21+
22+
body {
23+
background: var(--background);
24+
color: var(--foreground);
25+
font-family: Arial, Helvetica, sans-serif;
26+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import type { Metadata } from "next";
2+
import { Geist, Geist_Mono } from "next/font/google";
3+
import "./globals.css";
4+
5+
const geistSans = Geist({
6+
variable: "--font-geist-sans",
7+
subsets: ["latin"],
8+
});
9+
10+
const geistMono = Geist_Mono({
11+
variable: "--font-geist-mono",
12+
subsets: ["latin"],
13+
});
14+
15+
export const metadata: Metadata = {
16+
title: "Create Next App",
17+
description: "Generated by create next app",
18+
};
19+
20+
export default function RootLayout({
21+
children,
22+
}: Readonly<{
23+
children: React.ReactNode;
24+
}>) {
25+
return (
26+
<html lang="en">
27+
<body
28+
className={`${geistSans.variable} ${geistMono.variable} antialiased`}
29+
>
30+
{children}
31+
</body>
32+
</html>
33+
);
34+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import Image from "next/image";
2+
3+
export default function Home() {
4+
return (
5+
<div className="flex min-h-screen items-center justify-center bg-zinc-50 font-sans dark:bg-black">
6+
<main className="flex min-h-screen w-full max-w-3xl flex-col items-center justify-between py-32 px-16 bg-white dark:bg-black sm:items-start">
7+
<Image
8+
className="dark:invert"
9+
src="/next.svg"
10+
alt="Next.js logo"
11+
width={100}
12+
height={20}
13+
priority
14+
/>
15+
<div className="flex flex-col items-center gap-6 text-center sm:items-start sm:text-left">
16+
<h1 className="max-w-xs text-3xl font-semibold leading-10 tracking-tight text-black dark:text-zinc-50">
17+
To get started, edit the page.tsx file.
18+
</h1>
19+
<p className="max-w-md text-lg leading-8 text-zinc-600 dark:text-zinc-400">
20+
Looking for a starting point or more instructions? Head over to{" "}
21+
<a
22+
href="https://vercel.com/templates?framework=next.js&utm_source=create-next-app&utm_medium=appdir-template-tw&utm_campaign=create-next-app"
23+
className="font-medium text-zinc-950 dark:text-zinc-50"
24+
>
25+
Templates
26+
</a>{" "}
27+
or the{" "}
28+
<a
29+
href="https://nextjs.org/learn?utm_source=create-next-app&utm_medium=appdir-template-tw&utm_campaign=create-next-app"
30+
className="font-medium text-zinc-950 dark:text-zinc-50"
31+
>
32+
Learning
33+
</a>{" "}
34+
center.
35+
</p>
36+
</div>
37+
<div className="flex flex-col gap-4 text-base font-medium sm:flex-row">
38+
<a
39+
className="flex h-12 w-full items-center justify-center gap-2 rounded-full bg-foreground px-5 text-background transition-colors hover:bg-[#383838] dark:hover:bg-[#ccc] md:w-[158px]"
40+
href="https://vercel.com/new?utm_source=create-next-app&utm_medium=appdir-template-tw&utm_campaign=create-next-app"
41+
target="_blank"
42+
rel="noopener noreferrer"
43+
>
44+
<Image
45+
className="dark:invert"
46+
src="/vercel.svg"
47+
alt="Vercel logomark"
48+
width={16}
49+
height={16}
50+
/>
51+
Deploy Now
52+
</a>
53+
<a
54+
className="flex h-12 w-full items-center justify-center rounded-full border border-solid border-black/[.08] px-5 transition-colors hover:border-transparent hover:bg-black/[.04] dark:border-white/[.145] dark:hover:bg-[#1a1a1a] md:w-[158px]"
55+
href="https://nextjs.org/docs?utm_source=create-next-app&utm_medium=appdir-template-tw&utm_campaign=create-next-app"
56+
target="_blank"
57+
rel="noopener noreferrer"
58+
>
59+
Documentation
60+
</a>
61+
</div>
62+
</main>
63+
</div>
64+
);
65+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { defineConfig, globalIgnores } from "eslint/config";
2+
import nextVitals from "eslint-config-next/core-web-vitals";
3+
import nextTs from "eslint-config-next/typescript";
4+
5+
const eslintConfig = defineConfig([
6+
...nextVitals,
7+
...nextTs,
8+
// Override default ignores of eslint-config-next.
9+
globalIgnores([
10+
// Default ignores of eslint-config-next:
11+
".next/**",
12+
"out/**",
13+
"build/**",
14+
"next-env.d.ts",
15+
]),
16+
]);
17+
18+
export default eslintConfig;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import type { NextConfig } from "next";
2+
3+
const nextConfig: NextConfig = {
4+
/* config options here */
5+
};
6+
7+
export default nextConfig;

0 commit comments

Comments
 (0)