NextJS Microfrontend(MFE) - 解决不正确的揭示

发布于 2025-01-28 11:19:14 字数 2821 浏览 3 评论 0 原文

我一直在NextJS MFE上关注博客。但是,我遇到了一个死胡同,并希望提供一些帮助。

我有2个应用程序 - 一个 shell 和其他排名 - app

shell应用程序的 next.config.js

/**
 * @type {import('next').NextConfig}
 */
const nextConfig = {
  /* config options here */
  webpack: (config, options) => {
    const { ModuleFederationPlugin } = options.webpack.container;

    config.plugins.push(
      new ModuleFederationPlugin({
        name: 'webapp',
        remotes: {
          'rankedApp': 'rankedApp@http://localhost:3502/_next/static/chunks/remoteEntry.js'
        }
      })
    );

    config.cache = false;

    return config;
  }
};

module.exports = nextConfig;

但是,当我尝试从 canded

/**
 * @type {import('next').NextConfig}
 */
const nextConfig = {
  /* config options here */
  webpack: (config, options) => {
    const { ModuleFederationPlugin } = options.webpack.container;

    config.plugins.push(
      new ModuleFederationPlugin({
        name: 'rankedApp',
        library: {
          type: 'var',
          name: 'rankedApp'
        },
        filename: 'static/chunks/remoteEntry.js',
        exposes: {
          './Example': './src/components/Example'
        },
        remotes: {},
        shared: []
      })
    );

    config.cache = false;

    config.output.publicPath = 'http://localhost:3502/_next/';

    return config;
  }
};

module.exports = nextConfig;

-app 中导入示例 component时,排名app 下一个配置代码>从 http:// localhost:3502 加载,但是块请求 src_component_example_tsx.js 是相对于 http:// localhost:3501 代码>有趣的是,它忽略了 publicPath 排名app 中。我出错的任何指针都可以很高兴看到。

”在此处输入图像说明”

import React, { FC, Fragment } from "react";
import dynamic from 'next/dynamic';

const RankedAppExample = dynamic(
  async () => {
    return await import("rankedApp/Example");
  },
  {
    ssr: false,
    loading: ({ error }) => {
      if (error) {
        return <Fragment>Error</Fragment>;
      }
      return <Fragment>Loading</Fragment>;
    }
  }
);

interface HomeProps {}

const Home: FC<HomeProps> = (props: HomeProps) => {
  return <Fragment>Home <RankedAppExample /></Fragment>;
};

export default Home;

I have been following blog on NextJS MFE. However I have hit a dead end and would like some help.

https://thekevinwang.com/2021/03/26/micro-frontends-nextjs/

I have 2 apps - one shell and other ranked-app

The shell app's next.config.js is

/**
 * @type {import('next').NextConfig}
 */
const nextConfig = {
  /* config options here */
  webpack: (config, options) => {
    const { ModuleFederationPlugin } = options.webpack.container;

    config.plugins.push(
      new ModuleFederationPlugin({
        name: 'webapp',
        remotes: {
          'rankedApp': 'rankedApp@http://localhost:3502/_next/static/chunks/remoteEntry.js'
        }
      })
    );

    config.cache = false;

    return config;
  }
};

module.exports = nextConfig;

and the ranked-app next config is

/**
 * @type {import('next').NextConfig}
 */
const nextConfig = {
  /* config options here */
  webpack: (config, options) => {
    const { ModuleFederationPlugin } = options.webpack.container;

    config.plugins.push(
      new ModuleFederationPlugin({
        name: 'rankedApp',
        library: {
          type: 'var',
          name: 'rankedApp'
        },
        filename: 'static/chunks/remoteEntry.js',
        exposes: {
          './Example': './src/components/Example'
        },
        remotes: {},
        shared: []
      })
    );

    config.cache = false;

    config.output.publicPath = 'http://localhost:3502/_next/';

    return config;
  }
};

module.exports = nextConfig;

However, when I attempt to import the Example component from ranked-app, the remoteEntry gets loaded from http://localhost:3502, but the request for chunk src_component_Example_tsx.js is relative to http://localhost:3501 interesting it ignores publicPath in ranked-app. Any pointers where I am going wrong could be nice to see.

enter image description here

enter image description here

import React, { FC, Fragment } from "react";
import dynamic from 'next/dynamic';

const RankedAppExample = dynamic(
  async () => {
    return await import("rankedApp/Example");
  },
  {
    ssr: false,
    loading: ({ error }) => {
      if (error) {
        return <Fragment>Error</Fragment>;
      }
      return <Fragment>Loading</Fragment>;
    }
  }
);

interface HomeProps {}

const Home: FC<HomeProps> = (props: HomeProps) => {
  return <Fragment>Home <RankedAppExample /></Fragment>;
};

export default Home;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文