10 KiB
Studio Routing and Lazy-Loading Architecture
This guide defines how OpenMotion Studio organizes workspace pages, semantic path URLs, React Router integration, code splitting, SPA fallback prefixes, and shell dependency boundaries. Follow these rules whenever adding, moving, or modifying a Studio page.
Directory ownership
All workspace route registration, Data Router wiring, and lazy surface management live under studio/src/router/ and shared/spa-routes.ts:
shared/spa-routes.ts— Shared allowlist of Studio SPA route prefixes (SPA_ROUTE_PREFIXES,STANDALONE_SPA_PREFIXES) and reserved backend/cloud prefixes (RESERVED_BACKEND_PREFIXES). Consumed byeditor-server.ts,server.ts,studio/vite.dev.config.ts,phase1.ts,scripts/check-routing.ts, and Studio routing helpers so local HTTP fallback and client routes never drift.studio/src/router/createAppRouter.tsx— React Router Data Router factory (createAppRouteObjects,createAppRouter,StudioRouterProvider) mounted at the root ofstudio/src/main.tsx. ConnectsworkspaceHistoryto the active router vialocation.stateand router-managed history indices (history.state.idx), withcreateMemoryRoutersupport for deterministic unit tests and StrictMode-safe lifecycle handling.studio/src/router/LegacyQueryRedirect.tsx— Mounted on the root/index route to transparentlyreplacelegacy/?page=..., bare/, and/?project=...#project/...bookmarks onto their canonical semantic paths while preserving orthogonal query parameters (embed,hostChrome,templates,template,project,session,tracking_context) and#project/...hashes without adding an extra history entry.studio/src/router/lazyPages.ts— Single source of truth for lazy-loaded workspace page and overlay components (lazyWithChunkReload),LAZY_PAGE_MODULE_PATHS, and intent-based prefetch helpers (preloadWorkspacePage,preloadStudioRoute).studio/src/router/routeRegistry.ts— Declarative route table (workspaceRouteRegistry) defining route IDs, match precedence,keepAlivepolicy, standalone flags, tracking page names, and arrival tracking helpers.studio/src/router/KeepAliveOutlet.tsx—useKeepAliveVisitedandKeepAliveSlotdriven directly byworkspaceRouteRegistry.keepAlive. KeepAlive routes stay mounted once visited (hidden+aria-hidden+inert); all other routes unmount immediately when inactive.studio/src/router/useWorkspaceBlocker.ts— React RouteruseBlocker-powered navigation guard for browser history traversal (POP) across projects or leaving a busy template library. Preserves Forward/Back history cleanly viablocker.proceed()/blocker.reset().studio/src/router/RouteViewport.tsx— Renders the active workspace surface and KeepAlive slots inside a single always-mounted<Suspense>boundary (with per-routeSettingsSurfaceBoundaryerror isolation). Keeping<Suspense>always mounted allowsWorkspaceFrame'suseTransitionto hold the currently visible page painted while the next lazy chunk resolves, eliminating blank screens and skeleton flashes during in-app navigation.studio/src/router/OverlayLayer.tsx— Renders cross-cutting overlays (template library dialog and standalone card preview) inside an always-mounted<Suspense fallback={null}>without coupling them intoWorkspaceFrame.tsx.
studio/src/motion/WorkspaceFrame.tsx is strictly the App Shell (header, project sidebar, chrome context, toasts, and keyboard shortcuts). Never import feature pages statically into WorkspaceFrame.tsx or studio/src/main.tsx.
Canonical URL contract
Every workspace destination has a canonical semantic pathname. Do not add new /?page=... query destinations.
| Surface | Canonical URL | Notes |
|---|---|---|
| Explore | /explore (filters in ?community=...&medium=...) |
Bare / redirects here via LegacyQueryRedirect; /explore/:community is also accepted on direct load. |
| Motion Studio | /motion |
Motion Studio landing & template catalog. |
| Focused media generator | /create/:scene (optional ?model=...&skill=...&cinemaTemplate=...) |
Unified image/video landing generator (text-to-image, image-edit, text-to-video, image-to-video, etc.). |
| Cinema Studio | /cinema · /cinema/:scene |
Cinema Studio landing and scene generator. |
| Design Studio | /design/:scene · /design/projects/:id |
HTML Design Studio scenes (prototype, deck, document, web-clone, projects) and project editor. Legacy Design image links rewrite to /create/:scene. |
| Sound Studio & Audio tab | /sound · /sound/:tab · /audio/:tab |
/sound opens Sound Studio home; /sound/:tab opens Sound Studio workbench; /audio/:tab opens the top-level Audio tab. |
| Specialty studios | /studios/:studioId · /studios/:studioId/:scene |
Covers shorts, marketing, explainer, 3d, and game (with optional ?skill=... or ?game=...). |
| Workflows / Scenarios | /scenarios · /scenarios/:id |
Scenario hub (?category=...) and workspace (?run=...&step=...&mode=agent). |
| Skills library | /skills · /skills/:id |
Optional ?category=.... |
| Models & Model Hubs | /models/:id · /model-hub/:id |
Provider-qualified model IDs are URL-encoded in :id. |
| Workspace assets | /assets · /assets/:id |
Optional ?version=... on /assets/:id. |
| Brand Kit | /brand-kit · /brand-kit/:id |
Official collection, mine, new, or a saved kit ID. |
| Settings center | /settings/:section (including /settings/mcp) |
Optional ?provider=... on /settings/providers and ?entry_from=... while inside Settings. |
| Standalone MCP setup | /mcp-setup · /mcp-setup/:client |
Optional ?host=...&setup=manual&example=.... Uses /mcp-setup because /mcp is reserved for the cloud Streamable HTTP MCP protocol endpoint. |
| Motion Editor | /editor/:projectId?session=...#project/:projectId?... · /editor |
Preserves #project/<id>?v=&t=&tab=... synchronized with the upstream timeline player. |
Orthogonal query parameters and legacy compatibility
- Cross-page overlays and host embeds remain orthogonal query parameters that compose cleanly on top of any canonical path:
?templates=1,?collection=...,?view=creators,?creator=...,?template=...,?embed=1,?hostChrome=1,?tracking_context=.... Root template/embed links (/?templates=1&embed=1,/?hostChrome=1) intentionally stay at/without redirecting. - Legacy bookmarks (
/?page=..., bare/,/?project=...#project/...,/#project/...,/?session=...) are transparently redirected in place viaLegacyQueryRedirect.tsx(replace), preserving all orthogonal query parameters and#project/...hashes.
Rules for adding or modifying a page
- Register lazily via
lazyWithChunkReload. Add every new workspace page tostudio/src/router/lazyPages.ts(includingLAZY_PAGE_MODULE_PATHS) and mount it throughRouteViewport.tsx(orOverlayLayer.tsxfor cross-page overlays). Do not statically import page modules fromWorkspaceFrame.tsx,ApplicationChrome.tsx, orWorkspaceBreadcrumb.tsx. - Keep route metadata, SPA prefixes, and Data Router leaves in sync. When adding a top-level route, update
studio/src/router/routeRegistry.ts,studio/src/router/createAppRouter.tsx, andshared/spa-routes.tstogether, and emit semantic paths fromworkspacePageRouting.tsinstead of introducing new?page=parameters. - Extract lightweight metadata before sharing across shell and pages. If the shell (breadcrumbs, header menus, search) needs labels or constants from a lazy page, place those pure constants in a tiny metadata module (for example
studio/src/motion/settingsMeta.ts) instead of importing the page component file. - Keep the shell free of heavy editor parsers and raw locale JSON. Never import
@hyperframes/parsers(barrel entry),studio/src/utils/sourcePatcher.ts,studio/src/utils/studioHelpers.ts,studio/src/i18n/*.json, orstudio/src/i18n/sourceCatalog.tsfrom shell-reachable modules. Bothscripts/check-routing.ts(run bynpm run check) andstudio/src/shellBoundary.test.tsenforce this boundary in CI and report the exact offending import chain. - KeepAlive allowlist. Only
editor,explore, andassetsstay mounted once visited (hidden+aria-hidden+inert) viaKeepAliveSlot. All other studios unmount when navigated away; preserve user drafts in lightweight in-memory maps (such asstudioDrafts) rather than expanding KeepAlive without a clear UX requirement. - Reserved backend and cloud prefixes. Never claim backend or cloud protocol prefixes in
SPA_ROUTE_PREFIXESor client routes (scripts/check-routing.tsandtests/spa-routes.test.tsfail CI on any exact, parent, or child prefix collision):/api,/motion-api,/design-api/mcp(reserved for cloud Streamable HTTP MCP endpoint; use/mcp-setupfor frontend MCP pages)/mcp-ui,/connect,/sessions/editor-assets,/guide-assets,/design-system,/web/.well-known,/__hyperframes_config
- Root-relative asset paths.
studio/vite.config.tspinsbase: '/'. Keep all HTML, CSS, font, and fetch URLs root-relative (/...) rather than relative (./...) so nested path routes resolve assets identically. - Shared vs. route-scoped stylesheets. Cross-page workspace host stylesheets (
studio/src/motion/skills.css,sceneDiscovery.css,assets.css) are imported eagerly instudio/src/main.tsxahead ofstudio.cssandapplication.cssso every lazy route gets identical layout and cascade order on cold direct load. Keep page-specific CSS strictly prefixed to that page and import it in the page module itself.
Verification checklist
When touching Studio routes, SPA fallback prefixes, or shell imports:
- Run
npm run check(executesscripts/check-routing.tsandscripts/check-i18n.ts),npm run typecheck:studio,npm run typecheck:backend,npm run test:studio, andnpm run build:studio. - Run backend SPA fallback & handoff tests:
node --experimental-strip-types --test tests/spa-routes.test.ts tests/server.test.ts tests/preview-handoff.test.ts. - Confirm
studio/src/shellBoundary.test.tsandstudio/src/router/appRouter.test.tsxpass without weakening any boundary assertions. - Inspect
studio/build/editor-assets/to verify new pages emit their own async chunks rather than expandingindex-*.js.