Skip to content

Commit 7d77699

Browse files
authored
feat(loader): supports passing Response as entry parameter for loadEntry function (#2919)
1 parent e28c729 commit 7d77699

File tree

4 files changed

+21
-13
lines changed

4 files changed

+21
-13
lines changed

.changeset/red-islands-mate.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@qiankunjs/loader": patch
3+
"qiankun": patch
4+
---
5+
6+
feat(loader): supports passing Response as entry parameter for loadEntry function

packages/loader/src/index.ts

+10-6
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import type {
88
} from '@qiankunjs/shared';
99
import { Deferred, prepareDeferredQueue, QiankunError } from '@qiankunjs/shared';
1010
import { createTagTransformStream } from './TagTransformStream';
11-
import { isUrlHasOwnProtocol } from './utils';
1211
import WritableDOMStream from './writable-dom';
1312

1413
type HTMLEntry = string;
@@ -42,10 +41,15 @@ const isDeferScript = (script: HTMLScriptElement): boolean => {
4241
* @param container
4342
* @param opts
4443
*/
45-
export async function loadEntry<T>(entry: Entry, container: HTMLElement, opts: LoaderOpts): Promise<T | void> {
44+
export async function loadEntry<T>(
45+
entry: Entry | { url: string; res: Response },
46+
container: HTMLElement,
47+
opts: LoaderOpts,
48+
): Promise<T | void> {
4649
const { fetch, streamTransformer, sandbox, nodeTransformer } = opts;
4750

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

@@ -139,7 +143,7 @@ export async function loadEntry<T>(entry: Entry, container: HTMLElement, opts: L
139143
} else {
140144
entryScriptLoadedDeferred.reject(
141145
new QiankunError(
142-
`entry ${entry} load failed as entry script ${script.dataset.src || script.src} load failed}`,
146+
`Entry ${entryUrl} load failed as entry script ${script.dataset.src || script.src} load failed}`,
143147
),
144148
);
145149
}
@@ -176,5 +180,5 @@ export async function loadEntry<T>(entry: Entry, container: HTMLElement, opts: L
176180
return entryScriptLoadedDeferred.promise;
177181
}
178182

179-
throw new QiankunError(`entry ${entry} response body is empty!`);
183+
throw new QiankunError(`The response body of entry ${entryUrl} is empty!`);
180184
}

packages/loader/src/utils.ts

-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
export function getEntireUrl(uri: string, baseURI: string): string {
2-
const publicPath = new URL(baseURI, window.location.href);
3-
const entireUrl = new URL(uri, publicPath.toString());
4-
return entireUrl.toString();
5-
}
6-
71
export function isUrlHasOwnProtocol(url: string): boolean {
82
const protocols = ['http://', 'https://', '//', 'blob:', 'data:'];
93
return protocols.some((protocol) => url.startsWith(protocol));

packages/qiankun/src/core/loadApp.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,11 @@ export default async function loadApp<T extends ObjectType>(
140140
initContainer(mountContainer, appName, { sandboxCfg: sandbox, mountTimes, instanceId });
141141
// html scripts should be removed to avoid repeatedly execute
142142
const htmlString = await getPureHTMLStringWithoutScripts(entry, fetchWithLruCache);
143-
await loadEntry(htmlString, mountContainer, containerOpts);
143+
await loadEntry(
144+
{ url: entry, res: new Response(htmlString, { status: 200, statusText: 'OK' }) },
145+
mountContainer,
146+
containerOpts,
147+
);
144148
}
145149
},
146150
async () => {

0 commit comments

Comments
 (0)