Open tjohnwu opened 2 years ago
export const getServerSideRedirect = async (context) => {
const token = context.req?.cookies?.token
console.log('redirect token: ', token)
const pathName = context.resolvedUrl
const newUrl = await getRedirectUrl(token, pathName)
console.log(newUrl)
if (newUrl) {
return {
redirect: {
destination: newUrl,
permanent: false,
},
}
}
return {
props: {},
}
}
Hi @tjohnwu 👋 thanks for raising this issue. Have you enabled ssr in your app by configuring Amplify like so?
import { Amplify } from "aws-amplify";
import awsConfig from "../src/aws-exports.js"
Amplify.configure({
...awsConfig,
ssr: true,
});
Also, please share the contents of your package.json file. Mostly need to see the build script for your app to see whether it's running next build
(both SSR and SSG) or next build && next export
(only SSG)
For more information: https://docs.aws.amazon.com/amplify/latest/userguide/server-side-rendering-amplify.html
Here is the snippet from my package.json
{
"name": "game",
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "next lint",
"format": "prettier --write ."
},
Yes I have Amplify.configure({}); in my _app.tsx .
It looks like there is something on the cloudfront side that is dropping my cookies from the request to getServerSideProps(). On a development build on my local machine, everything works fine.
Client (has cookies) -> SSR (no cookies found, can't forward cookies) -> Backend Service (no cookies)
It's not just my app specific token, the getServerSideProps() lambda on AWS is also missing my google analytics tokens.
Hey @tjohnwu
I am facing a similar issue, but by going to the Cloudfront distribution associated with the Amplify deployment into the Behaviours
tab you will see that for api/*
and for the Default (*)
when changing to Legacy cache settings
and Cookies
to All
you will see that cookies will show up.
Then one thing I am seeing now is that after each Amplify deployment the Cloudfront distribution is invalidated and rewritten so this setting goes back to what it was previously.
@tjohnwu it sounds like what you're experiencing is similar to @joaopgsilva 's issue. Can you try their solution?
Because this sounds more like a hosting issue than a JS one, I'm going to transfer this over to the hosting repo for better feedback and support on a more permanent solution.
Although my solution allowed for cookies to be passed, these cookies are then cached, causing unexpected responses.
So it is not a valid solution.
Using Amplify to deploy I am unable to get cookies on getServersideProps
or inside any API call.
Also experiencing this issue.
+1
@joaopgsilva's answer fixed the issue for me. The problem was actually the Cloudfront cache, after fixing it the API responses were just fine.
Behaviors
api/*
and Default (*)
, set Cookies
to All
While @0xNoxETH's solution works, the distribution's behaviors config seems to reset on every deployment...
Having the same issue with amplify not sending cookies server side. We use cookies to store information about the user, and update our UI with appropriate texts. But since this is information is not being sent, it's resulting in a minified react error #418, which states that "Hydration failed because the initial UI does not match what was rendered on the server." I've also been able to go to Cloudfront to update the cookies settings manually. But this is overriden on every deployment, and is not a maintainable deal solution...Any ideas on how we should proceed?
Having the same issue. Any progress on this?
+1
Before opening, please confirm:
JavaScript Framework
Next.js (tried with both 11 and 12)
Amplify APIs
Not applicable
Amplify Categories
No response
Environment information
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
andcontext.req.cookies
are both undefined.Expected behavior
On getServerSideProps() we should be able to retrieve the cookies of the user
Reproduction steps
Code Snippet
Log output
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