Rewrite data returned from middleware
Unanswered
Huge Letters posted this in #help-forum
I use nextjs@13.4.2, pages dir.
I would like to get user session in middleware and then when returning statically generated html page string - I wanna append a script tag to it with session data, so that afterwards that data is readily available on the client. I know there will be a minor content flash but upon hydration it will retrieve that data from the tag.
So far I don't really see how I can modify the response from middleware tho - I don't wanna interfere with what Nextjs responds with naturally, just wanna append some data. Is that doable?
I don't want to use getServerSideProps - I still want my pages to be statically rendered.
I'm fully aware I'm trying to hack around Nextjs really hard and it's ok if this isn't possible.
I would like to get user session in middleware and then when returning statically generated html page string - I wanna append a script tag to it with session data, so that afterwards that data is readily available on the client. I know there will be a minor content flash but upon hydration it will retrieve that data from the tag.
So far I don't really see how I can modify the response from middleware tho - I don't wanna interfere with what Nextjs responds with naturally, just wanna append some data. Is that doable?
I don't want to use getServerSideProps - I still want my pages to be statically rendered.
I'm fully aware I'm trying to hack around Nextjs really hard and it's ok if this isn't possible.
1 Reply
Or should I just use a non-http-only cookie since I will get content flash before hydration regardless?.. maybe I'm overthinking the problem too much