Skip to content
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

fix: clear globalLoading cache in node env #3173

Merged
merged 2 commits into from
Nov 7, 2024
Merged

Conversation

2heal1
Copy link
Member

@2heal1 2heal1 commented Nov 5, 2024

Description

The bundler bundle GLOBAL_LOADING_REMOTE_ENTRY into share object closure ,and it can not access via globalThis .
image

This PR aims to solve this issue and clear globalLoading internally in revalidate method.

Related Issue

#3119

Types of changes

  • Docs change / refactoring / dependency upgrade
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)

Checklist

  • I have added tests to cover my changes.
  • All new and existing tests passed.
  • I have updated the documentation.

Copy link

changeset-bot bot commented Nov 5, 2024

🦋 Changeset detected

Latest commit: 035aa79

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 27 packages
Name Type
@module-federation/runtime Patch
@module-federation/node Patch
@module-federation/devtools Patch
@module-federation/data-prefetch Patch
@module-federation/dts-plugin Patch
@module-federation/nextjs-mf Patch
@module-federation/retry-plugin Patch
@module-federation/runtime-tools Patch
@module-federation/webpack-bundler-runtime Patch
@module-federation/modern-js Patch
@module-federation/enhanced Patch
@module-federation/rspack Patch
@module-federation/rsbuild-plugin Patch
@module-federation/storybook-addon Patch
@module-federation/modernjsapp Patch
@module-federation/sdk Patch
@module-federation/managers Patch
@module-federation/manifest Patch
@module-federation/third-party-dts-extractor Patch
@module-federation/bridge-react Patch
@module-federation/bridge-vue3 Patch
@module-federation/bridge-shared Patch
@module-federation/bridge-react-webpack-plugin Patch
@module-federation/error-codes Patch
@module-federation/esbuild Patch
@module-federation/utilities Patch
website-new Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link
Contributor

@squadronai squadronai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Summary

This pull request aims to address an issue with the bundler, where the GLOBAL_LOADING_REMOTE_ENTRY was being bundled into a shared object closure, preventing it from being accessed via globalThis. The changes focus on clearing the globalLoading cache in the node environment to resolve this problem.

The key changes include:

  • Removing the assignment of an empty object to the __GLOBAL_LOADING_REMOTE_ENTRY__ property of the global scope in the hot-reload.ts file, which was causing issues with accessing the globalLoading cache.
  • Iterating over the keys in the globalLoading object and deleting them in the global.ts file, ensuring that the cache is properly cleared when the resetFederationGlobalInfo() function is called.

These modifications should help improve the overall functionality of the application by addressing the issue with the GLOBAL_LOADING_REMOTE_ENTRY and ensuring that the globalLoading cache is properly cleared in the node environment.

File Summaries
File Summary
packages/node/src/utils/hot-reload.ts The code changes aim to clear the globalLoading cache in the node environment. The changes involve removing the assignment of an empty object to the __GLOBAL_LOADING_REMOTE_ENTRY__ property of the global scope, which was causing issues with accessing the globalLoading cache. This modification should help resolve the problem and improve the overall functionality of the application.
packages/runtime/src/global.ts The code changes aim to clear the globalLoading cache in the node environment. This is done by iterating over the keys in the globalLoading object and deleting them, ensuring that the cache is properly cleared when the resetFederationGlobalInfo() function is called.

Copy link

netlify bot commented Nov 5, 2024

Deploy Preview for module-federation-docs ready!

Name Link
🔨 Latest commit 035aa79
🔍 Latest deploy log https://app.netlify.com/sites/module-federation-docs/deploys/672c74cba53289000812546e
😎 Deploy Preview https://deploy-preview-3173--module-federation-docs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link
Contributor

@squadronai squadronai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Incremental Review

Comments posted: 6

Configuration

Squadron Mode: essential

Commits Reviewed

54bfd1cf730a842fd323d88d1b7c8ff72678f0ce...fb49d7518b62c3f579702986bae95a61f1ea03b1

Files Reviewed
  • packages/node/src/utils/hot-reload.ts
  • packages/runtime/src/helpers.ts
Files Ignored

These files were ignored due to the filter in the squadron.yaml file.

  • .changeset/beige-cheetahs-tap.md

@module-federation module-federation deleted a comment from squadronai bot Nov 5, 2024
@module-federation module-federation deleted a comment from squadronai bot Nov 5, 2024
@module-federation module-federation deleted a comment from squadronai bot Nov 5, 2024
@module-federation module-federation deleted a comment from squadronai bot Nov 5, 2024
@module-federation module-federation deleted a comment from squadronai bot Nov 5, 2024
@module-federation module-federation deleted a comment from squadronai bot Nov 5, 2024
@2heal1 2heal1 changed the title WIP: fix: clear globalLoading cache in node env fix: clear globalLoading cache in node env Nov 5, 2024
Copy link
Contributor

@MadaraUchiha-314 MadaraUchiha-314 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can I get a canary version so that I can test whether this fixes the issue ?

Comment on lines 156 to 158
Object.keys(helpers.global.globalLoading).forEach((key) => {
delete helpers.global.globalLoading[key];
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already have a function exposed as part of helpers:

helpers.global.resetFederationGlobalInfo();

Can we move this logic to that function ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ohh I forget this function , yes , we can do it , let me move it

@ScriptedAlchemy
Copy link
Member

Tested and worked! @2heal1

Copy link
Contributor

@squadronai squadronai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Incremental Review

Comments posted: 3

Configuration

Squadron Mode: essential

Commits Reviewed

fb49d7518b62c3f579702986bae95a61f1ea03b1...a62ac79219d2e78753aa26dcad8bec74894c7eb4

Files Reviewed
  • packages/runtime/src/global.ts
Files Ignored

These files were ignored due to the filter in the squadron.yaml file.

  • .changeset/beige-cheetahs-tap.md

Comment on lines +116 to +119
Object.keys(globalLoading).forEach((key) => {
delete globalLoading[key];
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current approach to clearing the globalLoading object could be simplified using Object.keys().length. A more efficient way would be to reassign the object:

Suggested change
Object.keys(globalLoading).forEach((key) => {
delete globalLoading[key];
});
// Reset globalLoading to empty object
Object.assign(globalLoading, {});

This achieves the same result with better performance, especially when dealing with objects that have many properties.

Comment on lines 112 to 114
globalThis.__FEDERATION__.moduleInfo = {};
globalThis.__FEDERATION__.__SHARE__ = {};
globalThis.__FEDERATION__.__MANIFEST_LOADING__ = {};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding type safety checks before resetting these global objects to prevent potential runtime errors:

Suggested change
globalThis.__FEDERATION__.moduleInfo = {};
globalThis.__FEDERATION__.__SHARE__ = {};
globalThis.__FEDERATION__.__MANIFEST_LOADING__ = {};
if (!globalThis.__FEDERATION__) {
globalThis.__FEDERATION__ = {};
}
globalThis.__FEDERATION__.moduleInfo = {};
globalThis.__FEDERATION__.__SHARE__ = {};
globalThis.__FEDERATION__.__MANIFEST_LOADING__ = {};

Comment on lines 112 to 120
globalThis.__FEDERATION__.moduleInfo = {};
globalThis.__FEDERATION__.__SHARE__ = {};
globalThis.__FEDERATION__.__MANIFEST_LOADING__ = {};

Object.keys(globalLoading).forEach((key) => {
delete globalLoading[key];
});
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding error handling around the global state reset operations. If any of these operations fail, it could leave the system in an inconsistent state. A try-catch block would help handle potential errors gracefully:

Suggested change
globalThis.__FEDERATION__.moduleInfo = {};
globalThis.__FEDERATION__.__SHARE__ = {};
globalThis.__FEDERATION__.__MANIFEST_LOADING__ = {};
Object.keys(globalLoading).forEach((key) => {
delete globalLoading[key];
});
}
try {
if (!globalThis.__FEDERATION__) {
globalThis.__FEDERATION__ = {};
}
globalThis.__FEDERATION__.moduleInfo = {};
globalThis.__FEDERATION__.__SHARE__ = {};
globalThis.__FEDERATION__.__MANIFEST_LOADING__ = {};
Object.assign(globalLoading, {});
} catch (error) {
console.error('Failed to clear federation cache:', error);
throw error; // Re-throw to maintain error propagation
}

@2heal1
Copy link
Member Author

2heal1 commented Nov 6, 2024

Can I get a canary version so that I can test whether this fixes the issue ?

@MadaraUchiha-314 Yeah sure , try this 0.0.0-next-20241106033151

Copy link
Contributor

@squadronai squadronai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Incremental Review

Comments posted: 1

Configuration

Squadron Mode: essential

Commits Reviewed

5fc6045a328c943878328da3349f945d2610c999...035aa792e5a2d3c6e0438513f77628a28263d3b6

Files Reviewed
  • packages/runtime/src/global.ts
Files Ignored

These files were ignored due to the filter in the squadron.yaml file.

  • .changeset/beige-cheetahs-tap.md

Comment on lines 113 to 115
CurrentGlobal.__FEDERATION__.moduleInfo = {};
CurrentGlobal.__FEDERATION__.__SHARE__ = {};
CurrentGlobal.__FEDERATION__.__MANIFEST_LOADING__ = {};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code resets global federation state without checking if these properties exist first. Add property existence checks to prevent potential undefined errors:

Suggested change
CurrentGlobal.__FEDERATION__.moduleInfo = {};
CurrentGlobal.__FEDERATION__.__SHARE__ = {};
CurrentGlobal.__FEDERATION__.__MANIFEST_LOADING__ = {};
if (CurrentGlobal.__FEDERATION__.moduleInfo) CurrentGlobal.__FEDERATION__.moduleInfo = {};
if (CurrentGlobal.__FEDERATION__.__SHARE__) CurrentGlobal.__FEDERATION__.__SHARE__ = {};
if (CurrentGlobal.__FEDERATION__.__MANIFEST_LOADING__) CurrentGlobal.__FEDERATION__.__MANIFEST_LOADING__ = {};

This defensive programming approach prevents errors if any of these objects haven't been initialized yet.

Note: The existing comments already cover other important aspects like type safety, error handling, and the globalLoading optimization, so I've focused on this additional safety concern.

@2heal1 2heal1 merged commit 66ba7b1 into main Nov 7, 2024
16 checks passed
@2heal1 2heal1 deleted the feat/clear-cache-api branch November 7, 2024 08:17
@2heal1 2heal1 mentioned this pull request Nov 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants