Lazy imports break fast refresh?
Hey there, nice job on this library! I'm finding however that thinks break in unexpected ways when using lazy components and webpack bundle splitting. My repo is private but as far as I can tell, fast refresh only works once and then blows up like so:
I am using react scripts 4.0 alpha for my fast refresh integration.
Okay, it wasn't the lazy import per se, but the fact that I was getting a lazy component twice like so in my Router:
{
exact: true,
guard: GuestGuard,
path: "/login",
component: lazy(() => import("src/views/auth/LoginView"))
},
{
exact: true,
path: "/login-unprotected",
component: lazy(() => import("src/views/auth/LoginView"))
},
But when I change it to this it works:
const Login = lazy(() => import("src/views/auth/LoginView"))
{
exact: true,
guard: GuestGuard,
path: "/login",
component: Login
},
{
exact: true,
path: "/login-unprotected",
component: Login
},
Not sure why this would break fast refresh though.
You are actually breaking webpack but not fast refresh per se - by having duplicated dynamic imports means that they would be triggered in runtime and when you change the route the module system would have its old value replaced, breaking object equality.
I'm not sure if this can be better supported by webpack though ...
Yeah, that makes sense. Doing it this way does seem quite common though, especially when coupled with React Router. Just google react router code splitting and a majority of the tutorials have inline lazy imports like I originally had.
This is only a practical problem with fast refresh though. With full page reloads, object equality will be maintained for the duration of the app lifecycle. So it's actually not clear to me whether any change should occur in webpack or here in fast reresh. Is there potentially some meta data about the component that webpack adds that could be used to detect duplicated lazy imports?
Yeah, that makes sense. Doing it this way does seem quite common though, especially when coupled with React Router. Just google
react router code splittingand a majority of the tutorials have inline lazy imports like I originally had.This is only a practical problem with fast refresh though. With full page reloads, object equality will be maintained for the duration of the app lifecycle. So it's actually not clear to me whether any change should occur in webpack or here in fast reresh. Is there potentially some meta data about the component that webpack adds that could be used to detect duplicated lazy imports?
Yea, with HMR the module system needs a bit more care since we would constantly remove and re-add stuff into it. Can you try checking the Webpack repo see if anyone have encountered this and file an issue there? My gut feeling is that this might have been solved in Webpack 5, but I was quite busy in life to follow everything that is happening ...
So I’m seeing an issue with lazy loaded components not being refreshed too.
However, if I import the same file twice and render both then both get refreshed:
Does not refresh:
const PageLazy = lazy(() => import('../page'));
Both refresh:
import Page from import('../page');
const PageLazy = lazy(() => import('../page'));
Not sure why this would break fast refresh though.
I actually think this breaks fast refresh because the lazy imported component is anonymous (i.e. there's no way we could statically determine that it is a React component). It is generally discouraged to have unnamed components (in this case, the lazy(...) call is considered a component).
So I’m seeing an issue with lazy loaded components not being refreshed too.
However, if I import the same file twice and render both then both get refreshed:
Does not refresh:
const PageLazy = lazy(() => import('../page'));Both refresh:
import Page from import('../page'); const PageLazy = lazy(() => import('../page'));
I need more information to be able to help. Can you please provide a reproducible example? I wouldn't expect usual lazy imports to have problems ...
@pmmmwh Thanks for your reply. After being unable to reproduce with a minimal example, I took another look and realised we had a HOC with an unnamed arrow function inside it, and that is breaking the updates.
const withErrorBoundary = (WrappedComponent) => (props) => (
<ErrorBoundary>
<WrappedComponent {...props} />
</ErrorBoundary>
);
Is this something that could be handled by this plugin, or does this HOC need a rewrite?
Is this something that could be handled by this plugin, or does this HOC need a rewrite?
You'll need to add a name to the HOC.
This is an unsupported case in terms of React Refresh - we do not make guesses and assumptions based on source to determine whether the code is React related.
That’s fair enough, thanks for the instant response 👍
Is there a workaround to have named functional components when using React.lazy?
In order for React Refresh to work now, I have to first import the components before using the React.lazy to export them:
if (process.env.NODE_ENV !== 'production') {
import('@components/auth/Register');
import('@components/auth/SingIn');
import('@components/Dashboard');
import('@components/Front');
}
export const Register = React.lazy(() =>
import('@components/auth/Register' /* webpackChunkName: "register" */)
);
export const SignIn = React.lazy(() =>
import('@components/auth/SingIn' /* webpackChunkName: "signin" */)
);
export const Dashboard = React.lazy(() =>
import('@components/Dashboard' /* webpackChunkName: "dashboard" */)
);
export const Front = React.lazy(() =>
import('@components/Front' /* webpackChunkName: "frontend" */)
);
It's working as this is done only in non production environments, but it would be nice to have a way to have lazy loaded components have an actual name so React Refresh Plugin can recognize them.
@clytras If you can provide a reproduction I can take a look at it. Feel free to open a new issue.
@pmmmwh Thank you for replying.
I tried to create a single CRA and reproduce this but I can't! For the problematic app, I was using [email protected] with React 17, but could not even fix it after switching to stable 0.4.x. There is definitely something wrong with my webpack and devServer settings as I'm configuring it using Laravel Mix and there are more options regarding Mix HMR.
I'm starting to believe that it's better to directly use the official CRA and bind it to a backend framework rather than using the provided React solution that Laravel provides as it's even an outdated 16.x older React version.
Thanks again.
I'll see if I can reproduce that using the latest versions of this plugin + react-refresh.
@pmmmwh there is nothing to reproduce here.
I could not reproduce it using the latest CRA which has [email protected] and [email protected] which use @pmmmwh/[email protected] and [email protected]. So, this issue is all about some missconfiguration. It's a Laravel Sail dev-env which kicks some Docker containers which then I use the browser to connect from the host machine. It has some custom configs because at the time I was building the project, Laravel didn't have Mix support for React 17 and Fast Refresh, but now in the current version it does have and it even installs this package along with react-refresh when requesting a hot run:

Which with the latest versions, the issue I'm describing above does not even happen using the Laravel Mix through Laravel Sail app running inside Docker containers.