-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathmiddleware.ts
36 lines (31 loc) · 1.27 KB
/
middleware.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
export async function middleware(request: NextRequest) {
const url = request.nextUrl
// if path ends with /json we create response in middleware, otherwise we pass it through
// to next server to get page or api response from it
const response = url.pathname.includes('/json')
? NextResponse.json({
requestUrlPathname: new URL(request.url).pathname,
nextUrlPathname: request.nextUrl.pathname,
nextUrlLocale: request.nextUrl.locale,
})
: NextResponse.next()
response.headers.set('x-test-used-middleware', 'true')
return response
}
// matcher copied from example in https://nextjs.org/docs/pages/building-your-application/routing/middleware#matcher
// with `excluded` segment added to exclusion
export const config = {
matcher: [
/*
* Match all request paths except for the ones starting with:
* - api (API routes)
* - excluded (for testing localized routes and not just API routes)
* - _next/static (static files)
* - _next/image (image optimization files)
* - favicon.ico, sitemap.xml, robots.txt (metadata files)
*/
'/((?!api|excluded|_next/static|_next/image|favicon.ico|sitemap.xml|robots.txt).*)',
],
}