Framework guidesNext.js fetch vs tanstack-fetch: SSR cookies and hydration
Use tanstack-fetch in Next.js App Router — ssr-forward cookies, absolute baseUrl on the server, React Query dehydrate/hydrate, SSE, and migrate path.
Read the postOctober 12, 2026 · Nuxt · Vue · SSR

Stack
Nuxt 3 / 4 · tanstack-fetch/vue · @tanstack/vue-query · ssr-forward
Nuxt’s $fetch (ofetch) is ergonomic for Nitro and pages. It still leaves you to invent Query-ready errors, AbortSignal discipline, SSE with Authorization, and a browser-identical client after SSR.
tanstack-fetch plugs into Nuxt via createFetchPlugin, forwards cookies with ssr-forward, and shares options with Vue Query.
| If you need… | Prefer |
|---|---|
| SSR cookies + same client in browser | tanstack-fetch |
Vue Query without $fetch unwrap glue | tanstack-fetch |
Name clash with Nuxt useFetch | import as useFetchClient |
| One-off Nitro utility | $fetch is fine |
| SSE + Authorization | useSse |
npm install tanstack-fetch @tanstack/vue-queryOn the server, read useRequestHeaders(['cookie', …]). On the client, ssr-forward skips. One plugin registration covers both.
Contract that matters for Query:
isError works)signal through<script setup lang="ts">
const { data } = await useAsyncData('users', () => $fetch<User[]>('/api/users'))
</script>Great for simple pages — weaker when you need shared cache, mutations, refresh-token, and SSE with the same auth.
// plugins/tanstack-fetch.ts
import { createFetch } from 'tanstack-fetch'
import {
createRetryIdempotentInterceptor,
createSsrForwardInterceptor,
createTraceInterceptor,
} from 'tanstack-fetch/plugins'
import { createFetchPlugin } from 'tanstack-fetch/vue'
export default defineNuxtPlugin((nuxtApp) => {
const config = useRuntimeConfig()
const api = createFetch({
baseUrl: config.public.apiBase as string,
source: import.meta.server ? 'ssr' : 'client',
interceptors: [
createSsrForwardInterceptor(),
createTraceInterceptor(),
createRetryIdempotentInterceptor(),
],
incoming: () => {
if (!import.meta.server) return undefined
const headers = useRequestHeaders(['cookie', 'authorization', 'x-request-id'])
return {
cookie: headers.cookie,
authorization: headers.authorization,
requestId: headers['x-request-id'],
}
},
})
nuxtApp.vueApp.use(createFetchPlugin({ client: api }))
return { provide: { api } }
})<script setup lang="ts">
import { useFetch as useFetchClient } from 'tanstack-fetch/vue'
import { useQuery } from '@tanstack/vue-query'
const api = useFetchClient()
// or: const { $api } = useNuxtApp()
const { data, isPending } = useQuery({
queryKey: ['users'],
queryFn: ({ signal }) => api.get<User[]>('/users', { signal }),
})
</script>Example: Nuxt SSR. Pair with a Vue Query Nuxt plugin for dehydrate/hydrate.
Rough relative boilerplate (lower is better) — a map of where teams spend time, not a micro-benchmark.
tanstack-fetchtypical $fetch path
| Capability | tanstack-fetch | $fetch / ofetch |
|---|---|---|
| SSR cookie forward | ssr-forward | Manual |
| Vue Query fit | Native | Adapt |
| Name-safe composable | useFetchClient | Nuxt useFetch |
| Typed errors | FetchError | ofetch errors |
| Refresh single-flight | Plugin | DIY |
| SSE + Authorization | Yes | No |
| Nitro-only calls | Use either | Native $fetch |
import { createFetch } from 'tanstack-fetch'
import {
createAuthInterceptor,
createRefreshTokenInterceptor,
createStatusInterceptor,
} from 'tanstack-fetch/plugins'
let expiresAt = 0
const api = createFetch({
baseUrl: '/api',
interceptors: [
createAuthInterceptor({ getToken: () => localStorage.getItem('access_token') }),
createStatusInterceptor({ 403: () => toast.error('Forbidden') }),
],
})
api.use(
'refresh-token',
createRefreshTokenInterceptor({
refresh: async () => {
const body = await api.post<{ accessToken: string; expiresIn: number }>('/auth/refresh', {
interceptors: { eject: ['refresh-token', 'auth'] },
})
localStorage.setItem('access_token', body.accessToken)
expiresAt = Date.now() + body.expiresIn * 1000
},
before: { getExpiresAt: () => expiresAt, skewMs: 60_000 },
after: { enabled: true },
}),
)The plugin keeps one in-flight refresh and retries the failed call — the usual interceptor queue, without the queue code.
import { useMutation, useQueryClient } from '@tanstack/vue-query'
import { useFetch as useFetchClient } from 'tanstack-fetch/vue'
const api = useFetchClient()
const queryClient = useQueryClient()
useMutation({
mutationFn: (input: CreateUserInput) => api.post<User>('/users', { body: input }),
onSuccess: () => queryClient.invalidateQueries({ queryKey: ['users'] }),
})Browser EventSource cannot set Authorization. tanstack-fetch/sse streams over fetch.
Create the stream client from tanstack-fetch/sse (often in the same Nuxt plugin with sse-resume) and call useSse in a client-only component.
Status: connecting | connected | disconnected | error.
Use createTRPCFetch(api) so Nuxt SSR cookies apply to tRPC batches. See tRPC · Vue/Nuxt.
npx tanstack-fetch migrate --from ofetch --framework nuxt --write
npx tanstack-fetch doctor --dir ./plugins/tanstack-fetch.ts
plugins/vue-query.ts
queries/users.ts
pages/users.vue
components/OrdersLive.client.vuetanstack-fetchtanstack-fetch/sse + useSsecreateFetch across packagesssr-forward + incominguseFetch as useFetchClient if neededsignal in queryFnsnpm install tanstack-fetch @tanstack/vue-query
npx tanstack-fetch doctor --dir ./srcSame deep-dive format for every stack:
Framework guides·3 min read
Use tanstack-fetch in Next.js App Router — ssr-forward cookies, absolute baseUrl on the server, React Query dehydrate/hydrate, SSE, and migrate path.
Framework guides·3 min read
Compare SvelteKit event.fetch with tanstack-fetch for TanStack Query, SSR cookie forwarding, typed errors and authenticated SSE. Includes practical Svelte examples.
Framework guides·3 min read
Replace ofetch and axios glue in Vue 3 with tanstack-fetch — createFetchPlugin, useFetch, useSse, Vue Query, refresh-token, and a full comparison.