Skip to content

fix(native-federation-runtime) runtime crash with non-JS routing (like static HTML apps or SSR apps) #693

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
45 changes: 26 additions & 19 deletions libs/native-federation-runtime/src/lib/init-federation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import { appendImportMap } from './utils/add-import-map';
import {
FederationInfo,
InitFederationOptions,
ProcessRemoteInfoOptions,
} from './model/federation-info';
import { setOptions } from './utils/set-options';

/**
* Initialize the federation runtime
Expand All @@ -21,25 +21,30 @@ import {
*/
export async function initFederation(
remotesOrManifestUrl: Record<string, string> | string = {},
options?: InitFederationOptions
optionsOverride?: Partial<InitFederationOptions>
): Promise<ImportMap> {
const cacheOption = options?.cacheTag ? `?t=${options.cacheTag}` : '';
const remotes =
typeof remotesOrManifestUrl === 'string'
? await loadManifest(remotesOrManifestUrl + cacheOption)
: remotesOrManifestUrl;

const url = './remoteEntry.json' + cacheOption;
const hostInfo = await loadFederationInfo(url);
const hostImportMap = await processHostInfo(hostInfo);
const remotesImportMap = await processRemoteInfos(remotes, {
throwIfRemoteNotFound: false,
...options,
});

const importMap = mergeImportMaps(hostImportMap, remotesImportMap);
appendImportMap(importMap);
const options = setOptions(optionsOverride);

const cacheOption = options.cacheTag ? `?t=${options.cacheTag}` : '';

const remotesPromise = typeof remotesOrManifestUrl === 'string'
? loadManifest(remotesOrManifestUrl + cacheOption)
: Promise.resolve(remotesOrManifestUrl);

const remotes = await remotesPromise;
const remotesImportMap = await processRemoteInfos(remotes, options);

let importMap: ImportMap;
if (options.hostRemoteEntry) {
const hostInfoPromise = loadFederationInfo(options.hostRemoteEntry + cacheOption);
const hostInfo = await hostInfoPromise;
const hostImportMap = await processHostInfo(hostInfo);
importMap = mergeImportMaps(hostImportMap, remotesImportMap);
} else {
importMap = remotesImportMap;
}

appendImportMap(importMap);
return importMap;
}

Expand All @@ -49,8 +54,10 @@ async function loadManifest(remotes: string): Promise<Record<string, string>> {

export async function processRemoteInfos(
remotes: Record<string, string>,
options: ProcessRemoteInfoOptions = { throwIfRemoteNotFound: false }
optionsOverride?: Partial<InitFederationOptions>
): Promise<ImportMap> {
const options = setOptions(optionsOverride);

const processRemoteInfoPromises = Object.keys(remotes).map(
async (remoteName) => {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ export interface FederationInfo {

export interface InitFederationOptions {
cacheTag?: string;
}

export interface ProcessRemoteInfoOptions extends InitFederationOptions {
hostRemoteEntry: string|false;
throwIfRemoteNotFound: boolean;
}
}
9 changes: 9 additions & 0 deletions libs/native-federation-runtime/src/lib/utils/set-options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { InitFederationOptions } from "../model/federation-info";

export function setOptions(overrides: Partial<InitFederationOptions>|undefined) {
return {
hostRemoteEntry: "./remoteEntry.json",
throwIfRemoteNotFound: false,
...overrides
} as InitFederationOptions;
}