我用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).*)',
],
};
控制台日志(log)被多次命中.我觉得应该只有一次,对吧?对于这个全新的Next.js安装,我需要做什么配置吗?
注意:我将在middleware中执行一些用于身份验证的Cookie逻辑.屏幕截图: