From b92ef84a283a3e8425775db5469d41c4a5ad38e6 Mon Sep 17 00:00:00 2001 From: Jan Lenoch Date: Tue, 30 Oct 2018 12:19:20 +0100 Subject: [PATCH] Handle circular references when serializing node content --- package.json | 2 +- src/normalize.js | 19 ++++++++++++++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 9eeb17cd..8c4db2cd 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "gatsby-source-kentico-cloud", "description": "Gatsby source plugin for Kentico Cloud", - "version": "2.0.0-beta1", + "version": "2.0.0-beta2", "repository": { "type": "git", "url": "https://github.com/Kentico/gatsby-source-kentico-cloud" diff --git a/src/normalize.js b/src/normalize.js index e341f1f4..85ffdd7e 100644 --- a/src/normalize.js +++ b/src/normalize.js @@ -242,7 +242,24 @@ of valid objects.`); const createKcArtifactNode = (nodeId, kcArtifact, artifactKind, typeName = ``, additionalNodeData = null) => { - const nodeContent = JSON.stringify(kcArtifact); + let processedProperties = []; + + // Handle circular references when serializing. + const nodeContent = JSON.stringify(kcArtifact, (key, value) =>{ + if (typeof value === `object` && value !== null) { + if (processedProperties.indexOf(value) !== -1) { + try { + return JSON.parse(JSON.stringify(value)); + } catch (error) { + return; + } + } + processedProperties.push(value); + } + return value; + }); + + processedProperties = null; const nodeContentDigest = crypto .createHash(`md5`)