@contentful/experiences-core 1.30.0-dev-20250128T1255-64f728a.0 → 1.30.0-dev-20250129T1107-40c1b7c.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -3570,22 +3570,22 @@ const fetchReferencedEntities = async ({ client, experienceEntry, locale, }) =>
3570
3570
  throw new Error('Failed to fetch experience entities. Provided "experienceEntry" does not match experience entry schema');
3571
3571
  }
3572
3572
  const deepReferences = gatherDeepReferencesFromExperienceEntry(experienceEntry);
3573
- const entryIds = [];
3574
- const assetIds = [];
3573
+ const entryIds = new Set();
3574
+ const assetIds = new Set();
3575
3575
  for (const dataBinding of Object.values(experienceEntry.fields.dataSource)) {
3576
3576
  if (!('sys' in dataBinding)) {
3577
3577
  continue;
3578
3578
  }
3579
3579
  if (dataBinding.sys.linkType === 'Entry') {
3580
- entryIds.push(dataBinding.sys.id);
3580
+ entryIds.add(dataBinding.sys.id);
3581
3581
  }
3582
3582
  if (dataBinding.sys.linkType === 'Asset') {
3583
- assetIds.push(dataBinding.sys.id);
3583
+ assetIds.add(dataBinding.sys.id);
3584
3584
  }
3585
3585
  }
3586
3586
  const [entriesResponse, assetsResponse] = await Promise.all([
3587
- fetchAllEntries({ client, ids: entryIds, locale }),
3588
- fetchAllAssets({ client, ids: assetIds, locale }),
3587
+ fetchAllEntries({ client, ids: [...entryIds], locale }),
3588
+ fetchAllAssets({ client, ids: [...assetIds], locale }),
3589
3589
  ]);
3590
3590
  const { autoFetchedReferentAssets, autoFetchedReferentEntries } = gatherAutoFetchedReferentsFromIncludes(deepReferences, entriesResponse);
3591
3591
  // Using client getEntries resolves all linked entry references, so we do not need to resolve entries in usedComponents
@@ -3640,6 +3640,33 @@ const removeCircularPatternReferences = (experienceEntry, _parentIds) => {
3640
3640
  // @ts-expect-error - type of usedComponents doesn't yet allow a mixed list of both links and entries
3641
3641
  experienceEntry.fields.usedComponents = newUsedComponents;
3642
3642
  };
3643
+ /**
3644
+ * The CMA client will automatically replace links with entry references if they are available.
3645
+ * While we're not fetching the data sources, a self reference would be replaced as the entry is
3646
+ * fetched. Any circuar reference in the object breaks SSR where we have to stringify the JSON.
3647
+ * This would fail if the object contains circular references.
3648
+ */
3649
+ const removeSelfReferencingDataSource = (experienceEntry) => {
3650
+ const dataSources = experienceEntry.fields.dataSource;
3651
+ const newDataSource = Object.entries(dataSources).reduce((acc, [key, linkOrEntry]) => {
3652
+ if ('fields' in linkOrEntry && linkOrEntry.sys.id === experienceEntry.sys.id) {
3653
+ const entry = linkOrEntry;
3654
+ acc[key] = {
3655
+ sys: {
3656
+ id: entry.sys.id,
3657
+ linkType: 'Entry',
3658
+ type: 'Link',
3659
+ },
3660
+ };
3661
+ }
3662
+ else {
3663
+ const link = linkOrEntry;
3664
+ acc[key] = link;
3665
+ }
3666
+ return acc;
3667
+ }, {});
3668
+ experienceEntry.fields.dataSource = newDataSource;
3669
+ };
3643
3670
 
3644
3671
  const errorMessagesWhileFetching$1 = {
3645
3672
  experience: 'Failed to fetch experience',
@@ -3671,6 +3698,7 @@ async function fetchBySlug({ client, experienceTypeId, slug, localeCode, isEdito
3671
3698
  throw new Error(`No experience entry with slug: ${slug} exists`);
3672
3699
  }
3673
3700
  removeCircularPatternReferences(experienceEntry);
3701
+ removeSelfReferencingDataSource(experienceEntry);
3674
3702
  try {
3675
3703
  const { entries, assets } = await fetchReferencedEntities({
3676
3704
  client,
@@ -3724,6 +3752,7 @@ async function fetchById({ client, experienceTypeId, id, localeCode, isEditorMod
3724
3752
  throw new Error(`No experience entry with id: ${id} exists`);
3725
3753
  }
3726
3754
  removeCircularPatternReferences(experienceEntry);
3755
+ removeSelfReferencingDataSource(experienceEntry);
3727
3756
  try {
3728
3757
  const { entries, assets } = await fetchReferencedEntities({
3729
3758
  client,