middleware
Answered
oguisantosilva posted this in #help-forum
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
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
@oguisantosilva *middleware.ts* (app/middleware.ts)
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*'],
};
what's the
Also can you put it into an string like this:
appStatus
when you console.log it?Also can you put it into an string like this:
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*'],
};
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'
@oguisantosilva ts
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*'],
};
its not weird that doesnt print the appStatus?
it seems that the middleware its not even executing
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?
@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:
Also share your file structure
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