Using SWR with NextAuth
Unanswered
Montenegrin Mountain Hound posted this in #help-forum
Montenegrin Mountain HoundOP
Hi all,
I have an app that I am building which uses NextAuth (JWT, as it's credential based). I have a dashboard table that I want to use SWR with, so results are cached (it's entries added by that user).
From what I understand, these results would be cached and shown to all users. If so, is it possible to make it so all requests are cached for that user?
As well as that, possible to toggle between? Thanks
I have an app that I am building which uses NextAuth (JWT, as it's credential based). I have a dashboard table that I want to use SWR with, so results are cached (it's entries added by that user).
From what I understand, these results would be cached and shown to all users. If so, is it possible to make it so all requests are cached for that user?
As well as that, possible to toggle between? Thanks
15 Replies
Chub mackerel
SWR would only cache for the specific user
Montenegrin Mountain HoundOP
Oh it does? I was worried it was a global cache, as in, all users get the same result
As online it says its shared, so I've been confused
Chub mackerel
share the specific links/paragraphs you're talking about. unless you're using specific nextjs server functionality then it can't cache it for all users
Montenegrin Mountain HoundOP
Ahh thank you, weirdly using the default
new Map for cache provider, if I refresh the site it has to load the data again. Is this normal? I thought I could set a delay to use new dataChub mackerel
if you refresh the page it has no way to cache the data. it's not using localstorage or anything
but also it's likely not a problem that it does reload when page loads. that's what you want
when they say it caches responses it means if you call
api/me twice itll only make an api call onceMontenegrin Mountain HoundOP
Ah I see, so it "caches" while you're on the page? So if you switch pages, or you've got a background task. Then after you refresh its gone
Ah just tested, wow thats cool, so flicking between pages it keeps the data loaded with SWR, but with axios it loads each time
Chub mackerel
axios is just fetch()
swr is basically:
pseudo code but that's the idea. if we have the result just return what we have
swr is basically:
result = undefined
if (result) return result
result = useswr()
return resultpseudo code but that's the idea. if we have the result just return what we have
Montenegrin Mountain HoundOP
Ah yeah, that makes sense thank you.
So SWR always provides instant data to the client essentially? I see that it hits my endpoint each time, I assume that it never updates the frontend unless its seen as "stale" right? So the API request is still made each time
So SWR always provides instant data to the client essentially? I see that it hits my endpoint each time, I assume that it never updates the frontend unless its seen as "stale" right? So the API request is still made each time
Montenegrin Mountain HoundOP
Ah interesting, thanks for sharing - would this be recommended to leave “as-is�
If I hit the dashboard (table data loads) -> click “add†-> then click “back to dashboardâ€
That it would be sending an api request to grab the data again. In which I assume no re-render is done because it’s not “stale†right?
If I hit the dashboard (table data loads) -> click “add†-> then click “back to dashboardâ€
That it would be sending an api request to grab the data again. In which I assume no re-render is done because it’s not “stale†right?