perf(studio): warm editor modules while Explore is idle (#1072)

Co-authored-by: lefarcen <935902669@qq.com>
This commit is contained in:
open-design-crew[bot]
2026-11-25 15:01:01 +08:00
committed by GitHub
parent 08d9e5d44b
commit 73b19cb770
6 changed files with 18 additions and 10 deletions

View File

@@ -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<HTMLElement>(null);
const [firstPages] = useState(() => new Map<string, TemplatePage>());
const community = exploreCommunities.find(item => item.id === props.filters.community);

View File

@@ -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]);

View File

@@ -24,9 +24,7 @@ const LazyMotionEditorHost = lazy(loadMotionEditorHost);
const shellHost: StudioHost = {
feedback: false,
EditorHost: LazyMotionEditorHost,
preloadEditorHost: () => {
void loadMotionEditorHost();
},
preloadEditorHost: loadMotionEditorHost,
};
/**

View File

@@ -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);
});

View File

@@ -9,15 +9,15 @@ const loadEditor = () => import('../App').then(({ StudioApp }) => {
});
let preloaded: Promise<Awaited<ReturnType<typeof loadEditor>> | 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;

View File

@@ -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<unknown>;
}
export const StudioHostContext = createContext<StudioHost | null>(null);