Skip to content

fix: javascript 资源加载完成时,如果 microapp 已经卸载,则不应该执行该文件 #1607

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/create_app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export default class CreateApp implements AppInterface {
public isPrefetch: boolean
public isPrerender: boolean
public prefetchLevel?: number
public mountIdentifier?: symbol
public fiber = false
public routerMode: string
public attrs?: Record<string, string>
Expand Down Expand Up @@ -287,6 +288,7 @@ export default class CreateApp implements AppInterface {
this.routerMode = routerMode

const dispatchBeforeMount = () => {
this.mountIdentifier = Symbol('mountIdentifier')
dispatchLifecyclesEvent(
this.container,
this.name,
Expand Down Expand Up @@ -493,6 +495,8 @@ export default class CreateApp implements AppInterface {
unmountcb?: CallableFunction,
umdHookUnmountResult?: unknown,
): void {
this.mountIdentifier = undefined

// dispatch state event to micro app
dispatchCustomEventToMicroApp(this, 'statechange', {
appState: appStates.UNMOUNT
Expand Down
20 changes: 18 additions & 2 deletions src/source/scripts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,11 +320,21 @@ export function fetchScriptsFromHtml (

const fiberScriptTasks: fiberTasks = app.isPrefetch || app.fiber ? [] : null

const startIdentifier = app.mountIdentifier

if (fetchScriptPromise.length) {
promiseStream<string>(fetchScriptPromise, (res: {data: string, index: number}) => {
const address = fetchScriptPromiseInfo[res.index][0]
const scriptInfo = fetchScriptPromiseInfo[res.index][1]
const currIdentifier = app.mountIdentifier
if (startIdentifier !== currIdentifier) {
scriptInfo.code = res.data
// app has been unmounted, old sandbox is not valid anymore. should not execute script
throw new Error('app has been unmounted, abort loading script')
}
injectFiberTask(fiberScriptTasks, () => fetchScriptSuccess(
fetchScriptPromiseInfo[res.index][0],
fetchScriptPromiseInfo[res.index][1],
address,
scriptInfo,
res.data,
app,
))
Expand Down Expand Up @@ -578,8 +588,14 @@ export function runDynamicRemoteScript (
if (scriptInfo.code || isTypeModule(app, scriptInfo)) {
defer(runDynamicScript)
} else {
const startIdentifier = app.mountIdentifier
fetchSource(address, app.name).then((code: string) => {
const currIdentifier = app.mountIdentifier
scriptInfo.code = code
if (startIdentifier !== currIdentifier) {
// app has been unmounted, old sandbox is not valid anymore. should not execute script
throw new Error('app has been unmounted, abort loading script')
}
runDynamicScript()
}).catch((err) => {
logError(err, app.name)
Expand Down
1 change: 1 addition & 0 deletions typings/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ declare module '@micro-app/types' {
isPrerender: boolean
isReloading?: boolean
prefetchLevel?: number
mountIdentifier?: symbol // used to identify if the app has been unmounted between async calls
// defaultPage: string // default page when mount
// baseroute: string // route prefix, default is ''
// hiddenRouter: boolean // hide router info of child from browser url
Expand Down