From 26fcd6bcbb0b2cc60c4f57cc201a8542c5fdad40 Mon Sep 17 00:00:00 2001 From: Evan Rusackas Date: Wed, 22 Apr 2026 16:02:55 -0700 Subject: [PATCH] fix(SqlLab): retry fetchQueryResults when resultsKey changes Replaces the one-shot hasRunInitialEffect mount guard in TabbedSqlEditors with a fetchedResultsKeyRef that records the last persisted resultsKey fetched. The old guard permanently blocked the effect once it ran, so when activeQueryEditor was unavailable on first render (common when tabHistory hydrates asynchronously), persisted results were never fetched. Tracking resultsKey lets the effect retry when it resolves and dedupes repeats. Addresses codeant-ai review on PR #39461. --- .../components/TabbedSqlEditors/index.tsx | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/superset-frontend/src/SqlLab/components/TabbedSqlEditors/index.tsx b/superset-frontend/src/SqlLab/components/TabbedSqlEditors/index.tsx index 21e930aef62..9ea64eff0b7 100644 --- a/superset-frontend/src/SqlLab/components/TabbedSqlEditors/index.tsx +++ b/superset-frontend/src/SqlLab/components/TabbedSqlEditors/index.tsx @@ -114,21 +114,23 @@ function TabbedSqlEditors({ return queryEditors.find(qe => qe.id === qeid) || null; }, [tabHistory, queryEditors]); - // Track whether the initial mount effect has run - const hasRunInitialEffect = useRef(false); + // Track the last persisted resultsKey we fetched, so the effect retries when + // the active query editor resolves after mount (or its latest query changes) + // but dedupes when the same resultsKey has already been fetched. + const fetchedResultsKeyRef = useRef(null); - // Fetch query results on initial mount if needed (equivalent to componentDidMount) + // Fetch query results for the active editor's latest query when its + // persisted resultsKey changes (equivalent to componentDidMount, but resilient + // to async hydration of activeQueryEditor). useEffect(() => { - if (hasRunInitialEffect.current) { - return; - } - hasRunInitialEffect.current = true; - const latestQuery = queries[activeQueryEditor?.latestQueryId || '']; + const resultsKey = latestQuery?.resultsKey; if ( isFeatureEnabled(FeatureFlag.SqllabBackendPersistence) && - latestQuery?.resultsKey + resultsKey && + fetchedResultsKeyRef.current !== resultsKey ) { + fetchedResultsKeyRef.current = resultsKey; // when results are not stored in localStorage they need to be // fetched from the results backend (if configured) actions.fetchQueryResults(latestQuery, displayLimit);