Next.js Discord

Discord Forum

purpose of the next() method of the NextResponse API

Answered
Encyrtid wasp posted this in #help-forum
Open in Discord
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
Answered by Jboncz
You can use below to terminate early

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();
        }
    }
View full answer

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
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

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
Yeah, or you can redirect it and not let it get to the endpoint, depending on what your doing.
Np bud. GL