Skip to content

Commit

Permalink
fix(nextjs-mf): cache bust remote urls (#2232)
Browse files Browse the repository at this point in the history
Co-authored-by: ScriptedAlchemy <[email protected]>
  • Loading branch information
ScriptedAlchemy and ScriptedAlchemy authored Mar 27, 2024
1 parent cc55760 commit 1e2c8a3
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/thick-birds-clap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@module-federation/nextjs-mf': patch
---

cache bust remote entry
2 changes: 1 addition & 1 deletion apps/3001-shop/pages/shop/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ Shop.getInitialProps = async () => {
const timeout = (ms: number) =>
new Promise((resolve) => setTimeout(resolve, ms));

const fetchPromise = fetch('https://swapi.dev/api/people/1')
const fetchPromise = fetch('http://swapi.dev/api/planets/1/')
.then((res) => res.json())
.catch((err) => {
if (err instanceof Error) {
Expand Down
6 changes: 3 additions & 3 deletions apps/3002-checkout/pages/checkout/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ Checkout.getInitialProps = async () => {
const timeout = (ms: number) =>
new Promise((resolve) => setTimeout(resolve, ms));

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

// this will resolve after 3 seconds
const timerPromise = timeout(3000).then(() => ({
Expand Down
24 changes: 21 additions & 3 deletions packages/nextjs-mf/src/plugins/container/runtimePlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,19 @@ export default function (): FederationRuntimePlugin {
init(args) {
return args;
},
beforeRequest(args) {
beforeRequest: (args) => {
const { options, id } = args;
const remoteName = id.split('/').shift();
const remote = options.remotes.find(
(remote) => remote.name === remoteName,
);
if (!remote) return args;
//@ts-ignore
if (remote?.entry?.includes('?t=')) {
return args;
}
//@ts-ignore
remote.entry = `${remote?.entry}?t=${Date.now()}`;
return args;
},
createScript({ url }) {
Expand All @@ -76,7 +88,12 @@ export default function (): FederationRuntimePlugin {
if (!moduleOrFactory) return args; // Ensure moduleOrFactory is defined

if (typeof window === 'undefined') {
let exposedModuleExports: any = moduleOrFactory();
let exposedModuleExports: any;
try {
exposedModuleExports = moduleOrFactory();
} catch (e) {
exposedModuleExports = moduleOrFactory;
}

const handler: ProxyHandler<any> = {
get(target, prop, receiver) {
Expand Down Expand Up @@ -130,12 +147,13 @@ export default function (): FederationRuntimePlugin {
);
}
});
return () => exposedModuleExports;
} else {
// For objects, just wrap the exported object itself
exposedModuleExports = new Proxy(exposedModuleExports, handler);
}

return () => exposedModuleExports;
return exposedModuleExports;
}

return args;
Expand Down

0 comments on commit 1e2c8a3

Please sign in to comment.