Next.js Discord

Discord Forum

Middleware redirect with push

Unanswered
Western harvester ant posted this in #help-forum
Open in Discord
Western harvester antOP
Hi! Is there a way to configure NextResponse.redirect(new URL("/login", request.url)) to push the login in history so i can go back to the page i was redirected from?

I have also tried redirect("/login", RedirectType.push) but is throwing an error in the middleware (Error: NEXT_REDIRECT)

I'd like to avoid transmitting the previous path as a search param.

24 Replies

do this instead
import { NextResponse } from 'next/server'
 
// Given an incoming request...
const loginUrl = new URL('/login', request.url)
// Add ?from=/incoming-url to the /login URL
loginUrl.searchParams.set('from', request.nextUrl.pathname)
// And redirect to the new URL
return NextResponse.redirect(loginUrl)

then handle the from url after login success
@Ray do this instead ts import { NextResponse } from 'next/server' // Given an incoming request... const loginUrl = new URL('/login', request.url) // Add ?from=/incoming-url to the /login URL loginUrl.searchParams.set('from', request.nextUrl.pathname) // And redirect to the new URL return NextResponse.redirect(loginUrl) then handle the from url after login success
Western harvester antOP
thanks for your response, but as i said above, i'd like to avoid using search params, am looking for a way to push to the router. i find it strange you can do it from server actions but not from middleware
redirect is for route handler and server action
behind the scene it throw an error to the page or the handler
that's why you got NEXT_REDIRECT in middleware
well I just tried, i can go back the page i was redirected from
Western harvester antOP
oh i see, that makes sense, so as a workaround i could transform the search param you suggested in a router.replace + router.push in the login page? but i think that would enter an infinite loop with the middleware redirecting, any way i can replace a previous history item?
@Ray well I just tried, i can go back the page i was redirected from
Western harvester antOP
tried using the search param or?
no
I try to go to a page where it will be redirect me to another page in middleware
then I press back, i just go back to the page i was redirected from
I think this is the behaviour you want?
Western harvester antOP
so like,

starting from /
going to /link (which triggers a redirect in the middleware)
redirecting to /login

then if you press back it goes to /link??
go to /
Western harvester antOP
well yea, that's a replace not a push
i want /link the router history
I wouldn't want a restricted page in the client browser history lol
Western harvester antOP
haha maybe my usage is not the best example but on some complex conditional restrictions you might be able to go back
i just want it so i can pop it later, in a way the url is not affected so the user doesn't see the previous page that was blocked
I think you can make that page and do the redirect inside it
eg app/link/page.tsx
export default function Page() {
  redirect("/login", RedirectType.push) 
}
Western harvester antOP
ehhh yes but i wanted to take advantage of the middleware to centralize stuff
lol