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.

perf(webv2): stop layout switches paying React's fallback throttle

joshistoast14364c6ad4ab
diff --git a/invokeai/frontend/webv2/performance/architecture-baseline.json b/invokeai/frontend/webv2/performance/architecture-baseline.json
index f04b331738..66b771cc34 100644
--- a/invokeai/frontend/webv2/performance/architecture-baseline.json
+++ b/invokeai/frontend/webv2/performance/architecture-baseline.json
@@ -2,10 +2,10 @@
"build": {
"launchpad": {
"baseline": {
- "brotliBytes": 798153,
+ "brotliBytes": 798155,
"cssRawBytes": 2159,
"fontRawBytes": 219480,
- "gzipBytes": 798207,
+ "gzipBytes": 798203,
"imageRawBytes": 156376,
"initialRawBytes": 1831516,
"largestAssetRawBytes": 737748,
@@ -238,10 +238,10 @@
]
},
"limits": {
- "brotliBytes": 806135,
+ "brotliBytes": 806137,
"cssRawBytes": 2181,
"fontRawBytes": 221675,
- "gzipBytes": 806190,
+ "gzipBytes": 806186,
"imageRawBytes": 157940,
"initialRawBytes": 1849832,
"largestAssetRawBytes": 745126,
@@ -256,15 +256,15 @@
},
"editor": {
"baseline": {
- "brotliBytes": 971557,
+ "brotliBytes": 971639,
"cssRawBytes": 2159,
"fontRawBytes": 219480,
- "gzipBytes": 970237,
+ "gzipBytes": 970352,
"imageRawBytes": 156376,
- "initialRawBytes": 2358365,
+ "initialRawBytes": 2358708,
"largestAssetRawBytes": 737748,
"otherAssetRawBytes": 0,
- "ownedRawBytes": 106268,
+ "ownedRawBytes": 106611,
"requestCount": 75,
"scriptRequestCount": 59,
"sourceOwners": [
@@ -723,15 +723,15 @@
]
},
"limits": {
- "brotliBytes": 981273,
+ "brotliBytes": 981356,
"cssRawBytes": 2181,
"fontRawBytes": 221675,
- "gzipBytes": 979940,
+ "gzipBytes": 980056,
"imageRawBytes": 157940,
- "initialRawBytes": 2381949,
+ "initialRawBytes": 2382296,
"largestAssetRawBytes": 745126,
"otherAssetRawBytes": 0,
- "ownedRawBytes": 106268,
+ "ownedRawBytes": 106611,
"requestCount": 75,
"scriptRequestCount": 59
},
@@ -740,7 +740,7 @@
"source": "src/app/WorkbenchApp.tsx"
}
},
- "capturedAt": "2026-07-26",
+ "capturedAt": "2026-07-27",
"developmentInvalidation": {
"platformUiBarrel": {
"specifier": "@platform/ui",
diff --git a/invokeai/frontend/webv2/performance/browser-baseline.json b/invokeai/frontend/webv2/performance/browser-baseline.json
index fe79bcddc0..e35ea824ff 100644
--- a/invokeai/frontend/webv2/performance/browser-baseline.json
+++ b/invokeai/frontend/webv2/performance/browser-baseline.json
@@ -1,6 +1,6 @@
{
"browserExecutable": "/home/toast/.cache/ms-playwright/chromium-1228/chrome-linux64/chrome",
- "capturedAt": "2026-07-26",
+ "capturedAt": "2026-07-27",
"routes": [
{
"activatedResourceBaseline": {
@@ -25,11 +25,11 @@
"scriptRequestCount": 0,
"totalRawBytes": 0
},
- "domContentLoadedMedianMs": 123,
+ "domContentLoadedMedianMs": 109.79999999888241,
"id": "launchpad",
"layoutSwitchMedianMs": 0,
- "loadMedianMs": 123,
- "longestTaskMaxMs": 77,
+ "loadMedianMs": 109.79999999888241,
+ "longestTaskMaxMs": 70,
"owner": "app",
"projectSwitchMedianMs": 0,
"readyMark": "invokeai:ready:launchpad",
@@ -56,7 +56,7 @@
"scriptRequestCount": 15,
"totalRawBytes": 1584282
},
- "routeReadyMedianMs": 254.5,
+ "routeReadyMedianMs": 225.09999999962747,
"scriptSourceOwnerSet": "launchpad-static",
"stateProfile": "empty"
},
@@ -83,11 +83,11 @@
"scriptRequestCount": 0,
"totalRawBytes": 0
},
- "domContentLoadedMedianMs": 121.79999999701977,
+ "domContentLoadedMedianMs": 106.40000000037253,
"id": "launchpad",
"layoutSwitchMedianMs": 0,
- "loadMedianMs": 121.89999999850988,
- "longestTaskMaxMs": 75,
+ "loadMedianMs": 106.40000000037253,
+ "longestTaskMaxMs": 67,
"owner": "app",
"projectSwitchMedianMs": 0,
"readyMark": "invokeai:ready:launchpad",
@@ -114,7 +114,7 @@
"scriptRequestCount": 15,
"totalRawBytes": 1584282
},
- "routeReadyMedianMs": 314.5,
+ "routeReadyMedianMs": 276.7999999988824,
"scriptSourceOwnerSet": "launchpad-static",
"stateProfile": "representative"
},
@@ -141,13 +141,13 @@
"scriptRequestCount": 0,
"totalRawBytes": 0
},
- "domContentLoadedMedianMs": 118.89999999850988,
+ "domContentLoadedMedianMs": 109.19999999925494,
"id": "editor-minimal",
"layoutSwitchMedianMs": 0,
- "loadMedianMs": 119,
- "longestTaskMaxMs": 122,
+ "loadMedianMs": 109.19999999925494,
+ "longestTaskMaxMs": 114,
"owner": "workbench",
- "projectSwitchMedianMs": 68.03515899999911,
+ "projectSwitchMedianMs": 61.89809799999966,
"readyMark": "invokeai:ready:widget:center:preview",
"remediationTicket": "deepen-widget-registry-loading",
"resourceBaseline": {
@@ -157,9 +157,9 @@
"largestAssetRawBytes": 737748,
"otherRawBytes": 89272,
"requestCount": 95,
- "scriptRawBytes": 2919222,
+ "scriptRawBytes": 2920198,
"scriptRequestCount": 89,
- "totalRawBytes": 3206106
+ "totalRawBytes": 3207082
},
"resourceLimits": {
"cssRawBytes": 17748,
@@ -168,11 +168,11 @@
"largestAssetRawBytes": 745126,
"otherRawBytes": 90165,
"requestCount": 95,
- "scriptRawBytes": 2948415,
+ "scriptRawBytes": 2949400,
"scriptRequestCount": 89,
- "totalRawBytes": 3238168
+ "totalRawBytes": 3239153
},
- "routeReadyMedianMs": 650.8999999985099,
+ "routeReadyMedianMs": 609,
"scriptSourceOwnerSet": "editor-minimal-static",
"stateProfile": "empty"
},
@@ -199,13 +199,13 @@
"scriptRequestCount": 0,
"totalRawBytes": 0
},
- "domContentLoadedMedianMs": 124.40000000596046,
+ "domContentLoadedMedianMs": 111.79999999888241,
"id": "editor-minimal",
"layoutSwitchMedianMs": 0,
- "loadMedianMs": 124.5,
- "longestTaskMaxMs": 138,
+ "loadMedianMs": 111.90000000037253,
+ "longestTaskMaxMs": 123,
"owner": "workbench",
- "projectSwitchMedianMs": 92.14579700000104,
+ "projectSwitchMedianMs": 78.9058159999986,
"readyMark": "invokeai:ready:widget:center:preview",
"remediationTicket": "deepen-widget-registry-loading",
"resourceBaseline": {
@@ -215,9 +215,9 @@
"largestAssetRawBytes": 737748,
"otherRawBytes": 89272,
"requestCount": 95,
- "scriptRawBytes": 2919222,
+ "scriptRawBytes": 2920198,
"scriptRequestCount": 89,
- "totalRawBytes": 3206106
+ "totalRawBytes": 3207082
},
"resourceLimits": {
"cssRawBytes": 17748,
@@ -226,11 +226,11 @@
"largestAssetRawBytes": 745126,
"otherRawBytes": 90165,
"requestCount": 95,
- "scriptRawBytes": 2948415,
+ "scriptRawBytes": 2949400,
"scriptRequestCount": 89,
- "totalRawBytes": 3238168
+ "totalRawBytes": 3239153
},
- "routeReadyMedianMs": 702.7000000029802,
+ "routeReadyMedianMs": 657.5999999996275,
"scriptSourceOwnerSet": "editor-minimal-static",
"stateProfile": "representative"
},
@@ -257,11 +257,11 @@
"scriptRequestCount": 7,
"totalRawBytes": 293339
},
- "domContentLoadedMedianMs": 123.39999999850988,
+ "domContentLoadedMedianMs": 110.70000000111759,
"id": "editor-canvas",
- "layoutSwitchMedianMs": 626.7000000029802,
- "loadMedianMs": 123.5,
- "longestTaskMaxMs": 125,
+ "layoutSwitchMedianMs": 325.7999999988824,
+ "loadMedianMs": 110.70000000111759,
+ "longestTaskMaxMs": 117,
"owner": "canvas",
"projectSwitchMedianMs": 0,
"readyMark": "invokeai:ready:widget:center:canvas",
@@ -273,9 +273,9 @@
"largestAssetRawBytes": 737748,
"otherRawBytes": 89272,
"requestCount": 99,
- "scriptRawBytes": 3195421,
+ "scriptRawBytes": 3196397,
"scriptRequestCount": 93,
- "totalRawBytes": 3482305
+ "totalRawBytes": 3483281
},
"resourceLimits": {
"cssRawBytes": 17748,
@@ -284,11 +284,11 @@
"largestAssetRawBytes": 745126,
"otherRawBytes": 90165,
"requestCount": 99,
- "scriptRawBytes": 3227376,
+ "scriptRawBytes": 3228361,
"scriptRequestCount": 93,
- "totalRawBytes": 3517129
+ "totalRawBytes": 3518114
},
- "routeReadyMedianMs": 1083.8000000044703,
+ "routeReadyMedianMs": 752.0999999996275,
"scriptSourceOwnerSet": "editor-canvas-static",
"stateProfile": "empty"
},
@@ -315,11 +315,11 @@
"scriptRequestCount": 7,
"totalRawBytes": 293339
},
- "domContentLoadedMedianMs": 131.10000000149012,
+ "domContentLoadedMedianMs": 111.09999999962747,
"id": "editor-canvas",
- "layoutSwitchMedianMs": 806.3000000044703,
- "loadMedianMs": 131.20000000298023,
- "longestTaskMaxMs": 249,
+ "layoutSwitchMedianMs": 668.3999999985099,
+ "loadMedianMs": 111.09999999962747,
+ "longestTaskMaxMs": 369,
"owner": "canvas",
"projectSwitchMedianMs": 0,
"readyMark": "invokeai:ready:widget:center:canvas",
@@ -331,9 +331,9 @@
"largestAssetRawBytes": 737748,
"otherRawBytes": 89272,
"requestCount": 99,
- "scriptRawBytes": 3195421,
+ "scriptRawBytes": 3196397,
"scriptRequestCount": 93,
- "totalRawBytes": 3482305
+ "totalRawBytes": 3483281
},
"resourceLimits": {
"cssRawBytes": 17748,
@@ -342,11 +342,11 @@
"largestAssetRawBytes": 745126,
"otherRawBytes": 90165,
"requestCount": 99,
- "scriptRawBytes": 3227376,
+ "scriptRawBytes": 3228361,
"scriptRequestCount": 93,
- "totalRawBytes": 3517129
+ "totalRawBytes": 3518114
},
- "routeReadyMedianMs": 1353.0999999940395,
+ "routeReadyMedianMs": 1122.6000000014901,
"scriptSourceOwnerSet": "editor-canvas-static",
"stateProfile": "representative"
},
@@ -373,11 +373,11 @@
"scriptRequestCount": 3,
"totalRawBytes": 14378
},
- "domContentLoadedMedianMs": 119.89999999850988,
+ "domContentLoadedMedianMs": 108.40000000037253,
"id": "editor-workflow",
- "layoutSwitchMedianMs": 349.29999999701977,
- "loadMedianMs": 120,
- "longestTaskMaxMs": 134,
+ "layoutSwitchMedianMs": 411.69999999925494,
+ "loadMedianMs": 108.40000000037253,
+ "longestTaskMaxMs": 110,
"owner": "workflow",
"projectSwitchMedianMs": 0,
"readyMark": "invokeai:ready:widget:center:workflow",
@@ -389,9 +389,9 @@
"largestAssetRawBytes": 737748,
"otherRawBytes": 89272,
"requestCount": 95,
- "scriptRawBytes": 2919222,
+ "scriptRawBytes": 2920198,
"scriptRequestCount": 89,
- "totalRawBytes": 3206106
+ "totalRawBytes": 3207082
},
"resourceLimits": {
"cssRawBytes": 17748,
@@ -400,11 +400,11 @@
"largestAssetRawBytes": 745126,
"otherRawBytes": 90165,
"requestCount": 95,
- "scriptRawBytes": 2948415,
+ "scriptRawBytes": 2949400,
"scriptRequestCount": 89,
- "totalRawBytes": 3238168
+ "totalRawBytes": 3239153
},
- "routeReadyMedianMs": 791.8999999985099,
+ "routeReadyMedianMs": 820.2000000011176,
"scriptSourceOwnerSet": "editor-minimal-static",
"stateProfile": "empty"
},
@@ -431,11 +431,11 @@
"scriptRequestCount": 3,
"totalRawBytes": 14378
},
- "domContentLoadedMedianMs": 122.5,
+ "domContentLoadedMedianMs": 110.5,
"id": "editor-workflow",
- "layoutSwitchMedianMs": 555.8999999985099,
- "loadMedianMs": 122.5,
- "longestTaskMaxMs": 473,
+ "layoutSwitchMedianMs": 484.90000000037253,
+ "loadMedianMs": 110.5,
+ "longestTaskMaxMs": 386,
"owner": "workflow",
"projectSwitchMedianMs": 0,
"readyMark": "invokeai:ready:widget:center:workflow",
@@ -447,9 +447,9 @@
"largestAssetRawBytes": 737748,
"otherRawBytes": 89272,
"requestCount": 95,
- "scriptRawBytes": 2919222,
+ "scriptRawBytes": 2920198,
"scriptRequestCount": 89,
- "totalRawBytes": 3206106
+ "totalRawBytes": 3207082
},
"resourceLimits": {
"cssRawBytes": 17748,
@@ -458,11 +458,11 @@
"largestAssetRawBytes": 745126,
"otherRawBytes": 90165,
"requestCount": 95,
- "scriptRawBytes": 2948415,
+ "scriptRawBytes": 2949400,
"scriptRequestCount": 89,
- "totalRawBytes": 3238168
+ "totalRawBytes": 3239153
},
- "routeReadyMedianMs": 1064.7000000029802,
+ "routeReadyMedianMs": 945.3000000007451,
"scriptSourceOwnerSet": "editor-minimal-static",
"stateProfile": "representative"
},
@@ -489,11 +489,11 @@
"scriptRequestCount": 3,
"totalRawBytes": 14378
},
- "domContentLoadedMedianMs": 119.10000000149012,
+ "domContentLoadedMedianMs": 109.69999999925494,
"id": "editor-gallery",
- "layoutSwitchMedianMs": 333.09999999403954,
- "loadMedianMs": 119.10000000149012,
- "longestTaskMaxMs": 132,
+ "layoutSwitchMedianMs": 268.8999999985099,
+ "loadMedianMs": 109.69999999925494,
+ "longestTaskMaxMs": 109,
"owner": "gallery",
"projectSwitchMedianMs": 0,
"readyMark": "invokeai:ready:widget:center:gallery",
@@ -505,9 +505,9 @@
"largestAssetRawBytes": 737748,
"otherRawBytes": 89272,
"requestCount": 95,
- "scriptRawBytes": 2919222,
+ "scriptRawBytes": 2920198,
"scriptRequestCount": 89,
- "totalRawBytes": 3206106
+ "totalRawBytes": 3207082
},
"resourceLimits": {
"cssRawBytes": 17748,
@@ -516,11 +516,11 @@
"largestAssetRawBytes": 745126,
"otherRawBytes": 90165,
"requestCount": 95,
- "scriptRawBytes": 2948415,
+ "scriptRawBytes": 2949400,
"scriptRequestCount": 89,
- "totalRawBytes": 3238168
+ "totalRawBytes": 3239153
},
- "routeReadyMedianMs": 795.6999999955297,
+ "routeReadyMedianMs": 681,
"scriptSourceOwnerSet": "editor-minimal-static",
"stateProfile": "empty"
},
@@ -547,11 +547,11 @@
"scriptRequestCount": 3,
"totalRawBytes": 14378
},
- "domContentLoadedMedianMs": 124,
+ "domContentLoadedMedianMs": 110.80000000074506,
"id": "editor-gallery",
- "layoutSwitchMedianMs": 450.20000000298023,
- "loadMedianMs": 124,
- "longestTaskMaxMs": 136,
+ "layoutSwitchMedianMs": 398.59999999962747,
+ "loadMedianMs": 110.80000000074506,
+ "longestTaskMaxMs": 124,
"owner": "gallery",
"projectSwitchMedianMs": 0,
"readyMark": "invokeai:ready:widget:center:gallery",
@@ -563,9 +563,9 @@
"largestAssetRawBytes": 737748,
"otherRawBytes": 89272,
"requestCount": 95,
- "scriptRawBytes": 2919222,
+ "scriptRawBytes": 2920198,
"scriptRequestCount": 89,
- "totalRawBytes": 3206106
+ "totalRawBytes": 3207082
},
"resourceLimits": {
"cssRawBytes": 17748,
@@ -574,11 +574,11 @@
"largestAssetRawBytes": 745126,
"otherRawBytes": 90165,
"requestCount": 95,
- "scriptRawBytes": 2948415,
+ "scriptRawBytes": 2949400,
"scriptRequestCount": 89,
- "totalRawBytes": 3238168
+ "totalRawBytes": 3239153
},
- "routeReadyMedianMs": 960.7000000029802,
+ "routeReadyMedianMs": 863,
"scriptSourceOwnerSet": "editor-minimal-static",
"stateProfile": "representative"
}
diff --git a/invokeai/frontend/webv2/src/workbench/shell/topbar/LayoutPresetMenu.tsx b/invokeai/frontend/webv2/src/workbench/shell/topbar/LayoutPresetMenu.tsx
index 921ee95931..1704211393 100644
--- a/invokeai/frontend/webv2/src/workbench/shell/topbar/LayoutPresetMenu.tsx
+++ b/invokeai/frontend/webv2/src/workbench/shell/topbar/LayoutPresetMenu.tsx
@@ -5,7 +5,7 @@ import { HStack, Icon, Menu, Portal, Stack, Text } from '@chakra-ui/react';
import { Button, ConfirmDialog, IconButton, MenuContent, RenameDialog } from '@platform/ui';
import { layoutPresets } from '@workbench/layoutPresets';
import { areLayoutPresetSnapshotsEqual, createLayoutPresetSnapshot } from '@workbench/layoutPresetSnapshots';
-import { preloadWidget } from '@workbench/widgetRegistry';
+import { loadWidget, preloadWidget } from '@workbench/widgetRegistry';
import { useActiveProjectSelector, useWorkbenchCommands, useWorkbenchSelector } from '@workbench/WorkbenchContext';
import { CheckIcon, ChevronDownIcon, EllipsisVerticalIcon, PencilIcon, PlusIcon, Trash2Icon } from 'lucide-react';
import { useCallback, useMemo, useState } from 'react';
@@ -35,6 +35,30 @@ const preloadLayoutPreset = (preset: LayoutPreset): void => {
}
};
+/**
+ * Resolves once every widget the preset will show is loaded.
+ *
+ * Applying a preset used to dispatch immediately after kicking off a preload,
+ * so the widgets it revealed rendered against a pending promise and suspended.
+ * That is what cost the switch its time — not the download, which is already
+ * finished by then, but React withholding the resolved tree for 300ms
+ * (`FALLBACK_THROTTLE_MS`) once a fallback has been shown. Waiting here instead
+ * keeps the previous layout on screen for the few ms the modules actually need
+ * and lets the new one commit immediately.
+ *
+ * `allSettled`, not `all`: a widget that fails to load must still be placed, so
+ * its own failure boundary can report it.
+ */
+const loadLayoutPreset = (preset: LayoutPreset): Promise<unknown> =>
+ Promise.allSettled(
+ Object.values(preset.snapshot.widgetRegions).flatMap((region) => {
+ const typeId = preset.snapshot.widgetInstances[region.activeInstanceId]?.typeId;
+ const pending = typeId ? loadWidget(typeId) : null;
+
+ return pending ? [pending] : [];
+ })
+ );
+
/** Global layout preset registry surfaced as a menu. */
export const LayoutPresetMenu = () => {
const activeLayoutSnapshot = useActiveProjectSelector(createLayoutPresetSnapshot, areLayoutPresetSnapshotsEqual);
@@ -73,8 +97,9 @@ export const LayoutPresetMenu = () => {
);
const applyPreset = useCallback(
(preset: LayoutPreset) => {
- preloadLayoutPreset(preset);
- layout.applyPreset(preset.id);
+ void loadLayoutPreset(preset).then(() => {
+ layout.applyPreset(preset.id);
+ });
},
[layout]
);
diff --git a/invokeai/frontend/webv2/src/workbench/widgetImplementationResource.test.ts b/invokeai/frontend/webv2/src/workbench/widgetImplementationResource.test.ts
index 3a482273b0..e91f37879d 100644
--- a/invokeai/frontend/webv2/src/workbench/widgetImplementationResource.test.ts
+++ b/invokeai/frontend/webv2/src/workbench/widgetImplementationResource.test.ts
@@ -53,4 +53,53 @@ describe('widget implementation resource', () => {
await vi.waitFor(() => expect(resource.getStatus()).toBe('failed'));
expect(loader).toHaveBeenCalledOnce();
});
+
+ // React's `use()` reads these fields off the thenable before deciding whether
+ // to suspend. Without them a settled promise still costs a suspension on first
+ // read — `use()` cannot inspect a native promise synchronously, so it attaches
+ // a callback and throws. That suspension shows a fallback, and once a fallback
+ // is on screen React withholds the resolved tree for FALLBACK_THROTTLE_MS
+ // (300ms) to avoid a flash. That throttle was the entire measured cost of
+ // switching layout, long after the chunk had finished downloading, so these
+ // three fields are load-bearing rather than decorative.
+ describe('exposes its settled state on the promise for React `use()`', () => {
+ it('marks the promise pending, then fulfilled with the value', async () => {
+ const resource = createWidgetImplementationResource('test', () => Promise.resolve(implementation));
+
+ const pending = resource.load() as Promise<WidgetImplementation> & {
+ status?: string;
+ value?: WidgetImplementation;
+ };
+ expect(pending.status).toBe('pending');
+
+ await pending;
+
+ expect(pending.status).toBe('fulfilled');
+ expect(pending.value).toBe(implementation);
+ });
+
+ it('marks the promise rejected with the reason', async () => {
+ const error = new Error('offline');
+ const resource = createWidgetImplementationResource('test', () => Promise.reject(error));
+
+ const pending = resource.load() as Promise<WidgetImplementation> & { reason?: unknown; status?: string };
+ await expect(pending).rejects.toThrow('offline');
+
+ expect(pending.status).toBe('rejected');
+ expect(pending.reason).toBe(error);
+ });
+
+ it('keeps the settled state on the promise handed to later callers', async () => {
+ const resource = createWidgetImplementationResource('test', () => Promise.resolve(implementation));
+
+ await resource.load();
+ const again = resource.load() as Promise<WidgetImplementation> & {
+ status?: string;
+ value?: WidgetImplementation;
+ };
+
+ expect(again.status).toBe('fulfilled');
+ expect(again.value).toBe(implementation);
+ });
+ });
});
diff --git a/invokeai/frontend/webv2/src/workbench/widgetImplementationResource.ts b/invokeai/frontend/webv2/src/workbench/widgetImplementationResource.ts
index 8ea36f7d1a..bbed381b0b 100644
--- a/invokeai/frontend/webv2/src/workbench/widgetImplementationResource.ts
+++ b/invokeai/frontend/webv2/src/workbench/widgetImplementationResource.ts
@@ -11,6 +11,24 @@ type ResourceState =
| { promise: Promise<WidgetImplementation>; status: 'loaded'; value: WidgetImplementation }
| { error: unknown; promise: Promise<WidgetImplementation>; status: 'failed' };
+/**
+ * The shape React's `use()` inspects before deciding to suspend. A bare promise
+ * — even an already-resolved one — always costs a suspension on first read,
+ * because `use()` cannot read a native promise synchronously; it has to attach a
+ * callback, throw, and retry on the ping. Settling these fields as soon as the
+ * load completes lets `use()` return the value on the very first render instead.
+ *
+ * That distinction is worth real time. Suspending shows a fallback, and once a
+ * fallback has been shown React withholds the resolved tree for
+ * `FALLBACK_THROTTLE_MS` (300ms) to avoid a flash — which measured as the whole
+ * cost of switching layout, long after the chunk had finished downloading.
+ */
+interface TrackedThenable<T> extends Promise<T> {
+ reason?: unknown;
+ status?: 'fulfilled' | 'pending' | 'rejected';
+ value?: T;
+}
+
const validateImplementation = (widgetId: WidgetTypeId, value: WidgetImplementation): WidgetImplementation => {
if (!value || typeof value !== 'object' || typeof value.view !== 'function') {
throw new TypeError(`Widget ${widgetId} implementation must provide a view component.`);
@@ -26,15 +44,20 @@ export const createWidgetImplementationResource = (
let state: ResourceState = { status: 'idle' };
const start = (): Promise<WidgetImplementation> => {
- const promise = Promise.resolve()
+ const promise: TrackedThenable<WidgetImplementation> = Promise.resolve()
.then(loader)
.then((implementation) => validateImplementation(widgetId, implementation));
+ promise.status = 'pending';
state = { promise, status: 'loading' };
void promise.then(
(value) => {
+ promise.status = 'fulfilled';
+ promise.value = value;
state = { promise, status: 'loaded', value };
},
(error: unknown) => {
+ promise.status = 'rejected';
+ promise.reason = error;
state = { error, promise, status: 'failed' };
}
);
diff --git a/invokeai/frontend/webv2/src/workbench/widgetRegistry.ts b/invokeai/frontend/webv2/src/workbench/widgetRegistry.ts
index bff729ec8c..d8ee0f5d43 100644
--- a/invokeai/frontend/webv2/src/workbench/widgetRegistry.ts
+++ b/invokeai/frontend/webv2/src/workbench/widgetRegistry.ts
@@ -156,6 +156,23 @@ export const preloadWidget = (widgetId: WidgetTypeId): void => {
}
};
+/**
+ * Like {@link preloadWidget}, but resolves when the implementation is in hand.
+ *
+ * Callers that are about to make a widget appear use this to get the module
+ * BEFORE committing the state change, so the render never suspends. Suspending
+ * is not free even when the chunk is already downloaded: showing a fallback
+ * makes React withhold the resolved content for `FALLBACK_THROTTLE_MS` (300ms)
+ * to avoid a flash, which measured as the entire cost of a layout switch.
+ *
+ * Returns `null` for an unknown or disabled widget, so callers can skip it.
+ */
+export const loadWidget = (widgetId: WidgetTypeId): Promise<unknown> | null => {
+ const widget = getWidgetById(widgetId);
+
+ return widget?.status === 'enabled' ? widget.implementation.load() : null;
+};
+
export const widgetRegistrationFailures = registeredWidgets.flatMap((widget) =>
widget.failure ? [widget.failure] : []
);