terraform-aws-next-js icon indicating copy to clipboard operation
terraform-aws-next-js copied to clipboard

Unable to use redirect inside getServerSideProps

Open datagutt opened this issue 2 years ago • 3 comments

I get an Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the clienterror 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 avatar Oct 05 '22 11:10 datagutt

@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.

khuezy avatar Oct 11 '22 16:10 khuezy

Nah, this is perfectly valid in regular Next.js: https://nextjs.org/docs/api-reference/data-fetching/get-server-side-props#redirect

datagutt avatar Oct 12 '22 10:10 datagutt

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?

khuezy avatar Oct 12 '22 14:10 khuezy