Skip to content

Commit

Permalink
Merge branch 'next' into feat/fetch-status
Browse files Browse the repository at this point in the history
  • Loading branch information
kuitos committed Mar 6, 2024
2 parents 55008d9 + 7d77699 commit 1cdaa06
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .changeset/poor-squids-hide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"qiankun": patch
---

fix(qiankun): should remove internal cache of loadMicroApp while loading failed
6 changes: 6 additions & 0 deletions .changeset/red-islands-mate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@qiankunjs/loader": patch
"qiankun": patch
---

feat(loader): supports passing Response as entry parameter for loadEntry function
16 changes: 10 additions & 6 deletions packages/loader/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import type {
} from '@qiankunjs/shared';
import { Deferred, prepareDeferredQueue, QiankunError } from '@qiankunjs/shared';
import { createTagTransformStream } from './TagTransformStream';
import { isUrlHasOwnProtocol } from './utils';
import WritableDOMStream from './writable-dom';

type HTMLEntry = string;
Expand Down Expand Up @@ -42,10 +41,15 @@ const isDeferScript = (script: HTMLScriptElement): boolean => {
* @param container
* @param opts
*/
export async function loadEntry<T>(entry: Entry, container: HTMLElement, opts: LoaderOpts): Promise<T | void> {
export async function loadEntry<T>(
entry: Entry | { url: string; res: Response },
container: HTMLElement,
opts: LoaderOpts,
): Promise<T | void> {
const { fetch, streamTransformer, sandbox, nodeTransformer } = opts;

const res = isUrlHasOwnProtocol(entry) ? await fetch(entry) : new Response(entry, { status: 200, statusText: 'OK' });
const entryUrl = typeof entry === 'string' ? entry : entry.url;
const res = typeof entry === 'string' ? await fetch(entry) : entry.res;
if (res.body) {
let foundEntryScript = false;
const entryScriptLoadedDeferred = new Deferred<T | void>();
Expand Down Expand Up @@ -120,7 +124,7 @@ export async function loadEntry<T>(entry: Entry, container: HTMLElement, opts: L
if (isEntryScript(script)) {
if (foundEntryScript) {
throw new QiankunError(
`You should not set multiply entry script in one entry html, but ${entry} has at least 2 entry scripts`,
`You should not include more than 1 entry scripts in a single HTML entry ${entryUrl} !`,
);
}

Expand All @@ -139,7 +143,7 @@ export async function loadEntry<T>(entry: Entry, container: HTMLElement, opts: L
} else {
entryScriptLoadedDeferred.reject(
new QiankunError(
`entry ${entry} load failed as entry script ${script.dataset.src || script.src} load failed}`,
`Entry ${entryUrl} load failed as entry script ${script.dataset.src || script.src} load failed}`,
),
);
}
Expand Down Expand Up @@ -176,5 +180,5 @@ export async function loadEntry<T>(entry: Entry, container: HTMLElement, opts: L
return entryScriptLoadedDeferred.promise;
}

throw new QiankunError(`entry ${entry} response body is empty!`);
throw new QiankunError(`The response body of entry ${entryUrl} is empty!`);
}
6 changes: 0 additions & 6 deletions packages/loader/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
export function getEntireUrl(uri: string, baseURI: string): string {
const publicPath = new URL(baseURI, window.location.href);
const entireUrl = new URL(uri, publicPath.toString());
return entireUrl.toString();
}

export function isUrlHasOwnProtocol(url: string): boolean {
const protocols = ['http://', 'https://', '//', 'blob:', 'data:'];
return protocols.some((protocol) => url.startsWith(protocol));
Expand Down
11 changes: 10 additions & 1 deletion packages/qiankun/src/apis/loadMicroApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,21 @@ export function loadMicroApp<T extends ObjectType>(

const parcelConfigObjectGetterPromise = loadApp(app, userConfiguration, lifeCycles);

let parcelConfigObjectGetter: ParcelConfigObjectGetter | undefined;

if (containerXPath) {
const appContainerXPathKey = getContainerXPathKey(containerXPath);
appConfigPromiseGetterMap.set(appContainerXPathKey, parcelConfigObjectGetterPromise);
try {
parcelConfigObjectGetter = await parcelConfigObjectGetterPromise;
} catch (e) {
appConfigPromiseGetterMap.delete(appContainerXPathKey);
throw e;
}
}

return (await parcelConfigObjectGetterPromise)(container);
parcelConfigObjectGetter = parcelConfigObjectGetter || (await parcelConfigObjectGetterPromise);
return parcelConfigObjectGetter(container);
};

if (!started) {
Expand Down
6 changes: 5 additions & 1 deletion packages/qiankun/src/core/loadApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,11 @@ export default async function loadApp<T extends ObjectType>(
initContainer(mountContainer, appName, { sandboxCfg: sandbox, mountTimes, instanceId });
// html scripts should be removed to avoid repeatedly execute
const htmlString = await getPureHTMLStringWithoutScripts(entry, enhancedFetch);
await loadEntry(htmlString, mountContainer, containerOpts);
await loadEntry(
{ url: entry, res: new Response(htmlString, { status: 200, statusText: 'OK' }) },
mountContainer,
containerOpts,
);
}
},
async () => {
Expand Down

0 comments on commit 1cdaa06

Please sign in to comment.