Skip to content

Commit 1e2c8a3

Browse files
fix(nextjs-mf): cache bust remote urls (#2232)
Co-authored-by: ScriptedAlchemy <[email protected]>
1 parent cc55760 commit 1e2c8a3

File tree

4 files changed

+30
-7
lines changed

4 files changed

+30
-7
lines changed

.changeset/thick-birds-clap.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': patch
3+
---
4+
5+
cache bust remote entry

apps/3001-shop/pages/shop/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ Shop.getInitialProps = async () => {
4949
const timeout = (ms: number) =>
5050
new Promise((resolve) => setTimeout(resolve, ms));
5151

52-
const fetchPromise = fetch('https://swapi.dev/api/people/1')
52+
const fetchPromise = fetch('http://swapi.dev/api/planets/1/')
5353
.then((res) => res.json())
5454
.catch((err) => {
5555
if (err instanceof Error) {

apps/3002-checkout/pages/checkout/index.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ Checkout.getInitialProps = async () => {
4747
const timeout = (ms: number) =>
4848
new Promise((resolve) => setTimeout(resolve, ms));
4949

50-
const fetchPromise = fetch(
51-
'https://jsonplaceholder.typicode.com/todos/1',
52-
).then((res) => res.json());
50+
const fetchPromise = fetch('http://swapi.dev/api/planets/1/').then((res) =>
51+
res.json(),
52+
);
5353

5454
// this will resolve after 3 seconds
5555
const timerPromise = timeout(3000).then(() => ({

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

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,19 @@ export default function (): FederationRuntimePlugin {
6161
init(args) {
6262
return args;
6363
},
64-
beforeRequest(args) {
64+
beforeRequest: (args) => {
65+
const { options, id } = args;
66+
const remoteName = id.split('/').shift();
67+
const remote = options.remotes.find(
68+
(remote) => remote.name === remoteName,
69+
);
70+
if (!remote) return args;
71+
//@ts-ignore
72+
if (remote?.entry?.includes('?t=')) {
73+
return args;
74+
}
75+
//@ts-ignore
76+
remote.entry = `${remote?.entry}?t=${Date.now()}`;
6577
return args;
6678
},
6779
createScript({ url }) {
@@ -76,7 +88,12 @@ export default function (): FederationRuntimePlugin {
7688
if (!moduleOrFactory) return args; // Ensure moduleOrFactory is defined
7789

7890
if (typeof window === 'undefined') {
79-
let exposedModuleExports: any = moduleOrFactory();
91+
let exposedModuleExports: any;
92+
try {
93+
exposedModuleExports = moduleOrFactory();
94+
} catch (e) {
95+
exposedModuleExports = moduleOrFactory;
96+
}
8097

8198
const handler: ProxyHandler<any> = {
8299
get(target, prop, receiver) {
@@ -130,12 +147,13 @@ export default function (): FederationRuntimePlugin {
130147
);
131148
}
132149
});
150+
return () => exposedModuleExports;
133151
} else {
134152
// For objects, just wrap the exported object itself
135153
exposedModuleExports = new Proxy(exposedModuleExports, handler);
136154
}
137155

138-
return () => exposedModuleExports;
156+
return exposedModuleExports;
139157
}
140158

141159
return args;

0 commit comments

Comments
 (0)