Preloading queries in Relay — optimise your app without React Suspense
As we explained on a previous blog, we’re big fans of Relay here at Finimize. It allows us to easily fetch, cache and distribute data for our mobile app screens.
There are some exciting features that are currently in Relay Experimental mode. Generally these take advantage of React’s experimental Concurrency and Suspense modes, and they present some exciting new possibilities for better patterns in web and mobile apps.
One of these patterns proposed by React Suspense is “render as you fetch”.
In this blog, we’ll show you how we implemented this using existing production-ready Relay tools, without the (currently) experimental preloadQuery.
But first….what does this allow us to do?
Up until now, Relay (and most data fetching frameworks) will fetch the data for a screen (or page) when you start to render that screen. This means the time taken to render each screen can be illustrated as:
With preloadQuery, we can start fetching the query when it looks like we might enter that screen, or at the start of our app. This means that when we actually render it, the query will be at least partially fetched — reducing overall time:
Some exciting possibilities for using preload query include:
- prefetching the data for an app home screen as soon as the app boots up (our use-case)
- firing the query on button click (or when we enter a react-navigation nav route), so it can fetch while we animate the transition to a screen
- prefetching the data for a web-app onMouseEnter of a button (next.js offers something similar here)
So — how can we do it?
It turns out we can achieve something similar to preloadQuery with any version of Relay. This will allow you to optimise now - without waiting for React Suspense to become stable. See the function below.
Using this function, you can now imperatively call a particular query from anywhere in your codebase, and have the data available everywhere! ✨
We then like to export our query from our component file, and use a query renderer as follows.
So then we can prefetch the data as follows, before our home screen.
Notice that we’ve used useStoreOrNetwork in the first example. This maps to the store-or-network relay fetch policy. This will show what’s in the store (i.e. what we fetched earlier) if it’s available, or else it’ll fetch the data we need.
We could even save this extra query by using the cache-only option from the react-relay-offline library.
Hopefully now you’ve seen how you can work with Relay to improve your data-fetching — and that you don’t need to wait for React’s Concurrency and Suspense modes to become stable to reap the rewards. Thanks for checking us out — and stay tuned for more exciting engineering blog posts, straight from the Finimize HQ!
🎧 Audio more your thing? Always thought about building that podcasting app for dogs? 🐕
📻 Check out our other blog post around how to build a global media player in React Native — and keep those dogs happy!