zth / rescript-relay-router

MIT License
16 stars 3 forks source link

Re-evaluate data on all navigations #64

Closed zth closed 2 years ago

zth commented 2 years ago

Fixes https://github.com/zth/rescript-relay-router/issues/39

This ensures that all route segments are potentially refreshed on each navigation. There's a lot more detail in the comments of the code itself, but a TLDR;

Problem

Relay has a powerful invalidation feature, where you can invalidate individual records in the store. This invalidation means that the next time any query relying on that data are evaluated, Relay will re-do the query if it finds stale data.

Together with suspense and concurrent mode, this gives a pretty powerful feature where you can invalidate and reload data for various things without disturbing the user, since you can stuff it all in a useTransition and do the full update "off screen". No spinners etc. Think things like "invalidate this whole organization because mutation X did something that I can't reasonably update locally, so please refetch anything that has to do with organization X the next time you evaluate it".

When preloading queries (like we do via prepare in the router), re-evaluating means calling Query.load again. Calling loadQuery is only done when preparing a route.

The fix

Previously, we were never re-calling prepare for a route that had already mounted. This PR changes that, so that we always call prepare again when we prepare a route with the intention of also rendering it. This makes it so that prepare is re-run for all route segments for every navigation. And that gives Relay the possibility to re-evaluate and potentially refetch stale data.