Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get "React refresh runtime was loaded twice." on the micro-frontend project #87

Closed
dnshi opened this issue Apr 18, 2023 · 2 comments
Closed

Comments

@dnshi
Copy link

dnshi commented Apr 18, 2023

Background

I am working on a micro-frontend project that a root container fetches and renders some applications' components, which are served on different vite dev servers (the applications are owned by different teams and have their own vite config).

To sync between the react refresh instance, I used RefreshRuntime.injectIntoGlobalHook(window); to share the state. But after I upgraded the version of vite-plugin-react-swc , with this change, e272817, I got the following message:

Error: React refresh runtime was loaded twice. Maybe you forgot the base path?
    at http://localhost:3001/@react-refresh:368:9

I understand the purpose of throwing the error, so want to ask if you have any suggestion to handle my use case? Maybe add a flag to detect whether injectIntoGlobalHook is used before throwing the error message? Thanks.

@ArnaudBarre
Copy link
Member

ArnaudBarre commented Apr 22, 2023

To sync between the react refresh instance, I used RefreshRuntime.injectIntoGlobalHook(window); to share the state.

Not sure I get this, for me this "preamble code" should run also exactly once (per window).

Your setup is kind of particular. If code happens to run on the page at this end I think this should be served by the same dev server. You could still have something like /src/app1, src/app2 with specific app code and each once has its own React root container and lifecycle.
How so you handle the fact that React is loaded only once?

Without more context, my advice would be to chose one dev server that owns the responsibility for injecting preamble and serving the runtime code and add a plugin in others vite dev server that rewrite the /@react-refresh import (after the plugin has transformed it) to be the one served by the main dev server

@dnshi
Copy link
Author

dnshi commented Apr 24, 2023

Thanks for the advice! I am going to update my code and rewrite the /@react-refresh import.

@dnshi dnshi closed this as completed Apr 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants