Skip to content

Svelte executes queryFn on server and ignores result #4798

@ottomated

Description

@ottomated

Describe the bug

When using svelte-query with sveltekit, createQuery executes on the server. This causes any client-specific APIs, such as a relative fetch or window access, to throw an error. Furthermore, since async SSR must be done in the load function in sveltekit, the result of queryFn is dropped.

Your minimal, reproducible example

https://codesandbox.io/p/sandbox/lucid-architecture-sqwk6e?file=%2Fsrc%2Froutes%2F%2Bpage.svelte

Steps to reproduce

  1. Open the dev logs
  2. Reload the page
  3. Observe the error being thrown on the server
  4. Observe that even though the error is caught, and the string 'error' is returned, the client renders undefined until it finishes fetching

Expected behavior

I expect queryFn only to be called on the client but it is called on the server.

How often does this bug happen?

Every time

Platform

  • OS: Linux
  • Browser: Firefox

TanStack Query version

4.22.1

Additional context

Workaround is to add

import { browser } from '$app/environment';
// ...
queryFn: () => {
    if (!browser) return null;
    // ...
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions