我使用 npx create-next-app@latest --typescript 创建了一个新的 Next.js。安装后(版本为13.3.4),在不更改任何文件的情况下,我在src文件夹内添加了一个新的middleware.ts文件,并且我只放置了这段代码:
import type { NextRequest } from "next/server";
import { NextResponse } from "next/server";
export function middleware(request: NextRequest) {
console.log("request", request.nextUrl.pathname);
return NextResponse.next();
}
// EDIT: By putting this block will get expected result.
export const config = {
matcher: [
/*
* Match all request paths except for the ones starting with:
* - api (API routes)
* - _next/static (static files)
* - _next/image (image optimization files)
* - favicon.ico (favicon file)
*/
'/((?!api|_next/static|_next/image|favicon.ico).*)',
],
};
控制台日志被多次点击。我想应该是一次吧?对于这个新的 Next.js 安装,我需要做任何配置吗?
注意:我将在中间件中执行一些 cookie 逻辑以进行身份验证。截图:
Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号