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

Application breaks when returning circular references from getServerSideProps #21364

Closed
samuelcastro opened this issue Jan 19, 2021 · 5 comments
Labels
bug Issue was opened via the bug report template.

Comments

@samuelcastro
Copy link

samuelcastro commented Jan 19, 2021

What version of Next.js are you using?

10.0.3

What version of Node.js are you using?

14.4.0

What browser are you using?

Chrome

What operating system are you using?

macOS

How are you deploying your application?

Vercel

Describe the Bug

Whenever a circular referenced object is returned from getServerSideProps the application breaks which is clearly not ideal.

I'd recommend the usage of Flatted instead of JSON.stringify | parse to serialized/deserialize objects.

Expected Behavior

Even when returning circular referenced objects the application should not break.

To Reproduce

Just try to return a circular referenced object like on getServerSideProps:

const a = [{one: 1}, {two: '2'}];
a[0].a = a;

This is what Next Right Now is doing to avoid issues, but I think it should be the default behaviour of Next.js. (UnlyEd/next-right-now#270)

Thanks!

@samuelcastro samuelcastro added the bug Issue was opened via the bug report template. label Jan 19, 2021
@timneutkens
Copy link
Member

Usage of JSON.stringify is intended, so not a bug. It'll become even more important with React Server Components.

@Vadorequest
Copy link
Contributor

@timneutkens How do you handle cases where you need circular references?

For instance, when you load some database entities which are linked to each other, creating circular references. Is it a valid approach to serialize them using Flatted, and return them from getServerSideProps and then parse it back in _app (using Flatted).

I don't see any issue with that, as long as it's just one props that is being serialized this way, and not the whole props object. It'd still be JSON.stringify by Next.js as expected by the framework.

@samuelcastro
Copy link
Author

@timneutkens Why is it intended and what's the importance for React Server Components?

@samuelcastro
Copy link
Author

samuelcastro commented Jan 22, 2021

Hey @timneutkens We'd love your feedback here. :)

@balazsorban44
Copy link
Member

This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@vercel vercel locked as resolved and limited conversation to collaborators Jan 28, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue was opened via the bug report template.
Projects
None yet
Development

No branches or pull requests

4 participants