From 538869f5b1ea0f257e4ae607ce0eeae33cf6af6b Mon Sep 17 00:00:00 2001 From: Kuitos Date: Mon, 26 Apr 2021 14:34:05 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20manual=20invoke=20single-spa=20s?= =?UTF-8?q?tart=20function=20for=20parcels=20and=20set=20urlRerouteOnly=20?= =?UTF-8?q?true=20as=20default=20(#1414)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 2 +- src/apis.ts | 21 ++++++++++++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 6d8cb0c..b48aaac 100644 --- a/package.json +++ b/package.json @@ -65,7 +65,7 @@ "@babel/runtime": "^7.10.5", "import-html-entry": "^1.9.0", "lodash": "^4.17.11", - "single-spa": "5.8.1", + "single-spa": "^5.9.2", "tslib": "^1.10.0" }, "devDependencies": { diff --git a/src/apis.ts b/src/apis.ts index 1b9ce6b..7d617fc 100644 --- a/src/apis.ts +++ b/src/apis.ts @@ -12,6 +12,10 @@ let microApps: Array>> = []; // eslint-disable-next-line import/no-mutable-exports export let frameworkConfiguration: FrameworkConfiguration = {}; + +let started = false; +const defaultUrlRerouteOnly = true; + const frameworkStartedDefer = new Deferred(); export function registerMicroApps( @@ -111,12 +115,26 @@ export function loadMicroApp( return (await parcelConfigObjectGetterPromise)(container); }; + if (!started) { + // We need to invoke start method of single-spa as the popstate event should be dispatched while the main app calling pushState/replaceState automatically, + // but in single-spa it will check the start status before it dispatch popstate + // see https://github.com/single-spa/single-spa/blob/f28b5963be1484583a072c8145ac0b5a28d91235/src/navigation/navigation-events.js#L101 + // ref https://github.com/umijs/qiankun/pull/1071 + startSingleSpa({ urlRerouteOnly: frameworkConfiguration.urlRerouteOnly ?? defaultUrlRerouteOnly }); + } + return mountRootParcel(memorizedLoadingFn, { domElement: document.createElement('div'), ...props }); } export function start(opts: FrameworkConfiguration = {}) { frameworkConfiguration = { prefetch: true, singular: true, sandbox: true, ...opts }; - const { prefetch, sandbox, singular, urlRerouteOnly, ...importEntryOpts } = frameworkConfiguration; + const { + prefetch, + sandbox, + singular, + urlRerouteOnly = defaultUrlRerouteOnly, + ...importEntryOpts + } = frameworkConfiguration; if (prefetch) { doPrefetchStrategy(microApps, prefetch, importEntryOpts); @@ -135,6 +153,7 @@ export function start(opts: FrameworkConfiguration = {}) { } startSingleSpa({ urlRerouteOnly }); + started = true; frameworkStartedDefer.resolve(); }