Prefetch lazy-loaded component
Cory House has posted following tweet today.
TIL you can prefetch lazy loaded React components.🔥— Cory House (@housecor) November 29, 2019
create-react-app honors Webpack directives: https://t.co/lgHo6GomQ6
So, to lazy load a component, but prefetch it behind the scenes:
const Hi = React.lazy(() =>
import(/* webpackPrefetch: true */ "./Hi")
Cory has pointed out a feature in CRA to enable prefetching lazy component, but I learned of a way to apply it on a project.
Suppose that you are building a customer intake site.
When a user receives a call, the user can quickly load up the initial customer page
because the site is small because non-essential code is split (lazy loaded).
Validating user (to confirm that the user has the correct customer's page on) will be the idle time to prefetch the lazy component such as tabs to fill in the customer's information, and modal confirmation boxes, etc.
The typical workflow will be,
- User searches for the customer
- Customer page is loaded fast (initial site is small due to code split)
- User confirms with customer if the right page is loaded (providing idle time for prefetch)
- During the idle time, non-essential (not needed right away on load) such as modal or forms in tabs are loaded.
- User can gather data from customer, and enter it quickly.
So this would especially be effective for LOB (line of business) apps with lots of data to enter or look up.
Ones I can think of are,
- Intake screen - where a user confirms the right customer page, and during that time, other forms/modals or non-essential but needed components are loaded.
- Dashboard - where graphs outside the viewport is lazily but prefetched (user analyzing the top graph will give enough time for other graphs outside the viewport to be prefetched during that time)
- Any Master-detail pages - e.g.) A site with lots of image with modal for details such as
- Nav menu - Top level menu is loaded right away, and menu items shown on hover are loaded lazy but prefetched.