Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refetchOnWindowFocus is not working (v5) #6351

Closed
frederikhors opened this issue Nov 10, 2023 · 4 comments
Closed

refetchOnWindowFocus is not working (v5) #6351

frederikhors opened this issue Nov 10, 2023 · 4 comments

Comments

@frederikhors
Copy link
Contributor

frederikhors commented Nov 10, 2023

Describe the bug

I just found out that refetchOnWindowFocus is not working.

I have this setup:

export const queryClient = new QueryClient({
  defaultOptions: {
    queries: {
      gcTime: 1000 * 30, // 30 seconds
      persister: //...
    },
  },
});

and even if the below works:

<svelte:window on:visibilitychange={() => console.log('visibilitychange changed!')} />

tanstack/query does not reload queries when visibilitychanges.

Can you confirm?

Your minimal, reproducible example

https://stackblitz.com/edit/sveltejs-kit-template-default-6ntbzr?file=src%2Froutes%2F%2Bpage.svelte,src%2Fapp.html

How often does this bug happen?

Every time

Platform

Win 10, Chrome 119.0.6045.124.

Tanstack Query adapter

svelte-query

TanStack Query version

5.4.3

TypeScript version

5

Additional context

"@tanstack/query-persist-client-core": "5.4.3",
"@tanstack/svelte-query": "5.4.3",
@TkDodo
Copy link
Collaborator

TkDodo commented Nov 11, 2023

I'm not really sure how svelte works, but somewhere, we need to call queryClient.mount() to subscribe to the window events. In React and solid, that's what the QueryClientProvider does:

React.useEffect(() => {
client.mount()
return () => {
client.unmount()
}
}, [client])

What's the equivalent for svelte here? @lachlancollins might know this :)

@lachlancollins
Copy link
Member

@TkDodo the equivalent mount/unmount logic should be this:

onMount(() => {
client.mount()
})
setQueryClientContext(client)
onDestroy(() => {
client.unmount()
})

@lachlancollins
Copy link
Member

@frederikhors Dominik was correct, you need to add the QueryClientProvider component somewhere in your app. The most common place to put this is src/routes/+layout.svelte:

<script lang="ts">
  import { QueryClient, QueryClientProvider } from '@tanstack/svelte-query'

  const queryClient = new QueryClient()
</script>

<QueryClientProvider client={queryClient}>
  <slot />
</QueryClientProvider>

Then you would remove this from +page.svelte:

const client = new QueryClient();

setQueryClientContext(client);

If you needed the client from context you could call const client = useQueryClient()

You can see the docs here: https://tanstack.com/query/latest/docs/svelte/overview

@frederikhors
Copy link
Contributor Author

It works! Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants