Gatsby Module Federation CORS error and eager consumption issue
Asked Answered
B

2

8

I am trying to use micro frontend app from gatsby using Module Federation. When I try to call the component which is running at localhost:3001, getting CORS error. any idea how to solve this?

enter image description here

Here is my gatsby-node.js


const packageJsonDeps = require('./package.json').dependencies;
const { ModuleFederationPlugin } = require("webpack").container;

exports.onCreateWebpackConfig = ({
                                     stage,
                                     rules,
                                     loaders,
                                     plugins,
                                     actions,
                                 }) => {
    actions.setWebpackConfig({
        plugins: [
            new ModuleFederationPlugin( {
                name: "app2",
                remotes: {
                    app1: "app1@http://localhost:3001/remoteEntry.js",
                },
                shared: {
                    ...packageJsonDeps,
                    react: { singleton: true, eager: true, requiredVersion: packageJsonDeps.react },
                    "react-dom": { singleton: true, eager: true, requiredVersion: packageJsonDeps["react-dom"] },
                    "styled-components": { singleton: true, eager: true, requiredVersion: packageJsonDeps["styled-components"] },
                    "gatsby":{singleton: true, eager: true, requiredVersion: packageJsonDeps["gatsby"]}
                },
            })
        ]
    })
} 

App2 idex.js

import Header from "app1/Header";
const data = "App2 data";

export default () => (

    <div style={{margin: '20px'}}>
        <React.Suspense fallback='Loading app2 data'>
            <Header data={ data } />
        </React.Suspense>
    </div>
);

Any help appreciated.Thanks

Blackbird answered 31/7, 2021 at 5:38 Comment(0)
E
3

Try to add in your webpack.config.js:

devServer: {
 ...
 headers: {
  "Access-Control-Allow-Origin": "*"
 }
}
Earmuff answered 15/12, 2021 at 12:48 Comment(2)
Thanks Eduard.. tried, but still sameBlackbird
Hello, did u find a solution for this problem?Brilliance
B
0

Following Eduard Klinger answer, did you try ? :

devServer: {
      ...,
      headers: {
        "Access-Control-Allow-Origin": "*",
        "Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, PATCH, OPTIONS",
        "Access-Control-Allow-Headers": "X-Requested-With, content-type, Authorization"
      }
    },
Beecher answered 20/6, 2022 at 16:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.