gridsome / gridsome/gridsome

@gridsome/source-contentful - handle cyclic references when using JSON.stringify() on Rich Text

Open
#1,601 1 comment 3 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
8.5k
Forks
466
PR merge metrics
No merged PRs in 30d

Description

## Summary

Currently, the gridsome build/develop scripts will throw errors and exit when encountering an object with circular references passed to `JSON.stringify()`. This is normal functioning for `JSON.stringify()`.

Contentful allows for content with references. These references can cause cyclic errors such as a content item referencing itself.

I've attached a PR adds some extra error handling to the Gridsome contentful plugin to deal with cyclic errors. This PR contains a changed [code snippet](https://github.com/douglascrockford/JSON-js/blob/master/cycle.js) which will 'decycle' an object which can then be passed to `JSON.stringify()`. The code that the code snippet is based on is Public Domain.

### Basic example

```
const foo = { beta: 'beta'}
foo.check = foo // this is a circular reference
JSON.stringify(foo)
```
Throws `TypeError: Converting circular structure to JSON
--> starting at object with constructor 'Object'
--- property 'check' closes the circle
at JSON.stringify ()`

If we take the above example, and use the `decycle()` in the attached PR, we get this:
`{"beta":"beta","check":{"$ref":"$"}}`. Although there is a data loss/change, this is acceptable as gridsome processes would exit before.

### Motivation

Contentful allows for references that can cause cyclic errors. A basic example is 2 blog posts that reference each other. This is functionality supported by Contentful, and should be supported by @gridsome/source-contentful

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by locating the @gridsome/source-contentful handling that passes Contentful data to JSON.stringify() in the build and develop paths. Review the attached PR and its decycle snippet, then verify that cyclic references no longer make those processes exit while processing referenced content.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, nodejs
Domain
backend, content
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.