Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update getServerSideProps docs with req.cookie note #29457

Merged
merged 5 commits into from
Sep 30, 2021
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion docs/basic-features/data-fetching.md
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,7 @@ export async function getServerSideProps(context) {
The `context` parameter is an object containing the following keys:

- `params`: If this page uses a dynamic route, `params` contains the route parameters. If the page name is `[id].js` , then `params` will look like `{ id: ... }`. To learn more, take a look at the [Dynamic Routing documentation](/docs/routing/dynamic-routes.md).
- `req`: [The HTTP IncomingMessage object](https://nodejs.org/api/http.html#http_class_http_incomingmessage).
- `req`: [The HTTP IncomingMessage object](https://nodejs.org/api/http.html#http_class_http_incomingmessage), plus additional [built-in parsing helpers](#built-in-contextreq-parsing).
vitorbal marked this conversation as resolved.
Show resolved Hide resolved
- `res`: [The HTTP response object](https://nodejs.org/api/http.html#http_class_http_serverresponse).
- `query`: An object representing the query string.
- `preview`: `preview` is `true` if the page is in the preview mode and `false` otherwise. See the [Preview Mode documentation](/docs/advanced-features/preview-mode.md).
Expand Down Expand Up @@ -727,6 +727,12 @@ The `context` parameter is an object containing the following keys:
>
> Fetching from an external API is fine!

### Built-in `context.req` parsing
vitorbal marked this conversation as resolved.
Show resolved Hide resolved

To improve the developer experience, the `context.req` request object received by `getServerSideProps` is pre-parsed to include some additional helpful properties. Those properties are:
vitorbal marked this conversation as resolved.
Show resolved Hide resolved

- `context.req.cookies` - An object containing the cookies sent by the request. Defaults to `{}`
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@styfle @leerob @molebox took another stab at this, with a dedicated sub-section for context.req built-in parsing. What do you all think?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I was thinking there were more helpers than just cookies but then I checked the type and its just cookies like you mentioned

req: IncomingMessage & {
cookies: NextApiRequestCookies
}
res: ServerResponse

And it looks like res doesn't have any helpers so maybe its best to go back to your original commit where it was mentioned inline.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or maybe we discovered a strange behavior where getServerSideProps() differs to much from API Routes and we should try to use the same helpers for both 🤔

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally, I don't think it hurts to have this extra section, even if it just talks about 1 helper. Nice for google foo

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with @molebox, I like that we'd be able to deeplink to this section. Additionally, this makes it easier to expand the list in the future if we do want to get to parity with the API route helpers

vitorbal marked this conversation as resolved.
Show resolved Hide resolved

### Example

Here’s an example which uses `getServerSideProps` to fetch data at request time and pre-renders it. This example is also in the [Pages documentation](/docs/basic-features/pages.md).
Expand Down