Router Cache and RefreshProvider opinions
Unanswered
Chinese Egret posted this in #help-forum
Chinese EgretOP
Is it legit to wrap a client component with a RefreshProvider like the following, in order to refresh the Router Cache?
Do you see any weakness with this approach?
Thank you.
Do you see any weakness with this approach?
Thank you.
44 Replies
what exactly are you trying to do?
@riský what exactly are you trying to do?
Chinese EgretOP
Hi, I am trying to avoid Router Cache when loading data for a Server Component and without using
invalidateTag or invalidatePath, forcing the router.refresh.sorry, i still kinda dont get whats going on? you want every page link to load and then refetch for latest data?
@riský sorry, i still kinda dont get whats going on? you want every page link to load and then refetch for latest data?
Chinese EgretOP
Data retrieved with
getPazienteFull can be modified from API calls in other points of the application. So I want to be sure that when I land on PazientePage, fresh data are loaded and not cached.@Chinese Egret Data retrieved with `getPazienteFull` can be modified from API calls in other points of the application. So I want to be sure that when I land on `PazientePage`, fresh data are loaded and not cached.
I would use client side rendering with react-query/swr if I want to avoid the router cache.
if seo is important, you could fetch the initial data on the server and pass it to react-query/swr
if seo is important, you could fetch the initial data on the server and pass it to react-query/swr
@Ray I would use client side rendering with react-query/swr if I want to avoid the router cache.
if seo is important, you could fetch the initial data on the server and pass it to react-query/swr
Chinese EgretOP
😠so annoying I need to switch from server side rendering to client side rendering only because of a so aggressive caching strategy...
Britannia Petite
no don't switch client side, are you using fetch ?*
if you want to disable cache and you are using fetch just :
const res= await fetch("https://yourapi.com/whatever",{ cache: 'no-store' });@Britannia Petite no don't switch client side, are you using fetch ?*
Chinese EgretOP
No, i am not using
fetch, function getPazienteFull directly read from the database.Britannia Petite
Maybe you are lookin for unstable_cache
@Britannia Petite Maybe you are lookin for unstable_cache
Chinese EgretOP
yeah, terrible approach and syntax btw.
In addition:
In addition:
Warning: This API is unstable and may change in the future. We will provide
migration documentation and codemods, if needed, as this API stabilizes.@Britannia Petite Maybe you are lookin for unstable_cache
Chinese EgretOP
Anyway, it does not work. Cache keeps to be not revalidated.
revalidate: The number of seconds after which the cache should be revalidated. Omit or pass false to cache indefinitely or until matching revalidateTag() or revalidatePath() methods are called.Chinese EgretOP
Even
https://nextjs.org/docs/app/api-reference/functions/unstable_noStore
unstable_noStore does not work. PazientePage continues to be cached.https://nextjs.org/docs/app/api-reference/functions/unstable_noStore
Britannia Petite
which version of nexT.js are u using ?
@Britannia Petite which version of nexT.js are u using ?
Chinese EgretOP
"next": "^14.1.0"@Chinese Egret Even `unstable_noStore` does not work. `PazientePage` continues to be cached.
https://nextjs.org/docs/app/api-reference/functions/unstable_noStore
currently, the only way to bypass the router cache is use client side rendering or create a component like the one you did in the question to invalidate it on navigation
const router = useRouter()
const pathname = usePathname()
useEffect(() => {
router.refresh()
}, [pathname])@Britannia Petite if you want to disable cache and you are using fetch just :
js
const res= await fetch("https://yourapi.com/whatever",{ cache: 'no-store' });
this disable the data cache only but not router cache
@Ray currently, the only way to bypass the router cache is use client side rendering or create a component like the one you did in the question to invalidate it on navigation
ts
const router = useRouter()
const pathname = usePathname()
useEffect(() => {
router.refresh()
}, [pathname])
Chinese EgretOP
mmm your version is interesting 😀 Each time the
pathname changes, then router.refresh is triggered. is it right?@Ray and render it in layout.tsx
Chinese EgretOP
Great, it is working, thank you.
@Ray and render it in layout.tsx
Chinese EgretOP
the downside is that SSR (database access) is triggered multiple times. Perhaps not the best in terms of performance.
@Chinese Egret the downside is that SSR (database access) is triggered multiple times. Perhaps not the best in terms of performance.
that is the reason they create router cache
@Ray that is the reason they create router cache
Chinese EgretOP
I don't completely agree. I expect to render fresh data from database (without caching) every time I land on the page.
the best way, I would say is to render a piece of data which is highly dynamic on client, you don't need to render the full page on client
or use page router for that page until there is a way to config the router cache
@Ray or use page router for that page until there is a way to config the router cache
Chinese EgretOP
yes, infact
noStore on top of the Server Component should do the trick, but in my case it is not working.@Chinese Egret yes, infact `noStore` on top of the Server Component should do the trick, but in my case it is not working.
noStore do nothing to router cache
it just tell next not to static generate
@Ray currently, the only way to bypass the router cache is use client side rendering or create a component like the one you did in the question to invalidate it on navigation
ts
const router = useRouter()
const pathname = usePathname()
useEffect(() => {
router.refresh()
}, [pathname])
Chinese EgretOP
I think I am going to switch to client side rendering, it's a nightmare ðŸ˜
maybe you could try out trpc with create-t3-app
it bootstrap a project which can do server rendering and client rendering
@Ray maybe you could try out trpc with create-t3-app
Chinese EgretOP
I was about to ask an offtopic question: a good typesafe api's mechanism.
yeah the setup in create-t3-app is very good with trpc
I would highly recommend you create a project and play with it
with create-t3-app
Chinese EgretOP
ok, thank you all for the great help, I really appreciated
you're welcome
Britannia Petite
mmh ok thought you were talking about data cache per route, my bad