Skip to content

Commit

Permalink
fix(nextjs-mf): fix broken loading of non nextjs remotes
Browse files Browse the repository at this point in the history
the change to consume mf 2.0 manifests broke loading of non nextjs remotes because it was hard coded
to assume next remotes.
  • Loading branch information
h3adache committed Dec 11, 2024
1 parent fa7a0bd commit 7890299
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { removeUnnecessarySharedKeys } from './remove-unnecessary-shared-keys';

describe('removeUnnecessarySharedKeys', () => {
beforeEach(() => {
jest.spyOn(console, 'warn').mockImplementation(() => {});
jest.spyOn(console, 'warn').mockImplementation(jest.fn());
});

afterEach(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import {
} from '@module-federation/enhanced';

class InvertedContainerPlugin {
constructor() {}

public apply(compiler: Compiler): void {
compiler.hooks.thisCompilation.tap(
'EmbeddedContainerPlugin',
Expand Down
21 changes: 9 additions & 12 deletions packages/nextjs-mf/src/plugins/container/runtimePlugin.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import { FederationRuntimePlugin } from '@module-federation/runtime/types';
import {
ModuleInfo,
ConsumerModuleInfoWithPublicPath,
} from '@module-federation/sdk';

export default function (): FederationRuntimePlugin {
return {
Expand Down Expand Up @@ -199,22 +195,23 @@ export default function (): FederationRuntimePlugin {
return args;
}

// re-assign publicPath based on remoteEntry location
if (options.inBrowser) {
remoteSnapshot.publicPath = remoteSnapshot.publicPath.substring(
// re-assign publicPath based on remoteEntry location if in browser nextjs remote
const { publicPath } = remoteSnapshot;
if (options.inBrowser && publicPath.includes('/_next/')) {
remoteSnapshot.publicPath = publicPath.substring(
0,
remoteSnapshot.publicPath.lastIndexOf('/_next/') + 7,
publicPath.lastIndexOf('/_next/') + 7,
);
} else {
const serverPublicPath = manifestUrl.substring(
0,
manifestUrl.indexOf('mf-manifest.json'),
);

remoteSnapshot.publicPath = serverPublicPath;
if ('publicPath' in manifestJson.metaData) {
manifestJson.metaData.publicPath = serverPublicPath;
}
}

if ('publicPath' in manifestJson.metaData) {
manifestJson.metaData.publicPath = remoteSnapshot.publicPath;
}

return args;
Expand Down
2 changes: 1 addition & 1 deletion packages/nextjs-mf/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export type { FlushedChunksProps } from './flushedChunks';
*/
export const revalidate = function (
fetchModule: any = undefined,
force: boolean = false,
force = false,
): Promise<boolean> {
if (typeof window !== 'undefined') {
console.error('revalidate should only be called server-side');
Expand Down

0 comments on commit 7890299

Please sign in to comment.