Next.js Discord

Discord Forum

middleware

Answered
oguisantosilva posted this in #help-forum
Open in Discord
my middleware isn't redirection when the .env var is closed
Answered by B33fb0n3
your middleware must be outside of your app dir. Move it one layer up so it's in the same folder like your next.config.ts
View full answer

36 Replies

middleware.ts (app/middleware.ts)

import { NextResponse } from 'next/server';

export function middleware(request: Request) {
  const appStatus = process.env.NEXT_PUBLIC_APP_STATUS;

  if (appStatus === 'closed') {
    return NextResponse.redirect(new URL('/maintenance', request.url));
  }

  return NextResponse.next();
}

export const config = {
  matcher: ['/', '/:path*'],
};
.env

NEXT_PUBLIC_APP_STATUS=closed
it prints closed
i think the middleware never executes
right dir
pretty weird
the .env needs to be .env.local ?
if i try to print the value of APP_STATUS in the middleware it doesnt print
thats why i think the middleware isnt function at all
import { NextResponse } from 'next/server';

export function middleware(request: Request) {
  const appStatus = process.env.NEXT_PUBLIC_APP_STATUS;

  // it doesnt print the value of appStatus
  console.log(appStatus);

  if (appStatus === 'closed') {
    return NextResponse.redirect(new URL('/forbidden', request.url));
  }

  return NextResponse.next();
}

export const config = {
  matcher: ['/', '/:path*'],
};
@oguisantosilva I am quite confused right now. Is your issue solved like that?
i just checked because i already did that, sorry for the missunderstading
@oguisantosilva i just checked because i already did that, sorry for the missunderstading
Ah got it. Please replace this:
- appStatus === 'closed'
+ appStatus == 'closed'
i tried almost everting, the middleware isnt executing i dont know why
@oguisantosilva i tried almost everting, the middleware isnt executing i dont know why
when do u want the middleware to execute?
like on every route?
@oguisantosilva its not weird that doesnt print the appStatus?
wait... you said it print it?? You are pretty confusing 🤔
@B33fb0n3 wait... you said it print it?? You are pretty confusing 🤔
it prints on the client like the image shows
in the server (middleware) it doesnt print
@oguisantosilva in the server (middleware) it doesnt print
can you change your matcher to this:
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, sitemap.xml, robots.txt (metadata files)
     */
    '/((?!api|_next/static|_next/image|favicon.ico|sitemap.xml|robots.txt).*)',
  ],
}

Also share your file structure
i already tried dozens of things, i cant think more what can it be
ill try to reinstall OS xD
@oguisantosilva Click to see attachment
your middleware must be outside of your app dir. Move it one layer up so it's in the same folder like your next.config.ts
Answer
lol, i really thought that the middleware.ts was a file like not-found.tsx and forbidden.tsx so it should stay in the same dir
problem solved, thanks!
happy to help