I have a 2 step Application Flow that looks like this:
const Step1 = React.lazy(() => import('./Step1'));
const Step1 = React.lazy(() => import('./Step2'));
<Suspense fallback={<Loading />}>
<Route path="/step1" render={() => <Step1 />} />
<Route path="/step2" render={() => <Step2 />} />
</Suspense>
Using React.lazy, I can defer loading <Step2 />
while the user is on <Step1 />
, which can improve initial page load. However, I would like to prefetch <Step2 />
while the user is on <Step1 />
as an optimization. Is there an API to do this with React.lazy?
Edit:
To elaborate - I'm using a router to render a 2 step form. Initially the user will start on /step1
. After the user completes all the tasks in <Step1 />
they will be routed to path /step2
. At this point the router will render the <Step2 />
component.
I'm asking if there is a pattern to pre-fetch <Step2 />
while the user is still on <Step1 />
.
Link
or other methods at that time the other file will be loaded i.e. in route based splitting the file is loaded based on the route. You can try this by simpling having the above code and in the component, you can render10000
items of array by simply looping over them and you can then see the difference. – Cannreact-router
. Do follow this thread to get most out of the conversation. Link: github.com/facebook/react/pull/17245 – Cann