Skip to content

Commit

Permalink
Fix block preview not rendering before user interaction (#3520)
Browse files Browse the repository at this point in the history
Co-authored-by: Johannes Obermair <48853629+johnnyomair@users.noreply.github.com>
  • Loading branch information
chernylu and johnnyomair authored Feb 27, 2025
1 parent 83b8111 commit 75fb1d0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/tender-boats-yell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@comet/cms-site": patch
---

Fix block preview not rendering before user interaction
11 changes: 7 additions & 4 deletions packages/site/cms-site/src/iframebridge/useBlockPreviewFetch.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect, useRef } from "react";
import { useEffect, useState } from "react";

import { createFetchInMemoryCache } from "../graphQLFetch/fetchInMemoryCache";
import { convertPreviewDataToHeaders, createFetchWithDefaults, createGraphQLFetch, GraphQLFetch } from "../graphQLFetch/graphQLFetch";
Expand All @@ -16,16 +16,19 @@ export function useBlockPreviewFetch(apiUrl: string): { fetch: Fetch; graphQLFet
export function useBlockPreviewFetch(apiUrl?: string | undefined): { fetch: Fetch; graphQLFetch?: GraphQLFetch };
export function useBlockPreviewFetch(apiUrl?: string | undefined) {
const { showOnlyVisible, graphQLApiUrl } = useIFrameBridge();
const [graphQLFetch, setGraphQLFetch] = useState<GraphQLFetch | undefined>(() =>
apiUrl ? createBlockPreviewFetch(apiUrl, !showOnlyVisible) : undefined,
);

const graphQLFetchRef = useRef(apiUrl ? createBlockPreviewFetch(apiUrl, !showOnlyVisible) : undefined);
useEffect(() => {
if (graphQLApiUrl) {
graphQLFetchRef.current = createBlockPreviewFetch(graphQLApiUrl, !showOnlyVisible);
// We need to use an updater function here because createBlockPreviewFetch's return value would otherwise be incorrectly treated as an updater function.
setGraphQLFetch(() => createBlockPreviewFetch(graphQLApiUrl, !showOnlyVisible));
}
}, [showOnlyVisible, graphQLApiUrl]);

return {
graphQLFetch: graphQLFetchRef.current,
graphQLFetch,
fetch: cachingFetch,
};
}
Expand Down

0 comments on commit 75fb1d0

Please sign in to comment.