Skip to content

Instantly share code, notes, and snippets.

@jherr
Last active October 19, 2024 23:55
Show Gist options
  • Save jherr/dd10bc34448590a2661a0c6acbcc7e8a to your computer and use it in GitHub Desktop.
Save jherr/dd10bc34448590a2661a0c6acbcc7e8a to your computer and use it in GitHub Desktop.

Revisions

  1. jherr revised this gist Oct 29, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion WP5-nextjs.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    1. Install `@module-federation/nexjts-mf`.
    1. Install `@module-federation/nextjs-mf`.

    ```sh
    yarn add @module-federation/nextjs-mf
  2. jherr revised this gist Oct 28, 2020. 1 changed file with 4 additions and 12 deletions.
    16 changes: 4 additions & 12 deletions WP5-nextjs.md
    Original file line number Diff line number Diff line change
    @@ -8,19 +8,11 @@ yarn add @module-federation/nextjs-mf

    ```json
    "resolutions": {
    "webpack": "5.1.3",
    "next": "9.5.5"
    "webpack": "5.1.3"
    },
    ```

    3. Update next version to canary.

    ```json
    "dependencies": {
    "next": "^9.5.6-canary.0",
    ```

    4. Create next.config.js.
    3. Create next.config.js.

    Remote:

    @@ -102,7 +94,7 @@ module.exports = {
    };
    ```

    6. Add `pages/_document.js`:
    4. Add `pages/_document.js`:

    Remote:

    @@ -164,7 +156,7 @@ class MyDocument extends Document {
    export default MyDocument;
    ```

    7. import Nav
    5. import Nav

    ```js
    const Nav = (await import("test1/nav")).default;
  3. jherr revised this gist Oct 28, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion WP5-nextjs.md
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    1. Install `@module-federation/nexjts-mf`.

    ```sh
    yarn add @module-federation/nextjs-mf@0.0.1-beta.4
    yarn add @module-federation/nextjs-mf
    ```

    2. Add resolutions to `package.json`:
  4. jherr revised this gist Oct 28, 2020. No changes.
  5. jherr revised this gist Oct 28, 2020. 1 changed file with 12 additions and 2 deletions.
    14 changes: 12 additions & 2 deletions WP5-nextjs.md
    Original file line number Diff line number Diff line change
    @@ -25,7 +25,10 @@ yarn add @module-federation/[email protected]
    Remote:

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

    module.exports = {
    @@ -45,6 +48,8 @@ module.exports = {
    // Configures ModuleFederation and other Webpack properties
    withModuleFederation(config, options, mfConf);

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

    if (!isServer) {
    config.output.publicPath = "http://localhost:3000/_next/";
    }
    @@ -57,7 +62,10 @@ module.exports = {
    Consumer:

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

    module.exports = {
    @@ -83,6 +91,8 @@ module.exports = {
    // Configures ModuleFederation and other Webpack properties
    withModuleFederation(config, options, mfConf);

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

    if (!isServer) {
    config.output.publicPath = "http://localhost:3000/_next/";
    }
  6. jherr revised this gist Oct 28, 2020. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions WP5-nextjs.md
    Original file line number Diff line number Diff line change
    @@ -25,7 +25,7 @@ yarn add @module-federation/[email protected]
    Remote:

    ```js
    const { withModuleFederation } = require("@module-federation/nexjts-mf");
    const { withModuleFederation } = require("@module-federation/nextjs-mf");
    const path = require("path");

    module.exports = {
    @@ -57,7 +57,7 @@ module.exports = {
    Consumer:

    ```js
    const { withModuleFederation } = require("@module-federation/nexjts-mf");
    const { withModuleFederation } = require("@module-federation/nextjs-mf");
    const path = require("path");

    module.exports = {
    @@ -98,7 +98,7 @@ Remote:

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

    class MyDocument extends Document {
    static async getInitialProps(ctx) {
    @@ -127,7 +127,7 @@ Consumer:

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

    class MyDocument extends Document {
    static async getInitialProps(ctx) {
  7. jherr revised this gist Oct 28, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion WP5-nextjs.md
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    1. Install `@module-federation/nexjts-mf`.

    ```sh
    yarn add @module-federation/nexjts-mf
    yarn add @module-federation/nextjs-mf@0.0.1-beta.4
    ```

    2. Add resolutions to `package.json`:
  8. jherr created this gist Oct 28, 2020.
    163 changes: 163 additions & 0 deletions WP5-nextjs.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,163 @@
    1. Install `@module-federation/nexjts-mf`.

    ```sh
    yarn add @module-federation/nexjts-mf
    ```

    2. Add resolutions to `package.json`:

    ```json
    "resolutions": {
    "webpack": "5.1.3",
    "next": "9.5.5"
    },
    ```

    3. Update next version to canary.

    ```json
    "dependencies": {
    "next": "^9.5.6-canary.0",
    ```

    4. Create next.config.js.

    Remote:

    ```js
    const { withModuleFederation } = require("@module-federation/nexjts-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);

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

    return config;
    },
    };
    ```

    Consumer:

    ```js
    const { withModuleFederation } = require("@module-federation/nexjts-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);

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

    return config;
    },
    };
    ```

    6. Add `pages/_document.js`:

    Remote:

    ```js
    import Document, { Html, Head, Main, NextScript } from "next/document";
    import { patchSharing } from "@module-federation/nexjts-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:

    ```js
    import Document, { Html, Head, Main, NextScript } from "next/document";
    import { patchSharing } from "@module-federation/nexjts-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;
    ```

    7. import Nav

    ```js
    const Nav = (await import("test1/nav")).default;
    ```

    Use it per normal.