Skip to content

Commit 6290689

Browse files
authored
Convert middleware-matcher example to TypeScript (vercel#42520)
Converted example to TypeScript to match Contribution docs. - Set cookies using `NextResponse` instead of the `Set-Cookie` header ## Documentation / Examples - [X] Make sure the linting passes by running `pnpm build && pnpm lint` - [X] The "examples guidelines" are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
1 parent 2578355 commit 6290689

File tree

7 files changed

+60
-32
lines changed

7 files changed

+60
-32
lines changed

examples/middleware-matcher/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
This example shows how to configure your [Next.js Middleware](https://nextjs.org/docs/advanced-features/middleware) to only match specific pages.
44

5-
The index page ([`pages/index.js`](pages/index.js)) has a list of links to dynamic pages, which will tell whether they were matched or not.
5+
The index page ([`pages/index.ts`](pages/index.ts)) has a list of links to dynamic pages, which will tell whether they were matched or not.
66

7-
The Middleware file ([`middleware.js`](middleware.js)) has a special `matcher` configuration key, allowing you to fine-grained control [matched pages](https://nextjs.org/docs/advanced-features/middleware#matcher).
7+
The Middleware file ([`middleware.ts`](middleware.ts)) has a special `matcher` configuration key, allowing you to fine-grained control [matched pages](https://nextjs.org/docs/advanced-features/middleware#matcher).
88

99
Please keep in mind that:
1010

examples/middleware-matcher/middleware.js

Lines changed: 0 additions & 18 deletions
This file was deleted.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { NextResponse } from 'next/server'
2+
import type { NextRequest } from 'next/server'
3+
4+
export default function middleware(req: NextRequest) {
5+
const path = req.nextUrl.pathname
6+
const slug = path.slice(1)
7+
8+
// Set a cookie on the response using the `ResponseCookies` API
9+
const response = NextResponse.next()
10+
response.cookies.set({
11+
name: 'middleware-slug',
12+
value: slug,
13+
path,
14+
})
15+
16+
return response
17+
}
18+
19+
export const config = {
20+
matcher: [
21+
'/disclaimer', // match a single, specific page
22+
'/((?!public|static).*)', // match all paths not starting with 'public' or 'static'
23+
],
24+
}

examples/middleware-matcher/package.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,13 @@
77
},
88
"dependencies": {
99
"next": "latest",
10-
"react": "latest",
11-
"react-dom": "latest"
10+
"react": "^18.2.0",
11+
"react-dom": "^18.2.0"
12+
},
13+
"devDependencies": {
14+
"@types/node": "^18.11.9",
15+
"@types/react": "^18.0.25",
16+
"@types/react-dom": "^18.0.8",
17+
"typescript": "^4.8.4"
1218
}
1319
}
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { useRouter } from 'next/router'
22

3-
function hasMiddlewareMatched(slug) {
3+
function hasMiddlewareMatched(slug?: string[]) {
44
const values =
55
(typeof document !== 'undefined' ? document.cookie : '')
66
.split(';')
@@ -10,10 +10,10 @@ function hasMiddlewareMatched(slug) {
1010
return values.some((value) => value === slug?.join('/'))
1111
}
1212

13-
export const ContentPage = (props) => {
14-
const {
15-
query: { slug }, // slug is an array of path segments
16-
} = useRouter()
13+
export default function ContentPage() {
14+
const router = useRouter()
15+
const slug = router.query.slug as string[] // slug is an array of path segments
16+
1717
return (
1818
<>
1919
<h1>
@@ -25,5 +25,3 @@ export const ContentPage = (props) => {
2525
</>
2626
)
2727
}
28-
29-
export default ContentPage

examples/middleware-matcher/pages/index.jsx renamed to examples/middleware-matcher/pages/index.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const Home = () => {
1+
export default function Home() {
22
const matching = ['/about', '/about/topic/cats', '/public/disclaimer']
33
const notMatching = ['/public', '/public/disclaimer/nested', '/static']
44
return (
@@ -26,5 +26,3 @@ const Home = () => {
2626
</div>
2727
)
2828
}
29-
30-
export default Home
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es5",
4+
"lib": ["dom", "dom.iterable", "esnext"],
5+
"allowJs": true,
6+
"skipLibCheck": true,
7+
"strict": true,
8+
"forceConsistentCasingInFileNames": true,
9+
"noEmit": true,
10+
"esModuleInterop": true,
11+
"module": "esnext",
12+
"moduleResolution": "node",
13+
"resolveJsonModule": true,
14+
"isolatedModules": true,
15+
"jsx": "preserve",
16+
"incremental": true
17+
},
18+
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
19+
"exclude": ["node_modules"]
20+
}

0 commit comments

Comments
 (0)