I’ve been working on dynamic import for the past couple of days, and this is an example of what I’ve just done,
A perfect example of dynamic import
Dynamic import of the specified route, For example: / user/center
const Routes = (props) => {
const staticRouter = [
<Route exact path="/" component={Home} key="s1"/>,
];
const [loading, setLoading] = useState(false);
const [routes, setRoutes] = useState(staticRouter);
const account = props.location.pathname === '/user/center';
const dynamicRouter = async () => {
setLoading(true);
if (account) {
const dynamic = await import('./event/dynamic');
const resultRouter = dynamic.Generate(props);
setRoutes([...staticRouter, ...resultRouter]);
} else {
setRoutes([...staticRouter]);
}
setLoading(false);
}
useEffect(async () => {
await dynamicRouter();
}, [account]);
if (loading) {
return <Loading/>
}
return (
<Layout>
<Switch>
{routes}
<Route component={NotFound} key='n1'/>
</Switch>
</Layout>
)
Dynamically import the contents of the routing file
./event/dynamic
import React from 'react';
import { Route } from 'react-router-dom';
import UCenter from '../../pages/UCenter';
export const Generate = props => {
return [
<Route path="/user/center" component={UCenter} {...props} key="d1"/>,
];
};
Refer to this post,go
See this post on how to tune meteor, go
Using bundle-visualizer I just managed to find a serious problem with react-icon, which made the packaged file shrink by 40% and reduced its speed by 1 second for the first opening and 600 milliseconds for the second opening