Next.js Discord

Discord Forum

Passing informations from middleware to SSR page

Unanswered
Britannia Petite posted this in #help-forum
Open in Discord
Avatar
Britannia PetiteOP
How can I pass informations from middleware to SSR page ?

I am using App Router

4 Replies

Avatar
Toyger
cookies or headers
Avatar
Britannia PetiteOP
no it does not work
if I use request or response.cookie.set('lang', value)

it will return as a response Set-Cookie and I will not be able to know on that request information I need but on the next one
Avatar
Toyger
headers work fine
middleware:
import { NextResponse } from 'next/server'
export function middleware(request) {
   
    const headers = new Headers(request.headers);
    headers.set('middlewareSet', 'mydata');

    const resp = NextResponse.next({
      request: {
        headers
      }
    });

    return resp;
}


export const config = {
    matcher: '/((?!api|_next/static|_next/image|favicon.ico).*)',
  }


page:
import { headers } from 'next/headers'

export default function Page() {
  const headersList = headers()
  const middlewareSet = headersList.get('middlewareSet')
  console.log(middlewareSet)
  return <div>111</div>;
}