question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add an option to avoid circular references in the object returned by `getEntries()`

See original GitHub issue

Currently I’m loading page’s data (from Contentful) in a next.js getStaticProps function. After that, I’m returning that data as a prop to my react page component.

However, getStaticProps function must not return JSON with circular references. If you do that, you end up with “circular references cannot be expressed in JSON” error.

To go around it I had to call .stringifySafe() on the entries and then parse that stringified string, so I could end up with the same JSON object, but without the circular references in it:

function removeEntryCollectionCircularReferences<T>(
  entries: EntryCollection<T>
): EntryCollection<T> {
  return JSON.parse(entries.stringifySafe()) as typeof entries;
}

export const getStaticProps: GetStaticProps = async context => {
  const pages = await contentfulClient.getEntries<IPageFields>({
    content_type: 'page',
    'fields.urlSlug': context.params.id
  });
  const page = removeEntryCollectionCircularReferences(pages).items[0];

  return { props: { page } };
};

However it is not ideal, as I’m stringifying and then parsing that whole JSON needlessly, also it requires me to have that util function. (I suspect more people will end up with similar problem soon.)

Ideally, maybe we could tell the Contentful Client not to fully serialize the linked entry, if it has been already included in the tree above it? It could be configured with a resolveCircularReferences: boolean option passed to the client or getEntries() method.

What do you think?

Environment

  • contentful.js 7.14.0
  • next 9.3.0

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:14
  • Comments:16 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
paulobmarcoscommented, Mar 20, 2020

After checking who was responsible for transforming the result, I found it to be : contentful-resolve-response.

Maybe that package should accept an option to go deep until a certain level.

In most cases, that deep level is the include parameter value. Although an option like resolveLinksDepth could be very useful to avoid circular references from a certain point.

1reaction
andreascfulcommented, Apr 21, 2022

Thank you @maapteh for providing your solution. I’m closing this issue for now since it seems that there are solutions and/or workarounds for this issue given in the thread. If anything pops up again we can definitely open it up again.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Remove or allow a circular reference - Microsoft Support
If you're using Excel 2010 or later, click File > Options > Formulas. · In the Calculation options section, select the Enable iterative...
Read more >
Better Way of Dealing With Circular References? - EF Core ...
In my opinion, the use of a DTO is the correct way of dealing with this issue. The fact that your datamodel does...
Read more >
TTree Class Reference - ROOT
Upon its invocation, a loop on all defined branches takes place that for each branch invokes the TBranch::Fill method. Add a column to...
Read more >
TypeError: cyclic object value - JavaScript - MDN Web Docs
The JavaScript exception "cyclic object value" occurs when object references were found in JSON. JSON.stringify() doesn't try to solve them ...
Read more >
TTree Class Reference - ROOT
Making several branches is particularly interesting in the data analysis phase, when one wants to histogram some attributes of an object (entry) without...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found