Skip to content

Commit fc5e851

Browse files
committed
refactor(rsc-mf): generalize remote publicPath runtime resolution
1 parent 7410cb8 commit fc5e851

2 files changed

Lines changed: 39 additions & 5 deletions

File tree

tests/integration/rsc-mf/host/runtime/forceRemotePublicPath.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import type { ModuleFederationRuntimePlugin } from '@module-federation/modern-js-v3';
22

3-
const TARGET_REMOTE_ALIAS = 'rscRemote';
43
const SSR_BUNDLES_SEGMENT = '/bundles/';
54

65
interface FederationRemoteConfig {
@@ -210,11 +209,17 @@ const forceRemotePublicPath = (): ModuleFederationRuntimePlugin => ({
210209
name: 'rsc-mf-force-remote-public-path',
211210
loadRemoteSnapshot(args: any) {
212211
const { remoteInfo, remoteSnapshot } = args;
213-
if (remoteInfo?.alias !== TARGET_REMOTE_ALIAS || !remoteSnapshot) {
212+
const remoteAlias =
213+
typeof remoteInfo?.alias === 'string' && remoteInfo.alias
214+
? remoteInfo.alias
215+
: typeof remoteInfo?.name === 'string' && remoteInfo.name
216+
? remoteInfo.name
217+
: undefined;
218+
if (!remoteAlias || !remoteSnapshot) {
214219
return args;
215220
}
216221
const { remotePublicPath, remoteSsrPublicPath } = resolveRemotePublicPaths({
217-
remoteAlias: remoteInfo.alias,
222+
remoteAlias,
218223
remoteEntry:
219224
typeof remoteInfo?.entry === 'string' ? remoteInfo.entry : undefined,
220225
});

tests/integration/rsc-mf/tests/forceRemotePublicPath.test.ts

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,10 @@ describe('host forceRemotePublicPath runtime plugin', () => {
2525
expect(typeof plugin.loadRemoteSnapshot).toBe('function');
2626
});
2727

28-
it('does not mutate non-target remotes', () => {
28+
it('does not mutate when remote alias and name are missing', () => {
2929
const plugin = forceRemotePublicPath();
3030
const args = {
3131
remoteInfo: {
32-
alias: 'anotherRemote',
3332
entry: 'http://127.0.0.1:3008/static/mf-manifest.json',
3433
},
3534
remoteSnapshot: {
@@ -42,6 +41,36 @@ describe('host forceRemotePublicPath runtime plugin', () => {
4241
expect(args.remoteSnapshot.publicPath).toBe('http://example.com/');
4342
});
4443

44+
it('supports remote names when alias is unavailable', () => {
45+
const plugin = forceRemotePublicPath();
46+
const args = {
47+
remoteInfo: {
48+
name: 'anotherRemote',
49+
entry: 'https://another-remote.example.com/static/mf-manifest.json',
50+
},
51+
remoteSnapshot: {
52+
publicPath: 'http://example.com/',
53+
metaData: {
54+
publicPath: 'http://example.com/',
55+
ssrPublicPath: 'http://example.com/bundles/',
56+
},
57+
},
58+
};
59+
60+
const result = plugin.loadRemoteSnapshot?.(args as any);
61+
62+
expect(result).toBe(args);
63+
expect(args.remoteSnapshot.publicPath).toBe(
64+
'https://another-remote.example.com/',
65+
);
66+
expect(args.remoteSnapshot.metaData.publicPath).toBe(
67+
'https://another-remote.example.com/',
68+
);
69+
expect(args.remoteSnapshot.metaData.ssrPublicPath).toBe(
70+
'https://another-remote.example.com/bundles/',
71+
);
72+
});
73+
4574
it('does not mutate when entry is missing or non-string', () => {
4675
const plugin = forceRemotePublicPath();
4776
const argsWithoutEntry = {

0 commit comments

Comments
 (0)