NextJS - Headers set in Middleware not available on Layout.tsx or Page.tsx

Hello

We have setup a vanilla NextJS site on version 14.2.8 when we set Headers in our middleware.ts files these are not being read by the root layout.tsx file nor any page.tsx we have.

The same code works perfectly fine on Vercel

Git Repo: GitHub - MineshS/next-troubleshooting

Commit: bare minimum set and read header · MineshS/next-troubleshooting@a2fa770 · GitHub

Netlify Deploy: https://nextjs-headers.netlify.app/
Vercel Deploy: https://next-troubleshooting.vercel.app/

Can anyone help me diagnose what the Issue could be

Many Thanks

Minesh

Netlify Support helped resolve this issue and suggested changing the logic from request to response which did help us resolve the issue

Solution: GitHub - hrishikesh-k/zd-316290

Explanation - "In your attempts (middleware as well as next.config.js), you were setting response headers. These headers are sent to the client and by no means are available in layout.tsx. In my current implementation, we’re changing the request headers. The flow is:

user requests page → middleware triggered → change the request headers → render layout.tsx → return response

This is why this works."