Skip to content
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
3 changes: 3 additions & 0 deletions packages/nextjs-mf/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ module.exports = {
'ts-jest',
{
tsconfig: '<rootDir>/tsconfig.spec.json',
// Disabled because runtimePlugin.ts uses webpack globals and
// untyped hook args that fail strict type-checking.
diagnostics: false,
},
],
},
Expand Down
1 change: 1 addition & 0 deletions packages/nextjs-mf/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
"dependencies": {
"fast-glob": "^3.2.11",
"@module-federation/runtime": "workspace:*",
"@module-federation/runtime-core": "workspace:*",
Comment thread
nag-clearstreet marked this conversation as resolved.
"@module-federation/sdk": "workspace:*",
"@module-federation/enhanced": "workspace:*",
"@module-federation/node": "workspace:*",
Expand Down
138 changes: 138 additions & 0 deletions packages/nextjs-mf/src/plugins/container/runtimePlugin.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
import createRuntimePlugin from './runtimePlugin';

describe('next-internal-plugin beforeRequest', () => {
const plugin = createRuntimePlugin();
const beforeRequest = plugin.beforeRequest! as (args: any) => any;

function makeArgs(
id: string,
remotes: Array<{ name: string; alias?: string; entry: string }>,
) {
return {
id,
options: {
remotes: remotes.map((r) => ({ ...r })),
},
};
}

it('appends ?t= when id has no expose path (bare id)', () => {
const args = makeArgs('app1', [
{ name: 'app1', entry: 'https://cdn.example.com/mf-manifest.json' },
]);

const result = beforeRequest(args);

expect(result.options.remotes[0].entry).toMatch(
/^https:\/\/cdn\.example\.com\/mf-manifest\.json\?t=\d+$/,
);
});

it('appends ?t= when remote name matches id prefix', () => {
const args = makeArgs('app1/button', [
{ name: 'app1', entry: 'https://cdn.example.com/mf-manifest.json' },
]);

const result = beforeRequest(args);

expect(result.options.remotes[0].entry).toMatch(
/^https:\/\/cdn\.example\.com\/mf-manifest\.json\?t=\d+$/,
);
});

it('appends ?t= when remote alias matches id prefix', () => {
const args = makeArgs('my-alias/button', [
{
name: 'my-remote-provider',
alias: 'my-alias',
entry: 'https://cdn.example.com/mf-manifest.json',
},
]);

const result = beforeRequest(args);

expect(result.options.remotes[0].entry).toMatch(
/^https:\/\/cdn\.example\.com\/mf-manifest\.json\?t=\d+$/,
);
});

it('appends ?t= when remote has a scoped alias', () => {
const args = makeArgs('@scope/my-remote/widget', [
{
name: 'my-remote-provider',
alias: '@scope/my-remote',
entry: 'https://cdn.example.com/mf-manifest.json',
},
]);

const result = beforeRequest(args);

expect(result.options.remotes[0].entry).toMatch(
/^https:\/\/cdn\.example\.com\/mf-manifest\.json\?t=\d+$/,
);
});

it('appends ?t= when remote has a scoped name (no alias)', () => {
const args = makeArgs('@federation/app1/button', [
{
name: '@federation/app1',
entry: 'https://cdn.example.com/mf-manifest.json',
},
]);

const result = beforeRequest(args);

expect(result.options.remotes[0].entry).toMatch(
/^https:\/\/cdn\.example\.com\/mf-manifest\.json\?t=\d+$/,
);
});

it('does not double-append ?t= if already present', () => {
const args = makeArgs('app1/button', [
{
name: 'app1',
entry: 'https://cdn.example.com/mf-manifest.json?t=1234567890',
},
]);

const result = beforeRequest(args);

expect(result.options.remotes[0].entry).toBe(
'https://cdn.example.com/mf-manifest.json?t=1234567890',
);
});

it('returns args unchanged when no remote matches', () => {
const args = makeArgs('unknown-remote/button', [
{ name: 'app1', entry: 'https://cdn.example.com/mf-manifest.json' },
]);

const result = beforeRequest(args);

expect(result.options.remotes[0].entry).toBe(
'https://cdn.example.com/mf-manifest.json',
);
});

it('only modifies the matching remote when multiple remotes exist', () => {
const args = makeArgs('app2/button', [
{
name: 'app1',
entry: 'https://cdn.example.com/app1/mf-manifest.json',
},
{
name: 'app2',
entry: 'https://cdn.example.com/app2/mf-manifest.json',
},
]);

const result = beforeRequest(args);

expect(result.options.remotes[0].entry).toBe(
'https://cdn.example.com/app1/mf-manifest.json',
);
expect(result.options.remotes[1].entry).toMatch(
/^https:\/\/cdn\.example\.com\/app2\/mf-manifest\.json\?t=\d+$/,
);
});
});
11 changes: 5 additions & 6 deletions packages/nextjs-mf/src/plugins/container/runtimePlugin.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ModuleFederationRuntimePlugin } from '@module-federation/runtime';
import { matchRemoteWithNameAndExpose } from '@module-federation/runtime-core';

export default function (): ModuleFederationRuntimePlugin {
return {
Expand Down Expand Up @@ -92,12 +93,10 @@ export default function (): ModuleFederationRuntimePlugin {
beforeRequest: function (args: any) {
const options = args.options;
const id = args.id;
const remoteName = id.split('/').shift();
const remote = options.remotes.find(function (remote: any) {
return remote.name === remoteName;
});
if (!remote) return args;
if (remote && remote.entry && remote.entry.includes('?t=')) {
const match = matchRemoteWithNameAndExpose(options.remotes, id);
if (!match) return args;
const remote = match.remote;
if (remote.entry && remote.entry.includes('?t=')) {
return args;
}
remote.entry = remote.entry + '?t=' + Date.now();
Comment thread
nag-clearstreet marked this conversation as resolved.
Expand Down
Loading