Does Next.js Recreate Top-Level Constants in Middleware?
Unanswered
Blue swimming crab posted this in #help-forum
Blue swimming crabOP
For this code:
Are the elements defined outside the middleware function (like PATHS) recreated on every request, or are they initialized once and reused across requests?
// middleware.ts
const PATHS = new Set([
"some",
"path",
"weneed",
"to",
"match",
]);
export function middleware(request: NextRequest) {
if(PATHS.has(request.nextUrl.pathname)) {
return NextResponse.redirect("/somewhere");
}
return NextResponse.next();
}
Are the elements defined outside the middleware function (like PATHS) recreated on every request, or are they initialized once and reused across requests?
1 Reply
Chum salmon
The top-level constants run once per instance.