diff --git a/examples/middleware/.eslintrc.json b/examples/middleware/.eslintrc.json deleted file mode 100755 index bffb357a71225..0000000000000 --- a/examples/middleware/.eslintrc.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "extends": "next/core-web-vitals" -} diff --git a/examples/middleware/README.md b/examples/middleware/README.md index daf2757ab9b4d..a8eeb816f9d3d 100755 --- a/examples/middleware/README.md +++ b/examples/middleware/README.md @@ -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`. diff --git a/examples/middleware/app/about/page.tsx b/examples/middleware/app/about/page.tsx new file mode 100644 index 0000000000000..9c8145d3bbd01 --- /dev/null +++ b/examples/middleware/app/about/page.tsx @@ -0,0 +1,3 @@ +export default function AboutPage() { + return

About

; +} diff --git a/examples/middleware/app/about2/page.tsx b/examples/middleware/app/about2/page.tsx new file mode 100644 index 0000000000000..76a7a4e29651a --- /dev/null +++ b/examples/middleware/app/about2/page.tsx @@ -0,0 +1,3 @@ +export default function About2Page() { + return

About 2

; +} diff --git a/examples/middleware/app/another/page.tsx b/examples/middleware/app/another/page.tsx new file mode 100644 index 0000000000000..b80bfc7640195 --- /dev/null +++ b/examples/middleware/app/another/page.tsx @@ -0,0 +1,3 @@ +export default function AnotherPage() { + return

Another

; +} diff --git a/examples/middleware/app/layout.tsx b/examples/middleware/app/layout.tsx new file mode 100644 index 0000000000000..81e02eca6fd10 --- /dev/null +++ b/examples/middleware/app/layout.tsx @@ -0,0 +1,18 @@ +import type { Metadata } from "next"; + +export default function RootLayout({ + children, +}: { + children: React.ReactNode; +}) { + return ( + + {children} + + ); +} + +export const metadata: Metadata = { + title: "Next.js Middleware example", + description: "Redirect and rewrite pages using Next.js Middleware.", +}; diff --git a/examples/middleware/app/page.tsx b/examples/middleware/app/page.tsx new file mode 100755 index 0000000000000..b294d00c403f2 --- /dev/null +++ b/examples/middleware/app/page.tsx @@ -0,0 +1,18 @@ +import Link from "next/link"; + +export default function Home() { + return ( +
+

Index

+

+ Go to about page (will redirect) +

+

+ Go to another page (will rewrite) +

+

+ Go to about 2 page (no redirect or rewrite) +

+
+ ); +} diff --git a/examples/middleware/app/redirected/page.tsx b/examples/middleware/app/redirected/page.tsx new file mode 100644 index 0000000000000..fa383f3ffa5c2 --- /dev/null +++ b/examples/middleware/app/redirected/page.tsx @@ -0,0 +1,3 @@ +export default function RedirectedPage() { + return

Redirected from /about

; +} diff --git a/examples/middleware/app/rewrite/page.tsx b/examples/middleware/app/rewrite/page.tsx new file mode 100644 index 0000000000000..9f1c1e57ff081 --- /dev/null +++ b/examples/middleware/app/rewrite/page.tsx @@ -0,0 +1,3 @@ +export default function RewritePage() { + return

Rewrite

; +} diff --git a/examples/middleware/next.config.js b/examples/middleware/next.config.js deleted file mode 100755 index 91ef62f0db592..0000000000000 --- a/examples/middleware/next.config.js +++ /dev/null @@ -1,6 +0,0 @@ -/** @type {import('next').NextConfig} */ -const nextConfig = { - reactStrictMode: true, -}; - -module.exports = nextConfig; diff --git a/examples/middleware/pages/_app.tsx b/examples/middleware/pages/_app.tsx deleted file mode 100755 index ef51d6851d836..0000000000000 --- a/examples/middleware/pages/_app.tsx +++ /dev/null @@ -1,7 +0,0 @@ -import type { AppProps } from "next/app"; - -function MyApp({ Component, pageProps }: AppProps) { - return ; -} - -export default MyApp; diff --git a/examples/middleware/pages/about.tsx b/examples/middleware/pages/about.tsx deleted file mode 100644 index a8acd7ace99b6..0000000000000 --- a/examples/middleware/pages/about.tsx +++ /dev/null @@ -1,7 +0,0 @@ -import { NextPage } from "next"; - -export const AboutPage: NextPage = () => { - return

About

; -}; - -export default AboutPage; diff --git a/examples/middleware/pages/about2.tsx b/examples/middleware/pages/about2.tsx deleted file mode 100644 index 8f0779125b430..0000000000000 --- a/examples/middleware/pages/about2.tsx +++ /dev/null @@ -1,7 +0,0 @@ -import { NextPage } from "next"; - -export const About2Page: NextPage = () => { - return

About 2

; -}; - -export default About2Page; diff --git a/examples/middleware/pages/another.tsx b/examples/middleware/pages/another.tsx deleted file mode 100644 index 9f7a7ca6e0cea..0000000000000 --- a/examples/middleware/pages/another.tsx +++ /dev/null @@ -1,7 +0,0 @@ -import { NextPage } from "next"; - -export const AnotherPage: NextPage = () => { - return

Another

; -}; - -export default AnotherPage; diff --git a/examples/middleware/pages/index.tsx b/examples/middleware/pages/index.tsx deleted file mode 100755 index 74af336304f36..0000000000000 --- a/examples/middleware/pages/index.tsx +++ /dev/null @@ -1,27 +0,0 @@ -import type { NextPage } from "next"; -import Link from "next/link"; - -const Home: NextPage = () => { - return ( -
-

Index

-

- - Go to about page (will redirect) - -

-

- - Go to another page (will rewrite) - -

-

- - Go to about 2 page (no redirect or rewrite) - -

-
- ); -}; - -export default Home; diff --git a/examples/middleware/pages/redirected.tsx b/examples/middleware/pages/redirected.tsx deleted file mode 100644 index 369ade6d857f0..0000000000000 --- a/examples/middleware/pages/redirected.tsx +++ /dev/null @@ -1,7 +0,0 @@ -import { NextPage } from "next"; - -export const RedirectedPage: NextPage = () => { - return

Redirected from /about

; -}; - -export default RedirectedPage; diff --git a/examples/middleware/pages/rewrite.tsx b/examples/middleware/pages/rewrite.tsx deleted file mode 100644 index c914b6b76ebdf..0000000000000 --- a/examples/middleware/pages/rewrite.tsx +++ /dev/null @@ -1,7 +0,0 @@ -import { NextPage } from "next"; - -export const RewritePage: NextPage = () => { - return

Rewrite

; -}; - -export default RewritePage; diff --git a/examples/middleware/tsconfig.json b/examples/middleware/tsconfig.json index 99710e857874f..6ef5cd577f6ed 100755 --- a/examples/middleware/tsconfig.json +++ b/examples/middleware/tsconfig.json @@ -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"] }