react-refresh-webpack-plugin icon indicating copy to clipboard operation
react-refresh-webpack-plugin copied to clipboard

Lazy imports break fast refresh?

Open scottmas opened this issue 6 years ago • 14 comments

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:

Screen Shot 2020-09-29 at 5 12 34 PM

I am using react scripts 4.0 alpha for my fast refresh integration.

scottmas avatar Sep 29 '20 23:09 scottmas

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.

scottmas avatar Sep 30 '20 00:09 scottmas

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 ...

pmmmwh avatar Oct 01 '20 13:10 pmmmwh

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?

scottmas avatar Oct 01 '20 16:10 scottmas

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?

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 ...

pmmmwh avatar Oct 03 '20 16:10 pmmmwh

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'));

mbalaam avatar Nov 26 '20 12:11 mbalaam

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 avatar Nov 30 '20 00:11 pmmmwh

@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?

mbalaam avatar Dec 02 '20 10:12 mbalaam

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.

pmmmwh avatar Dec 02 '20 10:12 pmmmwh

That’s fair enough, thanks for the instant response 👍

mbalaam avatar Dec 02 '20 10:12 mbalaam

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 avatar Apr 26 '21 10:04 clytras

@clytras If you can provide a reproduction I can take a look at it. Feel free to open a new issue.

pmmmwh avatar Apr 26 '21 18:04 pmmmwh

@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.

clytras avatar Apr 26 '21 20:04 clytras

I'll see if I can reproduce that using the latest versions of this plugin + react-refresh.

pmmmwh avatar Apr 27 '21 07:04 pmmmwh

@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:

Laravel Mix Run Hot

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.

clytras avatar Apr 27 '21 09:04 clytras