-
Notifications
You must be signed in to change notification settings - Fork 26.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'canary' into fix/upload-binary-artifact
- Loading branch information
Showing
6 changed files
with
115 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# 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. | ||
|
||
### Useful Links | ||
|
||
- [Data Fetching Docs](https://nextjs.org/docs/basic-features/data-fetching) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
test/integration/getserversideprops/pages/promise/mutate-res-props.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
export async function getServerSideProps(context) { | ||
return { | ||
props: (async function () { | ||
// Mimic some async work, like getting data from an API | ||
await new Promise((resolve) => setTimeout(resolve)) | ||
context.res.setHeader('test-header', 'this is a test header') | ||
return { | ||
text: 'res', | ||
} | ||
})(), | ||
} | ||
} | ||
|
||
export default ({ text }) => { | ||
return ( | ||
<> | ||
<div>hello {text}</div> | ||
</> | ||
) | ||
} |
22 changes: 22 additions & 0 deletions
22
test/integration/getserversideprops/pages/promise/mutate-res.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
let mutatedRes | ||
|
||
export async function getServerSideProps(context) { | ||
mutatedRes = context.res | ||
return { | ||
props: (async function () { | ||
return { | ||
text: 'res', | ||
} | ||
})(), | ||
} | ||
} | ||
|
||
export default ({ text }) => { | ||
mutatedRes.setHeader('test-header', 'this is a test header') | ||
|
||
return ( | ||
<> | ||
<div>hello {text}</div> | ||
</> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters