Skip to content

fix(nextjs-mf): cache bust remote urls #2232

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 27, 2024
Merged
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
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