【发布时间】:2020-11-07 00:45:33
【问题描述】:
我使用create-react-app 创建了我的应用程序。
在我的website 中,我对页面组件使用了延迟加载:
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>
);
};
我的可加载组件看起来像这样:
import React from 'react';
import LoadingIndicator from '~/components/common/LoadingIndicator';
import loadable from '~/utils/loadable';
export default loadable(() => import('./index'), {
fallback: <LoadingIndicator />,
});
有了这个懒组件:
/* 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;
所以我的问题是,代码拆分在 Firefox 中运行良好。单击菜单中的链接时,仅在访问新页面时才加载分隔的块文件。
但在 chrome 中,每个块文件都会被加载。
【问题讨论】:
标签: reactjs webpack create-react-app code-splitting craco