Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Fixes a loki flaky (#71225)

Sébastien292a66771713
diff --git a/frontend/src/metabase/lib/loki-utils.ts b/frontend/src/metabase/lib/loki-utils.ts
index c9b25d2bb51..59955e36c41 100644
--- a/frontend/src/metabase/lib/loki-utils.ts
+++ b/frontend/src/metabase/lib/loki-utils.ts
@@ -16,10 +16,20 @@ export const openImageBlobOnStorybook = ({
imageDownloaded.setAttribute("data-testid", "image-downloaded");
root.replaceChildren(imgElement);
- // the presence of this element is used to detect when the image is ready
- // in the storybook you'll need to `await canvas.findByTestId("image-downloaded");`
- // and then call `asyncCallback()` to continue the story
- root.appendChild(imageDownloaded);
+ // Wait for the image to fully load before signaling readiness,
+ // otherwise Loki may capture the screenshot before the image renders.
+ // In the storybook you'll need to `await canvas.findByTestId("image-downloaded");`
+ // and then call `asyncCallback()` to continue the story.
+ const onImageReady = () => {
+ root.appendChild(imageDownloaded);
+ window.document.body.style.height = "initial";
+ };
- window.document.body.style.height = "initial";
+ // The image may already be loaded if the blob URL resolved synchronously,
+ // so check `complete` to avoid a missed `load` event.
+ if (imgElement.complete) {
+ onImageReady();
+ } else {
+ imgElement.addEventListener("load", onImageReady);
+ }
};
diff --git a/frontend/src/metabase/visualizations/lib/save-chart-image.ts b/frontend/src/metabase/visualizations/lib/save-chart-image.ts
index fe6d7e17793..413d60ebed6 100644
--- a/frontend/src/metabase/visualizations/lib/save-chart-image.ts
+++ b/frontend/src/metabase/visualizations/lib/save-chart-image.ts
@@ -39,6 +39,10 @@ export const saveChartImage = async ({
// Appending any element to the node does not automatically increase the canvas height.
const canvasHeight = contentHeight + verticalOffset;
+ // Ensure fonts are fully loaded before capturing, otherwise
+ // html2canvas may render text with fallback fonts.
+ await document.fonts.ready;
+
const { default: html2canvas } = await import("html2canvas-pro");
const canvas = await html2canvas(node, {
scale: 2,
@@ -69,13 +73,19 @@ export const saveChartImage = async ({
},
});
- canvas.toBlob((blob) => {
+ if (isStorybookActive) {
+ // In storybook/loki we must wait for the blob and image to be ready
+ // before the play function returns, otherwise the async callback may
+ // be garbage-collected ("Promise was collected").
+ const blob = await new Promise<Blob | null>((resolve) =>
+ canvas.toBlob(resolve),
+ );
if (blob) {
- if (isStorybookActive) {
- // if we're running storybook we open the image in place
- // so we can test the export result with loki
- openImageBlobOnStorybook({ canvas, blob });
- } else {
+ openImageBlobOnStorybook({ canvas, blob });
+ }
+ } else {
+ canvas.toBlob((blob) => {
+ if (blob) {
const link = document.createElement("a");
const url = URL.createObjectURL(blob);
link.rel = "noopener";
@@ -85,6 +95,6 @@ export const saveChartImage = async ({
link.remove();
setTimeout(() => URL.revokeObjectURL(url), 60_000);
}
- }
- });
+ });
+ }
};