
If a user leaves your application and returns to stale data, Svelte Query automatically requests fresh data for you in the background. You can disable this globally or per-query using the refetchOnWindowFocus option:
<script>// Configure for all queriesimport { QueryClient, QueryClientProvider } from '@sveltestack/svelte-query'const queryClient = new QueryClient({defaultOptions: {queries: {refetchOnWindowFocus: false,},},})</script><QueryClientProvider client={queryClient}>...</QueryClientProvider>
useQuery('todos', fetchTodos, { refetchOnWindowFocus: false })
In rare circumstances, you may want to manage your own window focus events that trigger Svelte Query to revalidate. To do this, Svelte Query provides a focusManager.setEventListener function that supplies you the callback that should be fired when the window is focused and allows you to set up your own events. When calling focusManager.setEventListener, the previously set handler is removed (which in most cases will be the default handler) and your new handler is used instead. For example, this is the default handler:
focusManager.setEventListener(handleFocus => {// Listen to visibillitychange and focusif (typeof window !== 'undefined' && window.addEventListener) {window.addEventListener('visibilitychange', handleFocus, false)window.addEventListener('focus', handleFocus, false)}return () => {// Be sure to unsubscribe if a new handler is setwindow.removeEventListener('visibilitychange', handleFocus)window.removeEventListener('focus', handleFocus)}})
A great use-case for replacing the focus handler is that of iframe events. Iframes present problems with detecting window focus by both double-firing events and also firing false-positive events when focusing or using iframes within your app. If you experience this, you should use an event handler that ignores these events as much as possible. I recommend this one! It can be set up in the following way:
import { focusManager } from '@sveltestack/svelte-query'import onWindowFocus from './onWindowFocus' // The gist abovefocusManager.setEventListener(onWindowFocus) // Boom!
import { focusManager } from '@sveltestack/svelte-query'// Override the default focus statefocusManager.setFocused(true)// Fallback to the default focus checkfocusManager.setFocused(undefined)
The latest TanStack news, articles, and resources, sent to your inbox.