Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/solid-query-ssr-settled-state.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@tanstack/solid-query': patch
---

fix(solid-query): render settled query state during SSR
41 changes: 41 additions & 0 deletions packages/solid-query/src/__tests__/ssr.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// @vitest-environment node

import { Match, Switch } from 'solid-js'
import { renderToStringAsync } from 'solid-js/web'
import { describe, expect, it } from 'vitest'
import { QueryClient, QueryClientProvider, queryOptions, useQuery } from '..'

describe('useQuery SSR', () => {
it('renders the settled result when only status fields are read', async () => {
const fetchShellData = (slug: string) =>
queryOptions({
queryKey: ['shell-data', slug],
queryFn: () => `ok: ${slug}`,
})

function Page() {
const query = useQuery(() => fetchShellData('/'))

return (
<Switch>
<Match when={query.isPending}>
<span>PENDING</span>
</Match>
<Match when={query.isSuccess}>
<span>Success: {query.data}</span>
</Match>
</Switch>
)
}

const markup = await renderToStringAsync(() => (
<QueryClientProvider client={new QueryClient()}>
<Page />
</QueryClientProvider>
))

expect(markup).toContain('Success:')
expect(markup).toContain('ok: /')
expect(markup).not.toContain('PENDING')
})
})
10 changes: 9 additions & 1 deletion packages/solid-query/src/useBaseQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,15 @@ export function useBaseQuery<
}
return queryResource()?.data
}
return Reflect.get(target, prop)
const result = Reflect.get(target, prop)
if (
isServer &&
queryResource.state === 'ready' &&
typeof result !== 'function'
) {
return queryResource.latest?.[prop]
}
return result
},
}

Expand Down