-
Notifications
You must be signed in to change notification settings - Fork 113
/
Copy pathmiddleware.ts
executable file
·42 lines (38 loc) · 1.34 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
37
38
39
40
41
42
import { NextRequest, NextResponse } from 'next/server'
import { DEFAULT_LOCALE, locales } from '@/framework/locale/locale'
import authConfig from '@/config/auth-config'
import NextAuth from 'next-auth'
const ADMIN_INCLUDE_PATHS = ['/admin', '/plan-admin']
function isIncludes(originUrl: string) {
return ADMIN_INCLUDE_PATHS.some((it) => originUrl.includes(it))
}
const { auth } = NextAuth(authConfig)
export default auth(async function middleware(request: NextRequest) {
// Your custom middleware logic goes here
console.info('Middleware called', { url: request.url, method: request.method })
// 自动跳转到对应的语言页面
const nextUrl = (request as unknown as NextRequest).nextUrl
const pathname = nextUrl.pathname
const params = nextUrl.searchParams
const pathnameIsMissingLocale = locales.every(
(locale) =>
!pathname.startsWith(`/${locale}/`) && pathname !== `/${locale}`
)
// Redirect if there is no locale
if (pathnameIsMissingLocale) {
// e.g. incoming request is /products
// The new URL is now /en-US/products
return NextResponse.redirect(
new URL(
`/${DEFAULT_LOCALE}/${pathname}?${params.toString()}`,
request.url
)
)
}
return NextResponse.next()
})
export const config = {
matcher: [
'/((?!api|_next/static|_next/image|favicon.ico|.*\\..*|_next).*)',
]
}