startApp
Type:
FunctionParameters:
startOptionsReturns:
Promise<Function | void>
type lifecycle = (appWindow: Window) => any;
type loadErrorHandler = (url: string, e: Error) => any;
type startOptions = {
/** Unique identifier for the sub-app */
name: string;
/** Entry URL of the sub-app */
url: string;
/** Pre-fetched HTML string; if provided, Wujie will not fetch from url */
html?: string;
/** Target container element or selector */
el: HTMLElement | string;
/** Custom loading element displayed while loading */
loading?: HTMLElement;
/** Route sync toggle; when false, reload loses sub-app route while back/forward still works */
sync?: boolean;
/** Short path prefix map used during route sync */
prefix?: { [key: string]: string };
/** Enable Keep-Alive mode so sub-app state is preserved */
alive?: boolean;
/** Data and callbacks injected into the sub-app */
props?: { [key: string]: any };
/** Execute sub-app JS scripts in fiber mode */
fiber?: boolean;
/** Force iframe degradation mode */
degrade?: boolean;
/** Custom attributes for the JS sandbox iframe */
attrs?: { [key: string]: any };
/** Custom attributes for the fallback rendering iframe in degrade mode */
degradeAttrs?: { [key: string]: any };
/** Global code replacement hook */
replace?: (codeText: string) => string;
/** Custom fetch for static assets and fetch API calls */
fetch?: (input: RequestInfo, init?: RequestInit) => Promise<Response>;
/** Events listened on the sub-app window */
iframeAddEventListeners?: Array<string>;
/** on* event properties forwarded to the sub-app iframe */
iframeOnEvents?: Array<string>;
/** Sub-app plugins */
plugins?: Array<plugin>;
/** Sub-app lifecycle hooks */
beforeLoad?: lifecycle;
/** Only called when the sub-app has implemented lifecycle adaptation */
beforeMount?: lifecycle;
afterMount?: lifecycle;
beforeUnmount?: lifecycle;
afterUnmount?: lifecycle;
/** Only called for Keep-Alive sub-apps */
activated?: lifecycle;
deactivated?: lifecycle;
/** Called when a sub-app asset fails to load */
loadError?: loadErrorHandler;
};
Details: Starts and mounts a sub-app, asynchronously returning a
destroyfunction that tears down the sub-app. Callingdestroymanually is generally discouraged unless you are certain the sub-app will not be reopened.Warning
- In most cases, you should not manually call
destroyunless the main app will never open this sub-app again; destroying it causes a cold-start blank screen on the next visit. - The five options
name,replace,fetch,alive, anddegrademust remain strictly identical betweenpreloadAppandstartApp, or rendering issues may occur.
- In most cases, you should not manually call
name
Type:
stringDetails: Unique identifier for the sub-app.
Tip
If multiple menu entries in the main app open different pages of the same sub-app, use the same
nameacross them so they share a single sandbox instance.
url
Type:
stringDetails: Entry URL of the sub-app.
- In Singleton Mode, changing
urlnavigates the sub-app to the new route. - In Keep-Alive Mode, changing
urlhas no effect; use Communication to trigger route changes instead. - In Rebuild Mode, changing
urlnavigates to the new route — unless Route Sync is active and a synced route parameter is already present in the main app's URL, in which case the synced parameter takes precedence when rebuilding.
- In Singleton Mode, changing
html
Type:
stringDetails: Raw HTML string for the sub-app. When provided, Wujie uses it directly instead of fetching HTML from
url.
el
Type:
HTMLElement | stringDetails: Container element (or CSS selector) where the sub-app mounts. Give the container explicit dimensions to avoid layout issues. Wujie also adds the
wujie_iframeclass to the Web Component element for custom styling.
loading
Type:
HTMLElementDetails: Custom loading indicator element. To hide the default spinner completely, pass an empty element such as
document.createElement("span").
sync
Default:
falseType:
booleanDetails: Enables route synchronization. Wujie stores the sub-app's current path in the main app's URL query string under the key matching the sub-app's
name, so refreshing the browser or sharing the URL preserves the sub-app's route.Warning
Route sync reads from the main app's URL only once during initial startup or browser reload; afterward it syncs one-way from the sub-app to the main app URL. When
syncisfalse, browser back/forward still works for sub-apps, but refreshing the page loses the sub-app's route.
prefix
Type:
{ [key: string]: string }Details: Short-path alias map used when route sync is enabled to keep long sub-app URLs concise. See Short Path for examples.
alive
Default:
falseType:
booleanDetails:
Enables Keep-Alive Mode. Neither the sub-app's JS
instancenor its Web Component is destroyed when switching away — state and routing stay intact, and switching is just hot-swapping the Web Component DOM.Choose Keep-Alive Mode if you want instant, flicker-free switching without modifying the sub-app's lifecycle hooks.
If multiple main-app menus point to different pages of the same sub-app, Keep-Alive Mode is not recommended because
startAppcannot change the route of an already alive sub-app. Use Singleton Mode instead.Tip
Combining pre-execution with Keep-Alive Mode achieves an SSR-like experience where both data fetching and rendering complete ahead of time for instant opening.
props
Type:
{ [key: string]: any }Details: Data and callback functions injected into the sub-app (accessible via
window.$wujie.props).
fiber
Default:
trueType:
booleanDetails:
Controls JavaScript execution scheduling. Because running sub-app scripts can block the main app's rendering thread, setting
fiber: truewraps each script execution insiderequestIdleCallback(inspired by React Fiber) so the browser can stay responsive between scripts. Granularity is per JS file — splitting oversized bundles maximizes the benefit of fiber mode.Tip
- For a sub-app that must render immediately when the main app opens, set
fiber: falseto speed up initial load. - For all other scenarios, keep the default (
true).
- For a sub-app that must render immediately when the main app opens, set
degrade
Default:
falseType:
booleanDetails:
Forces iframe degradation mode. Wujie relies on
Proxyand Web Components and automatically falls back to aniframe+Object.definePropertyimplementation on older browsers (theoretically down to IE 9). Setdegrade: trueto explicitly opt into iframe rendering even on modern browsers.Warning
In Degrade Mode, modals and popups are confined inside the iframe and cannot cover the full main-app viewport.
attrs
Type:
{ [key: string]: any }Details: Custom HTML attributes applied to the sub-app's JS sandbox
iframe.
replace
Type:
(codeText: string) => stringDetails: Global runtime code replacement hook.
Tip
Useful when you cannot modify a sub-app's repository directly.
replaceis applied to the sub-app'shtml,js, andcsstext at runtime.
fetch
Type:
(input: RequestInfo, init?: RequestInit) => Promise<Response>Details: Custom
fetchimplementation. When specified, both static asset requests andfetch-based API requests from the sub-app go through this function.Tip
To include cookies in cross-origin requests, pass a custom
fetch:(url, options) => window.fetch(url, { ...options, credentials: "include" }).
iframeAddEventListeners
Type:
Array<string>Details: Event names that should be listened on the sub-app
windowdirectly.
iframeOnEvents
Type:
Array<string>Details:
on*event properties that should be bound on the sub-appiframewindow.
plugins
- Type:
Array<plugin>
interface ScriptObjectLoader {
/** External script URL; omit for inline scripts */
src?: string;
/** Whether the script is an ES module */
module?: boolean;
/** Whether the script executes asynchronously */
async?: boolean;
/** Whether to set the crossorigin attribute */
crossorigin?: boolean;
/** Value of the crossorigin attribute */
crossoriginType?: "anonymous" | "use-credentials" | "";
/** Inline script source code */
content?: string;
/** Callback executed with the sub-app window */
callback?: (appWindow: Window) => any;
}
interface StyleObjectLoader {
/** External stylesheet URL; omit for inline styles */
src?: string;
/** Inline CSS text */
content?: string;
}
type eventListenerHook = (
iframeWindow: Window,
type: string,
handler: EventListenerOrEventListenerObject,
options?: boolean | AddEventListenerOptions
) => void;
interface plugin {
/** Transform sub-app HTML */
htmlLoader?: (code: string) => string;
/** JS files to exclude from execution */
jsExcludes?: Array<string | RegExp>;
/** JS files loaded natively without proxying */
jsIgnores?: Array<string | RegExp>;
/** Scripts/callbacks executed before sub-app JS */
jsBeforeLoaders?: Array<ScriptObjectLoader>;
/** Transform sub-app JS source code */
jsLoader?: (code: string, url: string, base: string) => string;
/** Scripts/callbacks executed after sub-app JS */
jsAfterLoaders?: Array<ScriptObjectLoader>;
/** CSS files to exclude from loading */
cssExcludes?: Array<string | RegExp>;
/** CSS files loaded natively without proxying */
cssIgnores?: Array<string | RegExp>;
/** Styles inserted before sub-app CSS */
cssBeforeLoaders?: Array<StyleObjectLoader>;
/** Transform sub-app CSS text */
cssLoader?: (code: string, url: string, base: string) => string;
/** Styles inserted after sub-app CSS */
cssAfterLoaders?: Array<StyleObjectLoader>;
/** Hook for sub-app window.addEventListener */
windowAddEventListenerHook?: eventListenerHook;
/** Hook for sub-app window.removeEventListener */
windowRemoveEventListenerHook?: eventListenerHook;
/** Hook for sub-app document.addEventListener */
documentAddEventListenerHook?: eventListenerHook;
/** Hook for sub-app document.removeEventListener */
documentRemoveEventListenerHook?: eventListenerHook;
/** Hook after inserting elements into sub-app body or head */
appendOrInsertElementHook?: <T extends Node>(element: T, iframeWindow: Window) => void;
/** Hook after patching created elements in the sub-app */
patchElementHook?: <T extends Node>(element: T, iframeWindow: Window) => void;
/** Customize sub-app window properties */
windowPropertyOverride?: (iframeWindow: Window) => void;
/** Customize sub-app document properties */
documentPropertyOverride?: (iframeWindow: Window) => void;
}
- Details: Plugins for transforming sub-app assets and intercepting runtime behaviors. See the Plugin System guide for examples.
beforeLoad
Type:
(appWindow: Window) => anyDetails: Lifecycle hook called before the sub-app begins loading.
beforeMount
Type:
(appWindow: Window) => anyDetails: Lifecycle hook called right before the sub-app mounts.
afterMount
Type:
(appWindow: Window) => anyDetails: Lifecycle hook called right after the sub-app mounts.
beforeUnmount
Type:
(appWindow: Window) => anyDetails: Lifecycle hook called right before the sub-app unmounts.
afterUnmount
Type:
(appWindow: Window) => anyDetails: Lifecycle hook called right after the sub-app unmounts.
activated
Type:
(appWindow: Window) => anyDetails: Lifecycle hook triggered when a Keep-Alive sub-app is activated.
deactivated
Type:
(appWindow: Window) => anyDetails: Lifecycle hook triggered when a Keep-Alive sub-app is deactivated.
loadError
Type:
(url: string, e: Error) => anyDetails: Lifecycle hook triggered when a sub-app asset fails to load.
Note
If the sub-app has not implemented Lifecycle Adaptation, beforeMount, afterMount, beforeUnmount, and afterUnmount will not be invoked. For non-keep-alive sub-apps, activated and deactivated will not be invoked.
