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

Unable to use redirect inside getServerSideProps #366

Open
datagutt opened this issue Oct 5, 2022 · 3 comments
Open

Unable to use redirect inside getServerSideProps #366

datagutt opened this issue Oct 5, 2022 · 3 comments

Comments

@datagutt
Copy link

datagutt commented Oct 5, 2022

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;

``
@khuezy
Copy link

khuezy commented Oct 11, 2022

@datagutt please see the discussion: vercel/next.js#11281

I believe you'll need to set the statusCode: 302 in the redirect object.

@datagutt
Copy link
Author

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

@khuezy
Copy link

khuezy commented Oct 12, 2022

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants