From a0d3a5da04743be50a7dcc70ba7d1efca6f51fb3 Mon Sep 17 00:00:00 2001 From: Faraaz Biyabani Date: Thu, 20 Feb 2025 14:32:32 +0530 Subject: [PATCH 1/2] fix: call onChangeCallback when live_preview parameter is present in URL Visual Builder doesn't send client-data-send on site load, this leads to the site not making any calls to the preview service which is required for the subsequent entry PATCH requests to work correctly. onChangeCallback is called when either skipInitialRender is false or when live_preview search parameter is present in the URL. Since Visual Builder provides these values through the search parameter, the site makes entry GET calls to the preview service. Since live preview doesn't provide these params in non-SSR modes, it will continue to work the same. --- src/preview/contentstack-live-preview-HOC.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/preview/contentstack-live-preview-HOC.ts b/src/preview/contentstack-live-preview-HOC.ts index 77b4edb4..a2c29beb 100644 --- a/src/preview/contentstack-live-preview-HOC.ts +++ b/src/preview/contentstack-live-preview-HOC.ts @@ -152,7 +152,14 @@ class ContentstackLivePreview { onChangeCallback; } - if (!skipInitialRender) { + const isWindowDefined = typeof window !== "undefined"; + const isLivePreviewSearchParamPresent = + isWindowDefined && + new URLSearchParams(window.location.search).has("live_preview"); + // calling onChangeCallback when live_preview search parameter + // is present because we don't send the initial client-data-send + // message in visual builder + if (!skipInitialRender || isLivePreviewSearchParamPresent) { onChangeCallback(); } From 98562c7370c9b03583d7abb3552af07bc0e9f57f Mon Sep 17 00:00:00 2001 From: Faraaz Biyabani Date: Thu, 6 Mar 2025 16:57:19 +0530 Subject: [PATCH 2/2] fix: call onLiveEdit on registration only in builder Avoid calling onLiveEdit in apps other than builder as it can lead to multiple calls in Timeline and LP. Resolving this issue would then require storing some state to ensure the callback is only called from one code location on the first load which then has the posssibiliy of breaking setups that rely on the behaviour of the callback being called multiple times. --- src/preview/contentstack-live-preview-HOC.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/preview/contentstack-live-preview-HOC.ts b/src/preview/contentstack-live-preview-HOC.ts index a2c29beb..61034c69 100644 --- a/src/preview/contentstack-live-preview-HOC.ts +++ b/src/preview/contentstack-live-preview-HOC.ts @@ -156,10 +156,15 @@ class ContentstackLivePreview { const isLivePreviewSearchParamPresent = isWindowDefined && new URLSearchParams(window.location.search).has("live_preview"); + const isBuilder = + isWindowDefined && + new URLSearchParams(window.location.search).has("builder"); + const shouldCallCallback = + isLivePreviewSearchParamPresent && isBuilder; // calling onChangeCallback when live_preview search parameter // is present because we don't send the initial client-data-send // message in visual builder - if (!skipInitialRender || isLivePreviewSearchParamPresent) { + if (!skipInitialRender || shouldCallCallback) { onChangeCallback(); }