logologo
Guide
Practice
Configuration
Plugins
Showcase
Blog
Ecosystem
Module Federation Examples
Practical Module Federation
Zephyr Cloud
Nx
简体中文
English
Guide
Practice
Configuration
Plugins
Showcase
Blog
Module Federation Examples
Practical Module Federation
Zephyr Cloud
Nx
简体中文
English
logologo

Getting Started

Introduction
Setting Up the Environment
Quick Start Guide
Feature Navigation
Glossary of Terms
npm Packages

basic

Runtime

Runtime Access
Runtime API
Runtime Hooks
Rsbuild Plugin
Rspack Plugin
Webpack Plugin
Rspress Plugin
Vite Plugin
Metro
Type Hinting
Command Line Tool
Style Isolation

Data Solution

Data Fetching
Data Caching
Prefetch

Frameworks

Modern.js
Next.js

Deployment

Deploy with Zephyr Cloud

Debug

Enable debug mode
Chrome DevTools
Global variables

Troubleshooting

Overview

Runtime

RUNTIME-001
RUNTIME-002
RUNTIME-003
RUNTIME-004
RUNTIME-005
RUNTIME-006
RUNTIME-007
RUNTIME-008
RUNTIME-009

Build

BUILD-001
BUILD-002

Type

Overview
TYPE-001
Other
Edit this page on GitHub
Previous PagePrefetch
Next PageNext.js

#Modern.js

Modern.js is a progressive web development framework based on React. Internally at ByteDance, Modern.js supports the development of thousands of web applications.

The Module Federation team works closely with the Modern.js team and provides the @module-federation/modern-js plugin to help users better utilize Module Federation within Modern.js.

#Supports

  • modern.js ^2.56.1
  • Includes Server-Side Rendering (SSR)

We highly recommend referencing these applications, which showcases the best practices for integrating Modern.js with Module Federation:

  • Server-Side Rendering (SSR)
  • Component-Level Data Fetch

#Quick Start

#Installation

You can install the plugin using the following commands:

npm
yarn
pnpm
bun
npm add @module-federation/modern-js --save

#Apply Plugin

Apply this plugin in the plugins section of modern.config.ts:

modern.config.ts
import { appTools, defineConfig } from '@modern-js/app-tools';
import { moduleFederationPlugin } from '@module-federation/modern-js';

export default defineConfig({
  dev: {
    port: 3005,
  },
  runtime: {
    router: true,
  },
  // moduleFederationPlugin is a plugin for modern.js that can make certain modifications to the build/runtime
  plugins: [appTools(), moduleFederationPlugin()],
});

Then, create the module-federation.config.ts file and add the required configuration:

module-federation.config.ts
import { createModuleFederationConfig } from '@module-federation/modern-js';
export default createModuleFederationConfig({
  name: 'host',
  remotes: {
    remote: 'remote@http://localhost:3006/mf-manifest.json',
  },
  shared: {
    react: { singleton: true },
    'react-dom': { singleton: true },
  },
});

#Server-Side Rendering (SSR)

Note

For a better performance experience, Module Federation X Modern.js SSR only supports stream SSR.

There is no difference in using Module Federation in SSR scenarios compared to CSR scenarios; developers can continue with their existing development practices.

#Component-Level Data Fetch

See Data Fetching.

The Modern.js plugin re-exports @module-federation/bridge-react from @module-federation/modern-js/react, so you don't need to install it separately.

#API

@module-federation/modern-js re-exports @module-federation/modern-js/runtime from the runtime subpath. You can use @module-federation/modern-js/runtime to get the MF Runtime.Runtime。

#createRemoteComponent Deprecated

DANGER

This API has been deprecated. Please use createLazyComponent instead.

#Migration Guide

The parameters for createRemoteComponent and createLazyComponent are identical. The only difference is that the createLazyComponent API needs to be called through an instance after registering the plugin.

- import { createRemoteComponent } from '@module-federation/modern-js/runtime';
+ import { getInstance } from '@module-federation/modern-js/runtime';
+ import { lazyLoadComponentPlugin } from '@module-federation/modern-js/react';

const instance = getInstance();
// After registering the lazyLoadComponentPlugin, the instance will automatically add the createLazyComponent API
instance.registerPlugins([lazyLoadComponentPlugin()]);

- const LazyComponent = createRemoteComponent({
+ const LazyComponent = instance.createLazyComponent({
  loader: () => import('remote/Image'),
  loading: <div>loading...</div>,
  fallback: ({ error }) => {
    console.error(error)
    return <div>fallback</div>;
  },
});

const App: FC = () => {
  return <>
    <LazyComponent />
  </>;
};
export default App;

#createRemoteSSRComponent Deprecated

DANGER

This API has been deprecated. Please use createLazyComponent instead.

#Migration Guide

Same as createRemoteComponent#migration-guide

#Configuration

#ssr

  • Type: false
  • Required: No
  • Default value: undefined

@module-federation/modern-js will automatically add SSR-related build presets based on server.ssr in the modern.js config.

If the current project only needs to load MF in CSR, you can set ssr: false to help with progressive migration.

modern.config.ts
import { appTools, defineConfig } from '@modern-js/app-tools';
import { moduleFederationPlugin } from '@module-federation/modern-js';

// https://modernjs.dev/en/configure/app/usage
export default defineConfig({
  dev: {
    port: 3050,
  },
  runtime: {
    router: true,
  },
  server: {
    ssr: {
      mode: 'stream',
    },
  },
  plugins: [
    appTools(),
    moduleFederationPlugin({ ssr: false })
  ],
});

#fetchServerQuery

  • Type: Record<string, unknown>
  • Required: No
  • Default: undefined

If a downgrade occurs, an HTTP request will be sent to the server. This configuration can be used to add query parameters to that request.