diff --git a/studio/src/motion/ExploreStudios.tsx b/studio/src/motion/ExploreStudios.tsx index f75164104..b7bcd3a5c 100644 --- a/studio/src/motion/ExploreStudios.tsx +++ b/studio/src/motion/ExploreStudios.tsx @@ -5,7 +5,10 @@ import { TemplateIndustryMenu } from './TemplateIndustryMenu'; import { SkillSubmissionLink } from './SkillSubmissionLink'; import { templateContributionUrl } from './templateContribution'; import { MotionIcon } from './icons'; -import { useRef, useState, type ReactNode, type RefObject } from 'react'; +import { useEffect, useRef, useState, type ReactNode, type RefObject } from 'react'; +import { scheduleIdlePreloads } from '../utils/idlePreload'; +import { preloadMotionProjectEditor } from './MotionProjectEditor'; +import { useStudioHost } from './StudioHost'; import { exploreCommunities, findMediaStudio, findTechniqueStudio, type CreativeStudio, type ExploreCommunity, type MediaStudio } from '../../../shared/studio-catalog'; import type { GalleryTemplate, TemplatePage } from '../../../shared/catalog'; import type { ExploreFilters, TemplateCollection } from './workspacePageRouting'; @@ -553,6 +556,11 @@ function ExploreCollection({ community, root, firstPages, ...props }: ExplorePro export function ExploreStudios(props: ExploreProps) { const { t, locale } = useI18n(); const { openLibrary, openSkills } = useWorkspaceChrome(); + const preloadHost = useStudioHost()?.preloadEditorHost; + const onHome = props.active && !props.filters.community && !props.filters.medium; + useEffect(() => { + if (onHome && preloadHost) return scheduleIdlePreloads([preloadMotionProjectEditor, preloadHost]); + }, [onHome, preloadHost]); const root = useRef(null); const [firstPages] = useState(() => new Map()); const community = exploreCommunities.find(item => item.id === props.filters.community); diff --git a/studio/src/motion/MotionApp.tsx b/studio/src/motion/MotionApp.tsx index 4a7aff304..60fde8714 100644 --- a/studio/src/motion/MotionApp.tsx +++ b/studio/src/motion/MotionApp.tsx @@ -109,7 +109,7 @@ function MotionAppContent() { const origin = isolatedPreviewOrigin(); if (origin) previewLease = preparePreviewLease(projectId, origin, controller.signal); preloadMotionProjectEditor(); - host?.preloadEditorHost?.(); + void host?.preloadEditorHost?.().catch(() => {}); // The mounted host retries the import. } return () => controller.abort(); }, [route, attempt, libraryOnly, settingsPage, host]); diff --git a/studio/src/motion/MotionAppShell.tsx b/studio/src/motion/MotionAppShell.tsx index 28680e55f..5136fc425 100644 --- a/studio/src/motion/MotionAppShell.tsx +++ b/studio/src/motion/MotionAppShell.tsx @@ -24,9 +24,7 @@ const LazyMotionEditorHost = lazy(loadMotionEditorHost); const shellHost: StudioHost = { feedback: false, EditorHost: LazyMotionEditorHost, - preloadEditorHost: () => { - void loadMotionEditorHost(); - }, + preloadEditorHost: loadMotionEditorHost, }; /** diff --git a/studio/src/motion/MotionProjectEditor.test.ts b/studio/src/motion/MotionProjectEditor.test.ts index 04d2e21a7..f8cfff066 100644 --- a/studio/src/motion/MotionProjectEditor.test.ts +++ b/studio/src/motion/MotionProjectEditor.test.ts @@ -7,7 +7,9 @@ vi.mock('../App', () => ({ StudioApp: () => null })); it('mounts the resolved editor directly after its speculative import finishes', async () => { expect(getMotionProjectEditor()).not.toBe(StudioApp); - preloadMotionProjectEditor(); + const pending = preloadMotionProjectEditor(); + expect(preloadMotionProjectEditor()).toBe(pending); await vi.dynamicImportSettled(); expect(getMotionProjectEditor()).toBe(StudioApp); + expect(preloadMotionProjectEditor()).toBe(pending); }); diff --git a/studio/src/motion/MotionProjectEditor.ts b/studio/src/motion/MotionProjectEditor.ts index cb488e369..0120da6fe 100644 --- a/studio/src/motion/MotionProjectEditor.ts +++ b/studio/src/motion/MotionProjectEditor.ts @@ -9,15 +9,15 @@ const loadEditor = () => import('../App').then(({ StudioApp }) => { }); let preloaded: Promise> | undefined> | undefined; -export function preloadMotionProjectEditor(): void { - preloaded ??= loadEditor().catch(() => { +export function preloadMotionProjectEditor() { + return preloaded ??= loadEditor().catch(() => { // Let the actual mount retry a speculative chunk request that failed. preloaded = undefined; return undefined; }); } -const LazyEditor = lazy(async () => (await preloaded) ?? loadEditor()); +const LazyEditor = lazy(async () => (await preloadMotionProjectEditor()) ?? loadEditor()); // A finished preload can mount immediately, without a new Suspense fallback. export const getMotionProjectEditor = () => loadedEditor ?? LazyEditor; diff --git a/studio/src/motion/StudioHost.tsx b/studio/src/motion/StudioHost.tsx index 7b0d87aba..2a6eb19f7 100644 --- a/studio/src/motion/StudioHost.tsx +++ b/studio/src/motion/StudioHost.tsx @@ -24,7 +24,7 @@ export interface StudioHost { /** Optional lazy host wrapper mounted around the lazy editor inside ArtifactViewer. */ EditorHost?: ComponentType<{ children: ReactNode }>; /** Optional prefetcher paired with EditorHost to load it in parallel with the editor chunk. */ - preloadEditorHost?: () => void; + preloadEditorHost?: () => Promise; } export const StudioHostContext = createContext(null);