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
we revalidate the service worker cache (so latest data is available)
service worker posts a message to main client, which invalidates the react-query cache
when we return to suggestion screen, react query serves up the stale data for a time while re-requesting the data from the service worker
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?
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 therevalidateSwCache
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 forrevalidateSwCache
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 tosetQueryData
?