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): fetch the template catalog only when one is applied

joshistoastc0d72f2f7b03
diff --git a/invokeai/frontend/webv2/src/features/generation/ui/GenerateWidgetView.tsx b/invokeai/frontend/webv2/src/features/generation/ui/GenerateWidgetView.tsx
index 8396a07d96..36f2f66dac 100644
--- a/invokeai/frontend/webv2/src/features/generation/ui/GenerateWidgetView.tsx
+++ b/invokeai/frontend/webv2/src/features/generation/ui/GenerateWidgetView.tsx
@@ -62,10 +62,6 @@ export const GenerateWidgetView = () => {
const error = ui.models.error;
const models = ui.models.catalog;
const status = ui.models.status;
- // The active template is stored as a snapshot, so an edit made in another tab
- // or by another client only lands by re-reading it from the catalog here —
- // the same treatment model snapshots get in `syncGenerateWidgetValuesWithModels`.
- const { templates: promptTemplates } = usePromptTemplates();
useMountEffect(() => {
ui.models.ensureLoaded();
@@ -79,6 +75,17 @@ export const GenerateWidgetView = () => {
const normalizedSettings = normalizeGenerateSettings(storedValues);
const selectedModel = supportedModels.find((model) => model.key === normalizedSettings?.modelKey);
const settings = normalizedSettings ?? getDefaultGenerateSettings(selectedModel ?? supportedModels[0]);
+ // The active template is stored as a snapshot, so an edit made in another tab
+ // or by another client only lands by re-reading it from the catalog here —
+ // the same treatment model snapshots get in `syncGenerateWidgetValuesWithModels`.
+ //
+ // Fetched only when there is something to re-read. Most projects have no
+ // template applied, and asking unconditionally put a request on the widget's
+ // critical path for an answer nothing would use. The picker asks in earnest
+ // and shares this cache when it does.
+ const { templates: promptTemplates } = usePromptTemplates({
+ isEnabled: (normalizedSettings?.promptTemplate ?? null) !== null,
+ });
useEffect(() => {
const model = selectedModel ?? supportedModels[0];
diff --git a/invokeai/frontend/webv2/src/features/generation/ui/usePromptTemplates.ts b/invokeai/frontend/webv2/src/features/generation/ui/usePromptTemplates.ts
index d4bbdda1e6..aaa8b0e96d 100644
--- a/invokeai/frontend/webv2/src/features/generation/ui/usePromptTemplates.ts
+++ b/invokeai/frontend/webv2/src/features/generation/ui/usePromptTemplates.ts
@@ -34,9 +34,12 @@ export interface PromptTemplateCatalog {
* started it, so a sign-out mid-flight does not invalidate the next account's
* cache on the way back — the same rule `useWildcards` follows.
*/
-export const usePromptTemplates = (): PromptTemplateCatalog => {
+export const usePromptTemplates = ({ isEnabled = true }: { isEnabled?: boolean } = {}): PromptTemplateCatalog => {
const queryClient = useQueryClient();
- const query = useQuery(promptTemplatesQueryOptions());
+ // Off by default for the widget, which only wants to re-read an applied
+ // template and has nothing to re-read when none is applied. The picker asks
+ // for it in earnest, and shares this cache when it does.
+ const query = useQuery({ ...promptTemplatesQueryOptions(), enabled: isEnabled });
const templates = useMemo(() => query.data ?? [], [query.data]);
const defaultTemplates = useMemo(() => templates.filter((template) => template.isDefault), [templates]);
const userTemplates = useMemo(() => templates.filter((template) => !template.isDefault), [templates]);