aws-amplify / aws-amplify/amplify-hosting
NextJS getServerSideProps() missing cookies
- Dominant language
- Dockerfile
- Stars
- 481
- Forks
- 123
- PR merge metrics
- No merged PRs in 30d
Description
### Before opening, please confirm:
- [X] I have [searched for duplicate or closed issues](https://github.com/aws-amplify/amplify-js/issues?q=is%3Aissue+) and [discussions](https://github.com/aws-amplify/amplify-js/discussions).
- [X] I have read the guide for [submitting bug reports](https://github.com/aws-amplify/amplify-js/blob/main/CONTRIBUTING.md#bug-reports).
- [X] I have done my best to include a minimal, self-contained set of instructions for consistently reproducing the issue.
### JavaScript Framework
Next.js (tried with both 11 and 12)
### Amplify APIs
Not applicable
### Amplify Categories
_No response_
### Environment information
```
System:
OS: macOS 11.6.2
CPU: (8) x64 Intel(R) Core(TM) i5-1038NG7 CPU @ 2.00GHz
Memory: 56.79 MB / 16.00 GB
Shell: 5.8 - /bin/zsh
Binaries:
Node: 16.14.0 - /usr/local/bin/node
Yarn: 1.22.17 - /usr/local/bin/yarn
npm: 8.3.1 - /usr/local/bin/npm
Browsers:
Brave Browser: 98.1.35.104
Chrome: 99.0.4844.51
Firefox: 97.0.2
Safari: 15.2
npmGlobalPackages:
@aws-amplify/cli: 7.6.4
corepack: 0.10.0
eas-cli: 0.4.3
expo-cli: 4.0.13
expo: 39.0.5
gatsby-cli: 3.7.1
graphql: 15.4.0
lerna: 4.0.0
mocha: 9.1.3
n: 7.5.0
npm: 8.3.1
ts-node: 10.4.0
tslint: 6.1.3
typescript: 4.5.2
```
### Describe the bug
When using getServerSideProps() in my NextJS project, I am unable to grab the cookies from the header.
When doing a clientside request with fetch() the cookies work fine.
This also works fine on my local machine through `yarn dev`.
Is there something special I have to do to use cookies in getServerSideProps()?
`context.req.headers.cookies` and `context.req.cookies` are both undefined.
### Expected behavior
On getServerSideProps() we should be able to retrieve the cookies of the user
### Reproduction steps
1. Attach cookies to the url
2. Go to a route that uses getServerSideProps(). This should automatically send the cookies with the request.
3. Try to read the cookies in getServerSideProps() but shows up as undefined
### Code Snippet
```javascript
// Put your code below this line.
```
### Log output
```
// Put your logs below this line
```
### aws-exports.js
_No response_
### Manual configuration
_No response_
### Additional configuration
_No response_
### Mobile Device
_No response_
### Mobile Operating System
_No response_
### Mobile Browser
_No response_
### Mobile Browser Version
_No response_
### Additional information and screenshots
_No response_
Contributor guide
Research direction
Start by reproducing the issue in a Next.js 11 or 12 project using getServerSideProps(), comparing yarn dev with the affected deployment. Inspect the request context and cookie handling around context.req.headers.cookies and context.req.cookies. Done means the request cookies can be retrieved during server-side rendering, with a minimal reproduction and regression coverage.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, next.js
- Domain
- cloud, web-dev
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Needs clarification
- Newbie friendliness
- 38/100