question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using NextJS and Federated Web Components

See original GitHub issue

So Im trying to use Federated Modules to bring in a Vanilla Web Component (Im using lit-html for the rendering piece otherwise nothing else) into a NextJS application. Im attempting to follow the example here https://github.com/module-federation/module-federation-examples/tree/master/nextjs-ssr using the @telenko/node-mf plugin but so far I have not gotten this to work. Here is the next.config.js:


const packageJsonDeps = require("./package.json").dependencies;
const { NodeModuleFederation } = require("@telenko/node-mf");
const ModuleFederationPlugin = require("webpack/lib/container/ModuleFederationPlugin");

module.exports = {
  future: { webpack5: true },
  webpack: (config, { isServer }) => {
    const mfConf = {
      remotes: {
        create_cc: "snap_payment_create_cc_v1@http://localhost:5009/payment/create_cc/v1/remoteEntry.js"
      },
      shared: {
        react: {
          eager: true,
          requiredVersion: packageJsonDeps["react"],
          singleton: true,
        },
        "react-dom": {
          eager: true,
          requiredVersion: packageJsonDeps["react-dom"],
          singleton: true,
        },
      },
    };
    return {
      ...config,
      plugins: [
        ...config.plugins,
        new (isServer ? NodeModuleFederation : ModuleFederationPlugin)(mfConf),
      ],
      experiments: { topLevelAwait: true },
    };
  },

  webpackDevMiddleware: (config) => {
    // Perform customizations to webpack dev middleware config
    // Important: return the modified config
    return config;
  },
};

The web component is self defining so I would expect that I could bring it in just for the side effects like so:


await import("snap_payment_create_cc_v1/payment-create_cc")


const PaymentPaymentWrapper = async ({content}) => {
    console.log("TESTTSLKDJSLFKSJD", Test)

    return (
        <>
            <h3>{content.title}</h3>
            <div id="paymentCreation">
                <h3>{content.createMethodTitle}</h3>
                <payment-create_cc></payment-create_cc>
            </div>
        </>
    )
}

export default PaymentPaymentWrapper

but I get errors around using top-level-await (Error: error: top level await requires target to es2017 or higher and topLevelAwait:true for ecmascript) that I’m really struggling to solve and if I don’t use await on it then I get errors around resolving the module (Can’t resolve ‘snap_payment_create_cc_v1/payment-create_cc’). Any thoughts on what I could be doing wrong

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
ScriptedAlchemycommented, Dec 28, 2021

Next doesn’t play nice with the import syntax. Use low level api and probably need the paid plugin for it to have a chance of working

Rust compiler requires layers experiment as well to get top-level await

U can switch back to babel tho if needed

0reactions
ScriptedAlchemycommented, Jan 29, 2022

Also you can use next 12 and just keep a babelrc file. If Babel config is found SWC isn’t used

Read more comments on GitHub >

github_iconTop Results From Across the Web

Build Micro frontends in NextJS and ReactJS with Webpack 5 ...
Solutions for Micro Frontends · Basic implementation: Web components with a server-side include · Server-side rendering: Holocron, Podium, and Ara ...
Read more >
Building React App with Module Federation and NextJS/React
This document will take you step-by-step through the tasks required to set up a module federation module, with react app as host with...
Read more >
Nx, Next.js, and Module Federation - Valor Software
Module Federation is a Webpack 5 feature that has arrived to make it possible to share parts of an application to another at...
Read more >
Deploying micro frontends with module federation and Next.js
Module Federation is a way of enabling teams to build and deploy applications independently. Here is an example of using it for Next.js...
Read more >
Next.js 11, Module Federation, and SSR — A brave new world
Federation enables us to make Next.js highly modular. You can develop a MegaNav that can be deployed independently of the rest of your...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found