Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update middleware example to App Router #65618

Merged
merged 5 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/middleware/README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Middleware

This example shows how to use [Middleware in Next.js](https://nextjs.org/docs/advanced-features/middleware) to run code before a request is completed.
This example shows how to use [Middleware in Next.js](https://nextjs.org/docs/app/building-your-application/routing/middleware) to run code before a request is completed.

The index page ([`pages/index.tsx`](pages/index.tsx)) has a list of links to pages with `redirect`, `rewrite`, or normal behavior.
The index page ([`app/page.tsx`](app/page.tsx)) has a list of links to pages with `redirect`, `rewrite`, or normal behavior.

On the Middleware file ([`middleware.ts`](middleware.ts)) the routes are already being filtered by defining a `matcher` on the exported config. If you want the Middleware to run for every request, you can remove the `matcher`.

Expand Down
3 changes: 3 additions & 0 deletions examples/middleware/app/about/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function AboutPage() {
return <h1>About</h1>;
}
3 changes: 3 additions & 0 deletions examples/middleware/app/about2/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function About2Page() {
return <h1>About 2</h1>;
}
3 changes: 3 additions & 0 deletions examples/middleware/app/another/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function AnotherPage() {
return <h1>Another</h1>;
}
18 changes: 18 additions & 0 deletions examples/middleware/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import type { Metadata } from 'next';

export default function RootLayout({
children,
}: {
children: React.ReactNode
}) {
return (
<html lang="en">
<body>{children}</body>
</html>
)
}

export const metadata: Metadata = {
title: 'Next.js Middleware example',
description: 'Redirect and rewrite pages using Next.js Middleware.',
}
Original file line number Diff line number Diff line change
@@ -1,27 +1,24 @@
import type { NextPage } from "next";
import Link from "next/link";

const Home: NextPage = () => {
export default function Home() {
return (
<div>
<h1>Index</h1>
<p>
<Link href="/about" passHref>
<Link href="/about">
Go to about page (will redirect)
</Link>
</p>
<p>
<Link href="/another" passHref>
<Link href="/another">
Go to another page (will rewrite)
</Link>
</p>
<p>
<Link href="/about2" passHref>
<Link href="/about2">
Go to about 2 page (no redirect or rewrite)
</Link>
</p>
</div>
);
};

export default Home;
}
3 changes: 3 additions & 0 deletions examples/middleware/app/redirected/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function RedirectedPage() {
return <h1>Redirected from /about</h1>;
}
3 changes: 3 additions & 0 deletions examples/middleware/app/rewrite/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function RewritePage() {
return <h1>Rewrite</h1>;
}
6 changes: 0 additions & 6 deletions examples/middleware/next.config.js

This file was deleted.

7 changes: 0 additions & 7 deletions examples/middleware/pages/_app.tsx

This file was deleted.

7 changes: 0 additions & 7 deletions examples/middleware/pages/about.tsx

This file was deleted.

7 changes: 0 additions & 7 deletions examples/middleware/pages/about2.tsx

This file was deleted.

7 changes: 0 additions & 7 deletions examples/middleware/pages/another.tsx

This file was deleted.

7 changes: 0 additions & 7 deletions examples/middleware/pages/redirected.tsx

This file was deleted.

7 changes: 0 additions & 7 deletions examples/middleware/pages/rewrite.tsx

This file was deleted.

9 changes: 7 additions & 2 deletions examples/middleware/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,13 @@
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve",
"incremental": true
"incremental": true,
"plugins": [
{
"name": "next"
}
]
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
"exclude": ["node_modules"]
}