Next.js Discord

Discord Forum

help me understand Middleware in Next.js

Answered
Cape lion posted this in #help-forum
Open in Discord
Cape lionOP
import { NextRequest, NextResponse } from "next/server";

export function middleware(request: NextRequest) {
  // return NextResponse.redirect("/");
  return NextResponse.redirect(new URL("/", request.url))
}

export const config = {
  matcher: "/profile",
};

why is this not working? i tried restarting the dev server but it was to no avail.
also why cant you simply write NextResponse.redirect("/") when it says it accepts a string as well? and why is the second argument needed in new URL? is it to specify the base URL?
Answered by Cape lion
the latest one. nevermind, i had put the middleware fil inside the app directory. i put it ouside, in the root directory and it worked
View full answer

3 Replies