Refresh Route Data API #1996
Replies: 13 comments 21 replies
-
Just to be sure, the idea is that because this hook has its own state and type it will not cause a change in useTransition right? So the useTransition values will still be tied to the global transition and this as with useFetcher will have another state right? If that’s the case I think it makes sense since it’s not really transitioning anywhere. I like the API, simple, not too much to comment, it does what I would expect. |
Beta Was this translation helpful? Give feedback.
-
What will happen if the device is offline/network fail when |
Beta Was this translation helpful? Give feedback.
-
Will this feature let me target a specific loader to re-fetch in the case of nested layouts? Or is meant to be a true 1:1 functionality replacement for |
Beta Was this translation helpful? Give feedback.
-
Another question about this, will the state be global? If I call useDataRefresh on two components and one call refresh, should the other one also be able to see the state change? I think it should, this will also allow apps to have a single UI to show the refreshing state and call refresh from another place. |
Beta Was this translation helpful? Give feedback.
-
What about navigation interruptions? There's a potential conflict between forced loader re-runs via For example:
A couple quick options off the top of my head:
I think I might like option 2? |
Beta Was this translation helpful? Give feedback.
-
Would polling look something like this: import { useState, useEffect, useRef } from "react";
import { useDataRefresh } from "remix";
import { LikeSymbol } from "./like-symbol";
function LikeButton() {
let { refresh, state } = useDataRefresh();
useInterval(refresh, 5_000);
return (
<button>
<LikeSymbol count={state.count} />
</button>
);
}
function useInterval(callback, delay) {
const savedCallback = useRef();
useEffect(() => {
savedCallback.current = callback;
}, [callback]);
useEffect(() => {
function tick() {
savedCallback.current();
}
if (delay !== null) {
let id = setInterval(tick, delay);
return () => clearInterval(id);
}
}, [delay]);
} ? |
Beta Was this translation helpful? Give feedback.
-
I'm looking into switching to Remix from NextJS+ReactQuery. I have just such a use case and ReactQuery does refetch on focus by default. I like Ken's idea of a useDataRefresh hook combined with useBroadcastChannel. Have you given this some more thought? Any chance something like this will be added? |
Beta Was this translation helpful? Give feedback.
-
I'm making a PWA that serves data from the service worker's cache and then update the cache. When the cache is updated, I use a BroadcastChannel to let the app know it needs to revalidate the loader data. Will the revalidator send a special header to let the loader know this is a data revalidation request? For my use case this would be very helpful so that I could make the service worker immediately respond with the updated response it cached after it had served the initial request. Right now it revalidates every loader on the page and I don't know if I would prefer to revalidate only some specific loaders or every loader on the page. Anyway, that's what the flow looks like if it's supposed to revalidate only one loader: |
Beta Was this translation helpful? Give feedback.
-
I'm trying to switch to Remix from NextJS but we may be unable to without this piece. I quite like like Remix so would be happy to juggle a workaround in the interim but after searching quite extensively I can't find any vetted ones. I thought here would be the place to figure out if there is one, but lmk if you'd prefer a separate discussion. Let me quickly describe our site, the use-case may also help inform the design. Our site has a few pages but its main page is a dashboard. This dashboard hosts various widgets, some interactive, although none cause navigation, or even submission of anything. It's just retrieving data in various ways. The data updates constantly. The most volatile data changes every 2s or so. If you'd like to see the site: https://ultrasound.money/ Approaches I've considered:
This part worked perfectly with Next's Reading the proposal, this would be a step in the right direction, but still wouldn't suffice. It has the refresh, but still lacks a way to selectively refresh pieces of data at different intervals. Curious to hear what you think, and if this is even a type of site Remix expects to handle well. Thanks for a great lib! 💛 |
Beta Was this translation helpful? Give feedback.
-
Also exploring the option to switch from NextJS + React Query to Remix. Same use case, a dashboard that polls data on a regular interval (45 sec), and receives events with WebSocket, and events also trigger data update. |
Beta Was this translation helpful? Give feedback.
-
On the other side of the coin, is there any API to not refetch a particular route after an action? We have some pretty expensive queries at the root level, for example, that aren't likely to change on most actions. |
Beta Was this translation helpful? Give feedback.
-
Apologies to bump an old thread, but is there any update on this? This is crucial for us as we get messages from WebSockets and need to refresh. If it's been released already, I probably missed it lol |
Beta Was this translation helpful? Give feedback.
-
Now that Remix is on the latest React Router it has access to |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
It has become a common expectation in the React ecosystem that data fetching libraries have the ability to arbitrarily refresh their data at any time. It's most often used to revalidate the data on the page after a mutation is performed.
This isn't often needed in Remix since it automatically handles data revalidation after actions for you. However, there are several other cases where it would be useful to reload the route data on the page.
This is possible today but with unacceptable side effects:
Many people will
navigate
to exactly where the user is at to trigger a reload of all routes:This is bad because it will slice off the last entries of the history stack if the user isn't at the end of it (wipes out the "forward" button).
A better option is to submit a fetcher to get the routes to reload:
This is better because it doesn't mess with the history stack, but it's silly because it sends off a pointless
POST
first, wasting resources and slowing down the process. All we want is the reload of routes afterward.Since there are valid use cases, and it's already possible but with negative side effects, I'm proposing a new API here to add direct support for refreshing route data.
Proposal
The API is a mini
useFetcher
.I'd expect it to be used pretty simply:
You could add a global spinner to indicate the refresh:
Cross tab communication with BroadcastChannel:
Behavior
The revalidation fetches should be grouped with the
useFetcher
revalidations so that refreshes and fetcher revalidations will abort each other when reconciling race conditions.Thoughts?
Beta Was this translation helpful? Give feedback.
All reactions