Skip to content

Commit 5ad75fd

Browse files
feat(nextjs-mf): enable JSON manifest for NextJS (#2726)
Co-authored-by: Zack Jackson <zackary.l.jackson@gmail.com> Co-authored-by: Zack Jackson <25274700+ScriptedAlchemy@users.noreply.github.com>
1 parent ab6ab76 commit 5ad75fd

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

.changeset/red-meals-sleep.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@module-federation/nextjs-mf': minor
3+
---
4+
5+
Enabled JSON manifest remote protocol for NextJS plugin

packages/nextjs-mf/src/plugins/container/runtimePlugin.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
import { FederationRuntimePlugin } from '@module-federation/runtime/types';
2+
import {
3+
ModuleInfo,
4+
ConsumerModuleInfoWithPublicPath,
5+
} from '@module-federation/sdk';
26

37
export default function (): FederationRuntimePlugin {
48
return {
@@ -182,7 +186,39 @@ export default function (): FederationRuntimePlugin {
182186

183187
return args;
184188
},
189+
loadRemoteSnapshot(args) {
190+
const { from, remoteSnapshot, manifestUrl, manifestJson, options } = args;
185191

192+
// ensure snapshot is loaded from manifest
193+
if (
194+
from !== 'manifest' ||
195+
!manifestUrl ||
196+
!manifestJson ||
197+
!('publicPath' in remoteSnapshot)
198+
) {
199+
return args;
200+
}
201+
202+
// re-assign publicPath based on remoteEntry location
203+
if (options.inBrowser) {
204+
remoteSnapshot.publicPath = remoteSnapshot.publicPath.substring(
205+
0,
206+
remoteSnapshot.publicPath.lastIndexOf('/_next/') + 7,
207+
);
208+
} else {
209+
const serverPublicPath = manifestUrl.substring(
210+
0,
211+
manifestUrl.indexOf('mf-manifest.json'),
212+
);
213+
214+
remoteSnapshot.publicPath = serverPublicPath;
215+
if ('publicPath' in manifestJson.metaData) {
216+
manifestJson.metaData.publicPath = serverPublicPath;
217+
}
218+
}
219+
220+
return args;
221+
},
186222
resolveShare: function (args: any) {
187223
if (
188224
args.pkgName !== 'react' &&

0 commit comments

Comments
 (0)