Next.js Discord

Discord Forum

Quick Sanity Check

Answered
Black carp posted this in #help-forum
Open in Discord
Black carpOP
NextJS 14.0.3.

app/middleware.ts
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";

// See "Matching Paths" below to learn more
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).*)",
    ],
};

function middleware(request: NextRequest) {
    console.log({ request });
    return NextResponse.next();
}

export { config, middleware };


I see nothing being logged to confirm the middleware is successfully being invoked. I am 99% sure this is going to be something on my device maybe being a pain in the butt, but I wanted to sanity check that maybe I'm doing something wrong making NextJS ignore it.

I am navigating to localhost:3000 (main page) expecting to see the log out into my terminal. But I do not see anything.

Cache has been cleared. Just feel like I'm losing my mind with doing something I've done countless times before.
Answered by Argentine hake
I believe the middleware.ts file should be in your project root directory. Not in the app directory.
View full answer

4 Replies

Argentine hake
I believe the middleware.ts file should be in your project root directory. Not in the app directory.
Answer
Black carpOP
:PES_Facepalm:
Yep, that's it. Thank you @Argentine hake
I knew I was missing something easy to close out my day. Oh my.