Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Latency after first content update + no initial update message is sent #700

Closed
lsliwaradioluz opened this issue Apr 30, 2024 · 1 comment

Comments

@lsliwaradioluz
Copy link

lsliwaradioluz commented Apr 30, 2024

Describe the bug

  1. When I update my content for the first time, the CONTENT_UPDATED message is sent after a significant delay (~5s). Subsequent updates work as expected.
  2. When I have some content saved (and unpublished) and open Live Preview, Contentful does not send the initial CONTENT_UPDATED event to populate the iframe app with the saved and unpublished content.

To Reproduce
Steps to reproduce the behavior:

  • Add the following lines to console.log messages received by your frontend rendered within iframe
window.addEventListener("message", (event) => {
  console.log(event.data.action);
});
  • Open your content in a Live Preview window in Contentful dashboard,
  • Edit your content and wait for the console.log to appear (see videos)

Expected behaviors
Message arrives immediately.

Screenshots / example
Delay:
https://github.com/contentful/live-preview/assets/39009379/c98aa0a6-0488-4393-903d-348bd6636b67

Missing first update message:
https://github.com/contentful/live-preview/assets/39009379/e839f83a-c9a0-41e0-9147-4e5892a90526

Context (please complete the following information):

  • Live preview SDK version number: 3.3.1
  • REST

Additional context
Our live preview usage. The data we pass is either a VueJS ref or React's useState state.

async function initLivePreview(
  data: ContentfulSubscribeConfig["data"] | null,
  livePreviewConfig?: LivePreviewConfig
) {
  if (typeof window === "undefined" || data == null) return;

  const CONTENTFUL_APP_URL = "https://app.contentful.com";
  const isInsideContentful =
    window.location.ancestorOrigins?.contains(CONTENTFUL_APP_URL) ?? false;
  const isWithinIframe = window.self !== window.top; /** Firefox fallback */
  const isLivePreview = isInsideContentful || isWithinIframe;

  if (!isLivePreview) return;

  const { ContentfulLivePreview } = await import("@contentful/live-preview");

  /** Default configs */
  const defaultConfig = {
    locale: "en-US",
    enableLiveUpdates: true,
    debugMode: false,
    enableInspectorMode: true,
    callback: (updatedData: ContentfulSubscribeConfig["data"]) => {
      Object.assign(data, updatedData);
    },
  };

  /** Merged configs */
  const {
    locale,
    enableLiveUpdates,
    debugMode,
    enableInspectorMode,
    callback,
  } = { ...defaultConfig, ...livePreviewConfig };

  if (!ContentfulLivePreview.initialized) {
    ContentfulLivePreview.init({
      locale,
      enableLiveUpdates,
      debugMode,
      enableInspectorMode,
    });
  }

  const unsubscribe = ContentfulLivePreview.subscribe({
    data,
    locale,
    callback,
  });

  return unsubscribe;
}
@YvesRijckaert
Copy link
Contributor

Hi 👋 I am afraid that's intended behaviour. The auto-save event in Contentful currently saves every 5 seconds. We then sent over an event CONTENT_UPDATED so customers can react to this behaviour.
We are thinking of creating a better experience here (e.g. manual save trigger or reducing the seconds). But I can't communicate any timeline yet.

If you want immediate live updates then you can use the useLiveUpdates hook, then you don't have to wait for the save event.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants