Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(loader): supports passing Response as entry parameter for loadEntry function #2919

Merged
merged 2 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
kuitos marked this conversation as resolved.
Show resolved Hide resolved
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
Loading