I created my app with create-react-app
.
In my website, I used lazy loading for page components :
import HomePage from '~/containers/pages/HomePage/Loadable';
import RestaurantPage from '~/containers/pages/RestaurantPage/Loadable';
// other routes
const RoutesList = ({ history }) => {
history.listen(() => {
window.scrollTo(0, 0);
});
return (
<DefaultLayout history={history}>
<Switch>
<Route path="/restaurant/:slug" component={RestaurantPage} />
// other routes
<Route exact path="/" component={HomePage} />
<Route component={ErrorPage} statusCode="404" />
</Switch>
</DefaultLayout>
);
};
And my Loadable component looks like this :
import React from 'react';
import LoadingIndicator from '~/components/common/LoadingIndicator';
import loadable from '~/utils/loadable';
export default loadable(() => import('./index'), {
fallback: <LoadingIndicator />,
});
With this for lazy component :
/* eslint-disable react/jsx-props-no-spreading */
import React, { lazy, Suspense } from 'react';
const loadable = (importFunc, { fallback = null } = { fallback: null }) => {
const LazyComponent = lazy(importFunc);
return (props) => (
<Suspense fallback={fallback}>
<LazyComponent {...props} />
</Suspense>
);
};
export default loadable;
So my problem is, the code splitting works fine in firefox. When clicking a link in a the menu, the separated chunk files are loading only when accessing the new page.
But in chrome, every chunk files are loaded.