terraform-aws-next-js
terraform-aws-next-js copied to clipboard
Unable to use redirect inside getServerSideProps
I get an Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
error when trying to use redirect inside getServerSideProps like this:
import type { GetServerSidePropsContext, NextPage } from 'next';
import Head from 'next/head';
const Dummy: NextPage = () => {
return (
<>
<Head>
<title>Title</title>
</Head>
<div>
<p>dummy page 1</p>
</div>
</>
);
};
export const getServerSideProps = (context: GetServerSidePropsContext) => {
const { locale = 'en' } = context;
return {
redirect: {
destination: '/dummy2',
permanent: false,
},
};
};
export default Dummy;
``
@datagutt please see the discussion: https://github.com/vercel/next.js/discussions/11281
I believe you'll need to set the statusCode: 302
in the redirect
object.
Nah, this is perfectly valid in regular Next.js: https://nextjs.org/docs/api-reference/data-fetching/get-server-side-props#redirect
I haven't gotten the need to redirect via getSSP
, but I'll let you know if I come across this issue when I do.
For what it's worth, the docs also say: " In some rare cases, you might need to assign a custom status code for older HTTP clients to properly redirect", maybe the AWS lambdas are doing something weird?