How can i make it fetch once every session
Answered
Persian posted this in #help-forum
PersianOP
Hi im fetching discord api to get the user's guilds
but it's fetching every refresh or every time the page opens so i want to make it fetch once every session so i don't get api spam
but it's fetching every refresh or every time the page opens so i want to make it fetch once every session so i don't get api spam
Answered by alfon
const { data: guildData } = useQuery({
queryKey: ['user', userid, 'discordguilds'],
queryFn: () => fetch('...'),
staleTime: 1000 * 300 or match it with session expiration duration
})47 Replies
@Persian Hi im fetching discord api to get the user's guilds
but it's fetching every refresh or every time the page opens so i want to make it fetch once every session so i don't get api spam
the refetch on window focus is on the SessionProvider props
@alfon the refetch on window focus is on the SessionProvider props
PersianOP
more details please?
@alfon https://next-auth.js.org/getting-started/client#options
PersianOP
i don't understand what does this belongs to fetching data?
PersianOP
i want it to fetch once and don't fetch again except if the session changed
you can use the fetch() once on the jwt/session callback
using the trigger parameter to only detect when user signIn
PersianOP
can u send the docs for it?
PersianOP
but it's not about the session
im creating function that runs when a specefic page loads
but you said "fetch every session so i don't get api spam"
PersianOP
and every time it loads the function fetch the data
i see well maybe you can use a react-query to cache the data for x period of time
@alfon but you said "fetch every session so i don't get api spam"
PersianOP
oh sorry my bad
@alfon i see well maybe you can use a react-query to cache the data for x period of time
PersianOP
How can i do that?
const { data: guildData } = useQuery({
queryKey: ['user', userid, 'discordguilds'],
queryFn: () => fetch('...'),
staleTime: 1000 * 300 or match it with session expiration duration
})Answer
or you know
just store it in the session object :/ at user signin
PersianOP
i did once
but it's getting to large and sometimes the page stops and don't work except if i deleted the session from my browser
PersianOP
and what's that?
PersianOP
Okay ima use it
can i ask another question?
dont ask to ask :/
PersianOP
k :/
how can i make that every session get cleared when the server starts
can't do that automatically
either use db strategy or change NEXTAUTH_SECRET every time you start server
you can't log user out from the server if ure using jwt. its one of the disadvantage of using jwt
PersianOP
what other disadvantage does jwt have?
more attack vector?
you can ask gpt for that lmao
PersianOP
It's not available in my country
PersianOP
k thanks