purpose of the next() method of the NextResponse API
Answered
Encyrtid wasp posted this in #help-forum
Encyrtid waspOP
Can anyone please explain to me what the next() method of the NextResponse API in Next.js app router does? Because I’ve seen it is used two ways:
1 - NextResponse.next();
2- const someVariable = NextResponse.next();
What’s the difference between the two above? does it take any parameters or return any value? Please explain further
1 - NextResponse.next();
2- const someVariable = NextResponse.next();
What’s the difference between the two above? does it take any parameters or return any value? Please explain further
Answered by Jboncz
You can use below to terminate early
It allows it to continue on from middleware. You can also do things like;
return NextResponse.next()It allows it to continue on from middleware. You can also do things like;
//Authentication Cookie not found
if (!authenticationCookie) {
if (requestedPath.startsWith('/api/')) {
const response = NextResponse.json({ error: 'Authentication Timeout' }, { status: 200 })
const redirectLocation = referringPath;
response.cookies.set({
name: RedirectCookieName,
value: redirectLocation,
path: '/',
maxAge: 360
});
return response;
}
if (serverAction != null) {
const response = NextResponse();
response.headers.set('Middleware-Authentication', 'false')
return response;
}
else {
const response = NextResponse.redirect(`${SAMLURL}`)
const redirectLocation = `${requestedPath}${requestedPathParameters}`
//Set redirect cookie
response.cookies.set({
name: RedirectCookieName,
value: redirectLocation,
path: '/',
maxAge: 360
});
//Reroute to authentication url
return response.next();
}
}9 Replies
NextResponse.next(), as the docs say, is basically a handy function especially for middleware.It allows you to return early and it will pass on the headers that the request came with. This is what the first way is for. The second way is to add your own stuff to the response, like additional headers.
Reference:
1) https://nextjs.org/docs/app/api-reference/functions/next-response#next
2) https://github.com/vercel/next.js/blob/cc10bf5d1fa4fd2bead34661070c936d360516b3/packages/next/src/server/web/spec-extension/response.ts#L139
@Clown `NextResponse.next()`, as the docs say, is basically a handy function especially for middleware.
It allows you to return early and it will pass on the headers that the request came with. This is what the first way is for. The second way is to add your own stuff to the response, like additional headers.
Reference:
1) https://nextjs.org/docs/app/api-reference/functions/next-response#next
2) https://github.com/vercel/next.js/blob/cc10bf5d1fa4fd2bead34661070c936d360516b3/packages/next/src/server/web/spec-extension/response.ts#L139
Encyrtid waspOP
Thank you so much but what does “return early” mean in your answer?
if(condition) {
// Return early
return NextResponse.next();
}
// Rest of the code if condition isnt true@Clown
if(condition) {
// Return early
return NextResponse.next();
}
// Rest of the code if condition isnt true
Encyrtid waspOP
You mean return the NextResponse interface with same body and headers as the request?
You can use below to terminate early
It allows it to continue on from middleware. You can also do things like;
return NextResponse.next()It allows it to continue on from middleware. You can also do things like;
//Authentication Cookie not found
if (!authenticationCookie) {
if (requestedPath.startsWith('/api/')) {
const response = NextResponse.json({ error: 'Authentication Timeout' }, { status: 200 })
const redirectLocation = referringPath;
response.cookies.set({
name: RedirectCookieName,
value: redirectLocation,
path: '/',
maxAge: 360
});
return response;
}
if (serverAction != null) {
const response = NextResponse();
response.headers.set('Middleware-Authentication', 'false')
return response;
}
else {
const response = NextResponse.redirect(`${SAMLURL}`)
const redirectLocation = `${requestedPath}${requestedPathParameters}`
//Set redirect cookie
response.cookies.set({
name: RedirectCookieName,
value: redirectLocation,
path: '/',
maxAge: 360
});
//Reroute to authentication url
return response.next();
}
}Answer
@Jboncz You can use below to terminate early
js
return NextResponse.next()
It allows it to continue on from middleware. You can also do things like;
js
//Authentication Cookie not found
if (!authenticationCookie) {
if (requestedPath.startsWith('/api/')) {
const response = NextResponse.json({ error: 'Authentication Timeout' }, { status: 200 })
const redirectLocation = referringPath;
response.cookies.set({
name: RedirectCookieName,
value: redirectLocation,
path: '/',
maxAge: 360
});
return response;
}
if (serverAction != null) {
const response = NextResponse();
response.headers.set('Middleware-Authentication', 'false')
return response;
}
else {
const response = NextResponse.redirect(`${SAMLURL}`)
const redirectLocation = `${requestedPath}${requestedPathParameters}`
//Set redirect cookie
response.cookies.set({
name: RedirectCookieName,
value: redirectLocation,
path: '/',
maxAge: 360
});
//Reroute to authentication url
return response.next();
}
}
Encyrtid waspOP
That’s what I was looking for. You meant for the response to pass the middleware and continue its way to the server where the back-end handling logic is or route handler, thanks a lot.
Yeah, or you can redirect it and not let it get to the endpoint, depending on what your doing.
@Jboncz Yeah, or you can redirect it and not let it get to the endpoint, depending on what your doing.
Encyrtid waspOP
Thank you so much man
Np bud. GL