rsnext/errors/gssp-no-mutating-res.md
Kara f6c58cb83a
Warn when mutating res if not streaming (#30284)
In #29010, we started throwing an error if the res was mutated after
getServerSideProps() returned. This was to support classic streaming,
where it would be possible to accidentally mutate the response headers
after they were already sent. However, this change also caught [a few
non-streaming cases](https://github.com/vercel/next.js/pull/29010#issuecomment-943482743) that we don't want to break.

As such, with this change, we only throw the error if res is mutated
after gSSP returns *and* you've opted into using classic streaming.
Otherwise, we will only add a warning to the console.
2021-10-25 20:18:03 -05:00

1.1 KiB
Raw Blame History

Must not access ServerResponse after getServerSideProps() resolves

Why This Error Occurred

getServerSideProps() surfaces a ServerResponse object through the res property of its context arg. This object is not intended to be accessed or changed after getServerSideProps() resolves.

This is because the framework tries to optimize when items like headers or status codes are flushed to the browser. If they are changed after getServerSideProps() completes, we can't guarantee that the changes will work.

For this reason, accessing the object after this time is disallowed.

Possible Ways to Fix It

You can fix this error by moving any access of the res object into getServerSideProps() itself or any functions that run before getServerSideProps() returns.

If youre using a custom server and running into this problem due to session middleware like next-session or express-session, try installing the middleware in the server instead of getServerSideProps().