Skip to content

Context API Sharing

Understanding context api sharing helps you work with Module Federation confidently. Here you will learn the core ideas behind context api sharing, see working code, and pick up best practices used on real teams.

Context API Sharing Overview

At its core, context api sharing is about doing one thing well inside your Module Federation project. Once you understand the pattern, you can apply it consistently across features and teams.

Good context api sharing pays off across the whole codebase: fewer surprises, easier testing, and smoother onboarding. The snippet below is a solid starting point.

const { ModuleFederationPlugin } = require('webpack').container;

module.exports = {
  plugins: [
    new ModuleFederationPlugin({
      name: 'shell',
      remotes: { catalog: 'catalog@http://localhost:3001/remoteEntry.js' },
      exposes: {},
      shared: { react: { singleton: true }, 'react-dom': { singleton: true } },
    }),
  ],
};

ModuleFederationPlugin configures which modules an app exposes, consumes, and shares.

Context API Sharing Example

new ModuleFederationPlugin({
  name: 'app',
  filename: 'remoteEntry.js',
  exposes: { './Widget': './src/Widget' },
  remotes: { other: 'other@http://host/remoteEntry.js' },
  shared: { react: { singleton: true } },
});
  • Start from a minimal Context API Sharing example and grow it only as needed.
  • Keep configuration explicit so Context API Sharing behaves the same in every environment.
  • Name things clearly so teammates understand your Context API Sharing at a glance.
  • Add tests around Context API Sharing early to lock in expected behaviour.

Module Federation Cheatsheet

Key Module Federation settings related to context api sharing.

Option Example Purpose
name name: 'shell' Unique container name
filename filename: 'remoteEntry.js' Remote entry manifest
exposes exposes: { './X': './src/X' } Modules a remote shares
remotes remotes: { app: 'app@url' } Remotes a host consumes
shared shared: { react: { singleton: true } } Deduplicate libraries
lazy load import('remote/Module') Load remotes on demand
Suspense <Suspense fallback={...}> Handle async loading

How Context API Sharing Works in Module Federation

Context API Sharing builds on Module Federation's ability to load code from another independently built and deployed application at runtime. Each app can be a host, a remote, or both.

ModuleFederationPlugin configures which modules an app exposes, consumes, and shares.

  • Remotes expose modules through a remoteEntry.js manifest.
  • Hosts declare remotes and import exposed modules dynamically.
  • Shared dependencies are deduplicated, ideally as singletons.
  • Each micro frontend builds and deploys on its own schedule.

Practical Guidance for Context API Sharing

In production, context api sharing needs careful version management and graceful failure handling. Align shared dependency versions and always render a fallback when a remote cannot load.

Concern Recommendation
Shared versions Use singletons with requiredVersion
Runtime errors Wrap remotes in error boundaries and fallbacks
Deployment Resolve remotes from a runtime manifest
Performance Lazy-load remotes and cache remoteEntry.js

Common Mistakes

  • Skipping error handling and edge cases when wiring up context api sharing.
  • Leaving context api sharing untested, so regressions slip into production.
  • Over-engineering context api sharing before you actually need the extra flexibility.
  • Ignoring documentation, which makes context api sharing hard for the next developer to change.

Key Takeaways

  • Context API Sharing is a core part of working effectively with Module Federation.
  • Start small and keep context api sharing focused on a single responsibility.
  • Apply consistent patterns so context api sharing scales across your project.
  • Test and document context api sharing to keep it maintainable over time.

Pro Tip

Bookmark this context api sharing pattern and reuse it. Consistency across your Module Federation codebase is worth more than clever one-off solutions.