Prefetching is one of the easiest ways to make your application’s UI feel a lot faster with Apollo Client. Prefetching simply means loading data into the cache before it needs to be rendered on the screen. Essentially, we want to load all data required for a view as soon as we can guess that a user will navigate to it.
In Apollo Client, prefetching is very simple and can be done by running a component’s query before it is rendered. As a simple example, in GitHunt, we use the
withApollo higher-order component to directly call a
query as soon as the user hovers over a link to the comments page. With the data prefetched, the comments page renders immediately, and the user often experiences no delay at all:
There are a lot of different ways to anticipate that the user will end up needing some data in the UI. In addition to using the hover state, here are some other places you can preload data:
- The next step of a multi-step wizard immediately
- The route of a call-to-action button
- All of the data for a sub-area of the application, to make navigating within that area instant
If you have some other ideas, please send a PR to this article, and maybe add some more code snippets. A special form of prefetching is store hydration from the server, so you might also consider hydrating more data than is actually needed for the first page load to make other interactions faster.