diff --git a/.changeset/red-islands-mate.md b/.changeset/red-islands-mate.md new file mode 100644 index 000000000..097e531af --- /dev/null +++ b/.changeset/red-islands-mate.md @@ -0,0 +1,6 @@ +--- +"@qiankunjs/loader": patch +"qiankun": patch +--- + +feat(loader): supports passing Response as entry parameter for loadEntry function diff --git a/packages/loader/src/index.ts b/packages/loader/src/index.ts index bc6157e54..f3cfc466f 100644 --- a/packages/loader/src/index.ts +++ b/packages/loader/src/index.ts @@ -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; @@ -42,10 +41,15 @@ const isDeferScript = (script: HTMLScriptElement): boolean => { * @param container * @param opts */ -export async function loadEntry(entry: Entry, container: HTMLElement, opts: LoaderOpts): Promise { +export async function loadEntry( + entry: Entry | { url: string; res: Response }, + container: HTMLElement, + opts: LoaderOpts, +): Promise { 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(); @@ -120,7 +124,7 @@ export async function loadEntry(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} !`, ); } @@ -139,7 +143,7 @@ export async function loadEntry(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}`, ), ); } @@ -176,5 +180,5 @@ export async function loadEntry(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!`); } diff --git a/packages/loader/src/utils.ts b/packages/loader/src/utils.ts index cd5821f25..f2eae1462 100644 --- a/packages/loader/src/utils.ts +++ b/packages/loader/src/utils.ts @@ -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)); diff --git a/packages/qiankun/src/core/loadApp.ts b/packages/qiankun/src/core/loadApp.ts index d25dc764d..aa635cfc6 100644 --- a/packages/qiankun/src/core/loadApp.ts +++ b/packages/qiankun/src/core/loadApp.ts @@ -140,7 +140,11 @@ export default async function loadApp( 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 () => {