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
1 change: 0 additions & 1 deletion e2e/solid-start/basic-solid-query/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
"@tanstack/solid-query-devtools": "^6.0.0-rc.1",
"@tanstack/solid-router": "workspace:^",
"@tanstack/solid-router-devtools": "workspace:^",
"@tanstack/solid-router-ssr-query": "workspace:^",
"@tanstack/solid-start": "workspace:^",
"redaxios": "^0.5.1",
"solid-js": "^2.0.0-rc.4",
Expand Down
47 changes: 40 additions & 7 deletions e2e/solid-start/basic-solid-query/src/router.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { QueryClient } from '@tanstack/solid-query'
import { createRouter } from '@tanstack/solid-router'
import { setupRouterSsrQueryIntegration } from '@tanstack/solid-router-ssr-query'
import { QueryClient, QueryClientProvider } from '@tanstack/solid-query'
import { createRouter, isRedirect } from '@tanstack/solid-router'
import { routeTree } from './routeTree.gen'
import { DefaultCatchBoundary } from './components/DefaultCatchBoundary'
import { NotFound } from './components/NotFound'
import type { AnyRouter } from '@tanstack/solid-router'

export function getRouter() {
const queryClient = new QueryClient()
Expand All @@ -14,10 +14,43 @@ export function getRouter() {
defaultPreload: 'intent',
defaultErrorComponent: DefaultCatchBoundary,
defaultNotFoundComponent: () => <NotFound />,
// No integration package: on Solid, SSR transfer is native to
// QueryClientProvider (it serializes the request's cache into Solid's
// hydration registry and primes the client cache from it).
Wrap: (props) => (
<QueryClientProvider client={queryClient}>
{props.children}
</QueryClientProvider>
),
})
setupRouterSsrQueryIntegration({
router,
queryClient,
})
routeCacheRedirects(router, queryClient)
return router
}

// Redirects thrown where the router is driving — beforeLoad, loaders, and
// any queryFn a loader awaits — are the router's own to handle. Cache-driven
// fetches (mount fetches, background refetches, mutations) run outside it,
// so redirect() errors from both caches hand off to the router here.
// Client-only runtime navigation glue, not an SSR concern: on the server a
// redirect thrown during render resolves through the stream handler.
function routeCacheRedirects(router: AnyRouter, queryClient: QueryClient) {
if (typeof document === 'undefined') return
const navigateOnRedirect = <TRest extends Array<unknown>>(
onError?: (error: Error, ...rest: TRest) => void,
) => {
return (error: Error, ...rest: TRest) => {
if (isRedirect(error)) {
error.options._fromLocation = router.stores.location.get()
void router.navigate(router.resolveRedirect(error).options)
return
}
onError?.(error, ...rest)
}
}
const queryCache = queryClient.getQueryCache()
const mutationCache = queryClient.getMutationCache()
queryCache.config.onError = navigateOnRedirect(queryCache.config.onError)
mutationCache.config.onError = navigateOnRedirect(
mutationCache.config.onError,
)
}
1 change: 0 additions & 1 deletion e2e/solid-start/server-functions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
"@tanstack/solid-query-devtools": "^6.0.0-rc.1",
"@tanstack/solid-router": "workspace:^",
"@tanstack/solid-router-devtools": "workspace:^",
"@tanstack/solid-router-ssr-query": "workspace:^",
"@tanstack/solid-start": "workspace:^",
"js-cookie": "^3.0.5",
"redaxios": "^0.5.1",
Expand Down
44 changes: 40 additions & 4 deletions e2e/solid-start/server-functions/src/router.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createRouter } from '@tanstack/solid-router'
import { setupRouterSsrQueryIntegration } from '@tanstack/solid-router-ssr-query'
import { QueryClient } from '@tanstack/solid-query'
import { createRouter, isRedirect } from '@tanstack/solid-router'
import { QueryClient, QueryClientProvider } from '@tanstack/solid-query'
import type { AnyRouter } from '@tanstack/solid-router'
import { routeTree } from './routeTree.gen'
import { DefaultCatchBoundary } from './components/DefaultCatchBoundary'
import { NotFound } from './components/NotFound'
Expand All @@ -18,13 +18,49 @@ export function getRouter() {
bar: 'baz',
},
},
// No integration package: on Solid, SSR transfer is native to
// QueryClientProvider (it serializes the request's cache into Solid's
// hydration registry and primes the client cache from it).
Wrap: (props) => (
<QueryClientProvider client={queryClient}>
{props.children}
</QueryClientProvider>
),
})

setupRouterSsrQueryIntegration({ router, queryClient })
routeCacheRedirects(router, queryClient)

return router
}

// Redirects thrown where the router is driving — beforeLoad, loaders, and
// any queryFn a loader awaits — are the router's own to handle. Cache-driven
// fetches (mount fetches, background refetches, mutations) run outside it,
// so redirect() errors from both caches hand off to the router here.
// Client-only runtime navigation glue, not an SSR concern: on the server a
// redirect thrown during render resolves through the stream handler.
function routeCacheRedirects(router: AnyRouter, queryClient: QueryClient) {
if (typeof document === 'undefined') return
const navigateOnRedirect = <TRest extends Array<unknown>>(
onError?: (error: Error, ...rest: TRest) => void,
) => {
return (error: Error, ...rest: TRest) => {
if (isRedirect(error)) {
error.options._fromLocation = router.stores.location.get()
void router.navigate(router.resolveRedirect(error).options)
return
}
onError?.(error, ...rest)
}
}
const queryCache = queryClient.getQueryCache()
const mutationCache = queryClient.getMutationCache()
queryCache.config.onError = navigateOnRedirect(queryCache.config.onError)
mutationCache.config.onError = navigateOnRedirect(
mutationCache.config.onError,
)
}

declare module '@tanstack/solid-router' {
interface Register {
router: ReturnType<typeof getRouter>
Expand Down
1 change: 0 additions & 1 deletion e2e/solid-start/server-routes/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
"@tanstack/solid-query": "^6.0.0-rc.1",
"@tanstack/solid-router": "workspace:^",
"@tanstack/solid-router-devtools": "workspace:^",
"@tanstack/solid-router-ssr-query": "workspace:^",
"@tanstack/solid-start": "workspace:^",
"js-cookie": "^3.0.5",
"redaxios": "^0.5.1",
Expand Down
44 changes: 40 additions & 4 deletions e2e/solid-start/server-routes/src/router.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { createRouter } from '@tanstack/solid-router'
import { setupRouterSsrQueryIntegration } from '@tanstack/solid-router-ssr-query'
import { QueryClient } from '@tanstack/solid-query'
import { createRouter, isRedirect } from '@tanstack/solid-router'
import { QueryClient, QueryClientProvider } from '@tanstack/solid-query'
import { routeTree } from './routeTree.gen'
import { DefaultCatchBoundary } from './components/DefaultCatchBoundary'
import { NotFound } from './components/NotFound'
import type { AnyRouter } from '@tanstack/solid-router'

export function getRouter() {
const queryClient = new QueryClient()
Expand All @@ -13,8 +13,44 @@ export function getRouter() {
defaultErrorComponent: DefaultCatchBoundary,
defaultNotFoundComponent: () => <NotFound />,
scrollRestoration: true,
// No integration package: on Solid, SSR transfer is native to
// QueryClientProvider (it serializes the request's cache into Solid's
// hydration registry and primes the client cache from it).
Wrap: (props) => (
<QueryClientProvider client={queryClient}>
{props.children}
</QueryClientProvider>
),
})
setupRouterSsrQueryIntegration({ router, queryClient })
routeCacheRedirects(router, queryClient)

return router
}

// Redirects thrown where the router is driving — beforeLoad, loaders, and
// any queryFn a loader awaits — are the router's own to handle. Cache-driven
// fetches (mount fetches, background refetches, mutations) run outside it,
// so redirect() errors from both caches hand off to the router here.
// Client-only runtime navigation glue, not an SSR concern: on the server a
// redirect thrown during render resolves through the stream handler.
function routeCacheRedirects(router: AnyRouter, queryClient: QueryClient) {
if (typeof document === 'undefined') return
const navigateOnRedirect = <TRest extends Array<unknown>>(
onError?: (error: Error, ...rest: TRest) => void,
) => {
return (error: Error, ...rest: TRest) => {
if (isRedirect(error)) {
error.options._fromLocation = router.stores.location.get()
void router.navigate(router.resolveRedirect(error).options)
return
}
onError?.(error, ...rest)
}
}
const queryCache = queryClient.getQueryCache()
const mutationCache = queryClient.getMutationCache()
queryCache.config.onError = navigateOnRedirect(queryCache.config.onError)
mutationCache.config.onError = navigateOnRedirect(
mutationCache.config.onError,
)
}
12 changes: 12 additions & 0 deletions packages/solid-router-ssr-query/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@

# @tanstack/solid-router-ssr-query

> [!IMPORTANT]
> **Deprecated on the Solid v2 line.** Solid 2.0's native channels make this
> package's transport unnecessary: `QueryClientProvider` in
> `@tanstack/solid-query` v6 serializes the request's cache into Solid's
> hydration registry during SSR and primes the client cache from it — running
> this package alongside it ships every query payload twice. The two runtime
> conveniences it bundled are each a few lines of userland composition on
> public APIs (see the Solid Start e2e apps under `e2e/solid-start/`): wrap
> the router tree in `QueryClientProvider` via the router's `Wrap` option,
> and hand cache-driven `redirect()` errors to `router.navigate` from the
> caches' `config.onError`.

SSR query integration for TanStack Solid Router and TanStack Solid Query.

This package provides seamless integration between TanStack Router and TanStack Query for server-side rendering in Solid applications.
Expand Down
9 changes: 0 additions & 9 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading