Sheraff / soft-serve-tunes

Self hosted music server on raspberry pi
7 stars 0 forks source link

Avoid flash of stale content when returning to the suggestion screen #52

Closed Sheraff closed 1 year ago

Sheraff commented 1 year ago

https://github.com/Sheraff/soft-serve-tunes/blob/c550d7a6b2006b93fedbe5ace23afb552940a265/src/components/Suggestions/index.tsx#L23-L29

By listening to sw-trpc-invalidation service worker messages, we could have the revalidateSwCache function resolve its promise only when invalidation is actually done (instead of what it is currently: resolve when its been delegated to the service worker but no request has been made yet), which would allow us to force a refetch of react-query. This should only be an option for revalidateSwCache because we usually don't need that (or maybe we do, check the other places where it's used).

The current situation is that


Even with this modification, there will still be a flash of stale content if the data is modified after this phase of revalidations. For example: we're listening to a playlist, so "recently listened artists" changes every song (which is at the top of the suggestions).

Maybe there should be a way to actually subscribe to a query being "always fresh"? Because I believe we're pretty reliable with cache invalidation and with keeping the service worker up to the freshest data (so maybe all that stuff I said above is not actually useful) and the issue if that when the service worker tells the client it has fresh data it only invalidates react-query instead of setting its data.

Is the solution to actually postMessage the data itself from the service worker so it can be passed to setQueryData?