Skip to content

Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.

fix(webv2): commit the preview navigation context key synchronously

joshistoast2a381f4ea99d
diff --git a/invokeai/frontend/webv2/src/workbench/widgets/preview/usePreviewNavigation.ts b/invokeai/frontend/webv2/src/workbench/widgets/preview/usePreviewNavigation.ts
index 0449dc8132..a5aaaf201e 100644
--- a/invokeai/frontend/webv2/src/workbench/widgets/preview/usePreviewNavigation.ts
+++ b/invokeai/frontend/webv2/src/workbench/widgets/preview/usePreviewNavigation.ts
@@ -17,7 +17,7 @@ import {
} from '@features/gallery/queries';
import { parseDateTokens } from '@platform/search/dateTokens';
import { useInfiniteQuery } from '@tanstack/react-query';
-import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
+import { useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState } from 'react';
import type { PreviewNavigationItem } from './previewNavigation';
@@ -166,12 +166,15 @@ export const usePreviewNavigation = ({
// Lets a boundary fetch that resolves after the user has moved on compare the
// context it started in against the one now on screen, and drop its stale
- // result. Written from an effect rather than during render: the compiler
- // rejects render-phase ref writes, and an effect event cannot be called from
- // a promise continuation.
+ // result. Written from a LAYOUT effect: layout effects run synchronously
+ // inside the commit, so no promise continuation can observe the new UI with
+ // the old key — a passive effect leaves a post-paint gap where exactly that
+ // interleaving happens. (Render-phase ref writes are rejected by the
+ // compiler, and an effect event cannot be called from a promise
+ // continuation.)
const navigationContextKeyRef = useRef(navigationContextKey);
- useEffect(() => {
+ useLayoutEffect(() => {
navigationContextKeyRef.current = navigationContextKey;
}, [navigationContextKey]);