Skip to content

Instantly share code, notes, and snippets.

@jherr
Last active July 17, 2022 16:01
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save jherr/dd10bc34448590a2661a0c6acbcc7e8a to your computer and use it in GitHub Desktop.
Save jherr/dd10bc34448590a2661a0c6acbcc7e8a to your computer and use it in GitHub Desktop.
  1. Install @module-federation/nextjs-mf.
yarn add @module-federation/nextjs-mf
  1. Add resolutions to package.json:
  "resolutions": {
    "webpack": "5.1.3"
  },
  1. Create next.config.js.

Remote:

const {
  withModuleFederation,
  MergeRuntime,
} = require("@module-federation/nextjs-mf");
const path = require("path");

module.exports = {
  webpack: (config, options) => {
    const { buildId, dev, isServer, defaultLoaders, webpack } = options;
    const mfConf = {
      name: "test1",
      library: { type: config.output.libraryTarget, name: "test1" },
      filename: "static/runtime/remoteEntry.js",
      remotes: {},
      exposes: {
        "./nav": "./components/nav",
      },
      shared: [],
    };

    // Configures ModuleFederation and other Webpack properties
    withModuleFederation(config, options, mfConf);

    config.plugins.push(new MergeRuntime());

    if (!isServer) {
      config.output.publicPath = "http://localhost:3000/_next/";
    }

    return config;
  },
};

Consumer:

const {
  withModuleFederation,
  MergeRuntime,
} = require("@module-federation/nextjs-mf");
const path = require("path");

module.exports = {
  webpack: (config, options) => {
    const { buildId, dev, isServer, defaultLoaders, webpack } = options;
    const mfConf = {
      name: "test2",
      library: { type: config.output.libraryTarget, name: "test2" },
      filename: "static/runtime/remoteEntry.js",
      remotes: {
        // For SSR, resolve to disk path (or you can use code streaming if you have access)
        test1: isServer
          ? path.resolve(
              __dirname,
              "../test1/.next/server/static/runtime/remoteEntry.js"
            )
          : "test1", // for client, treat it as a global
      },
      exposes: {},
      shared: [],
    };

    // Configures ModuleFederation and other Webpack properties
    withModuleFederation(config, options, mfConf);

    config.plugins.push(new MergeRuntime());

    if (!isServer) {
      config.output.publicPath = "http://localhost:3000/_next/";
    }

    return config;
  },
};
  1. Add pages/_document.js:

Remote:

import Document, { Html, Head, Main, NextScript } from "next/document";
import { patchSharing } from "@module-federation/nextjs-mf";

class MyDocument extends Document {
  static async getInitialProps(ctx) {
    const initialProps = await Document.getInitialProps(ctx);
    return { ...initialProps };
  }

  render() {
    return (
      <Html>
        {patchSharing()}
        <Head />
        <body>
          <Main />
          <NextScript />
        </body>
      </Html>
    );
  }
}

export default MyDocument;

Consumer:

import Document, { Html, Head, Main, NextScript } from "next/document";
import { patchSharing } from "@module-federation/nextjs-mf";

class MyDocument extends Document {
  static async getInitialProps(ctx) {
    const initialProps = await Document.getInitialProps(ctx);
    return { ...initialProps };
  }

  render() {
    return (
      <Html>
        {patchSharing()}
        <script src="http://localhost:3000/_next/static/chunks/webpack.js" />
        <script src="http://localhost:3000/_next/static/runtime/remoteEntry.js" />
        <Head />
        <body>
          <Main />
          <NextScript />
        </body>
      </Html>
    );
  }
}

export default MyDocument;
  1. import Nav
const Nav = (await import("test1/nav")).default;

Use it per normal.

@NachoJusticia
Copy link

It seems @module-federation/nextjs-mf does not exports MergeRuntime anymore

Copy link

ghost commented Jun 6, 2022

It seems @module-federation/nextjs-mf does not exports MergeRuntime anymore

Yes.

TypeError: MergeRuntime is not a constructor the error i'm getting.

Any solutions ???

@jherr
Copy link
Author

jherr commented Jun 6, 2022

I don't think that module is supported for free anymore. :( This is the up-to-date version https://app.privjs.com/buy/packageDetail?pkg=@module-federation/nextjs-mf and you have to buy it. :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment