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

clarify possible getServerSideProps return values #33576

Closed
eklipse18 opened this issue Jan 23, 2022 · 5 comments · Fixed by #33577
Closed

clarify possible getServerSideProps return values #33576

eklipse18 opened this issue Jan 23, 2022 · 5 comments · Fixed by #33577
Labels
good first issue Easy to fix issues, good for newcomers

Comments

@eklipse18
Copy link

What is the improvement or update you wish to see?

I would like to clarify the API documentation for the return value of getServerSideProps.

Is there any context that might help us understand?

I was implementing user authentication on my website, and I was trying to restrict access to the login and signup pages if the user is logged in. To do so, I am implementing this getServerSideProps to redirect the user if they're logged in:

export async function getServerSideProps(context) {
  if (context.req.cookies.user) {
    return {
      redirect: {
        destination: '/',
        permanent: true,
      },
    }
  }
  return {}
}

If I return an empty object ({}), next gives me this error:

Reason: Props must be returned as a plain object from getServerSideProps: `{ props: { ... } }` (received: `[object Array]`).

I used an empty object because in the documentation, it is mentioned that that the props argument is optional. I later realised that maybe the content for the props property is optional, but not the object itself, and I was just proposing to clarify this for other people in the future.

Documentation

Does the docs page already exist? Please link to it.

https://nextjs.org/docs/api-reference/data-fetching/get-server-side-props

@eklipse18 eklipse18 added the Documentation Related to Next.js' official documentation. label Jan 23, 2022
@chozzz
Copy link
Contributor

chozzz commented Jan 23, 2022

The type basically says either one of these properties.

Any thoughts? Else, I will raise a PR regarding it.

chozzz pushed a commit to chozzz/next.js that referenced this issue Jan 23, 2022
@balazsorban44
Copy link
Member

balazsorban44 commented Jan 23, 2022

Yes, the correct way is to return either of those. The wording in the docs is a bit ambiguous. It doesn't make sense to use them together, neither does it to only return {}.

Feel free to open a PR to address this.

@balazsorban44 balazsorban44 added area: documentation good first issue Easy to fix issues, good for newcomers and removed Documentation Related to Next.js' official documentation. labels Jan 23, 2022
chozzz pushed a commit to chozzz/next.js that referenced this issue Jan 24, 2022
@ELBEQQAL94
Copy link

Hi, Can I work on this issue? If still open?

@chozzz
Copy link
Contributor

chozzz commented Jan 24, 2022

I have raised a PR here

@balazsorban44 balazsorban44 changed the title Docs: clarify possibly getServerSideProps return values Jan 24, 2022
@balazsorban44 balazsorban44 changed the title clarify possibly getServerSideProps return values clarify possible getServerSideProps return values Jan 24, 2022
chozzz pushed a commit to chozzz/next.js that referenced this issue Jan 25, 2022
@kodiakhq kodiakhq bot closed this as completed in #33577 Jan 26, 2022
@github-actions
Copy link
Contributor

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 25, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
good first issue Easy to fix issues, good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants