Next.js Discord

Discord Forum

How to work with cookies when using NextResponse to redirect?

Answered
North Pacific hake posted this in #help-forum
Open in Discord
North Pacific hakeOP
Hi there, I am trying to use NextResponse.redirect() in middleware.js however I am also trying to delete a cookie before the redirect. How can I achieve this?
Answered by Ray
const res = NextResponse.redirect(new URL("/", req.url));
res.cookies.delete("cookie_name");
return res;
View full answer

2 Replies

@North Pacific hake Hi there, I am trying to use NextResponse.redirect() in middleware.js however I am also trying to delete a cookie before the redirect. How can I achieve this?
const res = NextResponse.redirect(new URL("/", req.url));
res.cookies.delete("cookie_name");
return res;
Answer
@Ray ts const res = NextResponse.redirect(new URL("/", req.url)); res.cookies.delete("cookie_name"); return res;
North Pacific hakeOP
Many thanks as always 🙏🏻