Skip to content

Commit

Permalink
feat(loader): supports passing Response as entry parameter for loadEn…
Browse files Browse the repository at this point in the history
…try function (#2919)
  • Loading branch information
kuitos authored Mar 6, 2024
1 parent e28c729 commit 7d77699
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 13 deletions.
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
6 changes: 5 additions & 1 deletion packages/qiankun/src/core/loadApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,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, fetchWithLruCache);
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 7d77699

Please sign in to comment.