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

Convert middleware-matcher example to TypeScript #42520

Merged
Merged
Show file tree
Hide file tree
Changes from 3 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-matcher/README.md
Expand Up @@ -2,9 +2,9 @@

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

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.
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.

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).
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).

Please keep in mind that:

Expand Down
18 changes: 0 additions & 18 deletions examples/middleware-matcher/middleware.js

This file was deleted.

24 changes: 24 additions & 0 deletions examples/middleware-matcher/middleware.ts
@@ -0,0 +1,24 @@
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'

export default function middleware(req: NextRequest) {
const path = req.nextUrl.pathname
const slug = path.slice(1)

// Set a cookie on the response using the `ResponseCookies` API
const response = NextResponse.next()
response.cookies.set({
name: 'middleware-slug',
value: slug,
path,
})

return response
}

export const config = {
matcher: [
'/disclaimer', // match a single, specific page
'/((?!public|static).*)', // match all paths not starting with 'public' or 'static'
],
}
10 changes: 8 additions & 2 deletions examples/middleware-matcher/package.json
Expand Up @@ -7,7 +7,13 @@
},
"dependencies": {
"next": "latest",
"react": "latest",
"react-dom": "latest"
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
"devDependencies": {
"@types/node": "^18.11.9",
"@types/react": "^18.0.25",
"@types/react-dom": "^18.0.8",
"typescript": "^4.8.4"
}
}
@@ -1,6 +1,6 @@
import { useRouter } from 'next/router'

function hasMiddlewareMatched(slug) {
function hasMiddlewareMatched(slug?: string[]) {
const values =
(typeof document !== 'undefined' ? document.cookie : '')
.split(';')
Expand All @@ -10,10 +10,10 @@ function hasMiddlewareMatched(slug) {
return values.some((value) => value === slug?.join('/'))
}

export const ContentPage = (props) => {
const {
query: { slug }, // slug is an array of path segments
} = useRouter()
export default function ContentPage() {
const router = useRouter()
const slug = router.query.slug as string[] // slug is an array of path segments

return (
<>
<h1>
Expand All @@ -25,5 +25,3 @@ export const ContentPage = (props) => {
</>
)
}

export default ContentPage
@@ -1,4 +1,4 @@
const Home = () => {
export default function Home() {
const matching = ['/about', '/about/topic/cats', '/public/disclaimer']
const notMatching = ['/public', '/public/disclaimer/nested', '/static']
return (
Expand Down Expand Up @@ -26,5 +26,3 @@ const Home = () => {
</div>
)
}

export default Home
20 changes: 20 additions & 0 deletions examples/middleware-matcher/tsconfig.json
@@ -0,0 +1,20 @@
{
"compilerOptions": {
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve",
"incremental": true
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
"exclude": ["node_modules"]
}