DEV Community

Mohammad Garmabi
Mohammad Garmabi

Posted on Originally published at mohamadgarmabi.github.io

tanstack-fetch 2.2.1: I audited every entry point and fixed 31 bugs

tanstack-fetch 2.2.1 adds no new features. I went through every entry point (core, /sse, /trpc, the framework adapters, DevTools, and the CLI) and fixed 31 bugs. Each fix has a regression test.

npm install tanstack-fetch@2.2.1
Enter fullscreen mode Exit fullscreen mode

If you haven't seen it before: tanstack-fetch is a small, typed fetch client built for TanStack Query. It has guides for React, Vue, Next.js, Nuxt, Solid, Svelte/SvelteKit, Angular and TanStack Start.

Here are the fixes most likely to have affected you.

1. A relative baseUrl actually works now

const api = createFetch({ baseUrl: '/api' })
await api.get('/users')
// 2.2.0 โ†’ TypeError: Invalid URL
// 2.2.1 โ†’ GET https://your.site/api/users
Enter fullscreen mode Exit fullscreen mode

In the browser it resolves against location.href. On the server you get a clear error asking for an absolute baseUrl, so it no longer fails with a vague message.

2. Retries no longer re-send your POST by accident

Three fixes:

  • Interceptor errors thrown in onResponse / onResponseError, such as an onUnauthorized redirect, were wrapped as NETWORK_ERROR and could be retried, which meant re-sending a POST. They now propagate unchanged and are never retried.
  • controller.abort(reason) was treated as a network error and retried. It now rejects with your reason, with no retry.
  • { action: 'drop' } in onRequest was typed but ignored. It now rejects with REQUEST_DROPPED, and fetch is never called.

3. SSE resume does what its name says

Before this release, only a bad status at connect time went through the reconnect path. A mid-stream drop went straight to your code, and Last-Event-ID was never sent.

const api = createFetch({ baseUrl, plugins: ['sse-resume'] })

for await (const event of api.sse<Order>('/orders/stream')) {
  // after a drop: reconnects after 1s with Last-Event-ID: 7, continues at event 8
}
Enter fullscreen mode Exit fullscreen mode

Other SSE fixes:

  • api.sse('/chat', { body: { prompt } }) now sends a POST with the body. Before, it sent a GET and dropped the body.
  • The parser now handles \r\n split across chunks, id:-only blocks and bare data lines.
  • onSseClose runs exactly once, and close() during the reconnect wait actually stops the reconnect.

4. SSR leaks in Vue/Nuxt and Svelte

  • Vue/Nuxt useSse / useWs were opening connections during SSR. They now connect only in the browser.
  • Svelte's setFetchClient() stored the client in a module-level variable, shared across SSR requests. On the server, the client now comes from Svelte context only.

5. A DevTools XSS

The DevTools panel used innerHTML to render values that partly came from a response header (x-tanstack-fetch-mock), so a server could inject script into it. Every element is now built with textContent.

6. Codemods that produce working code

  • migrate converts ky's ky.get(url).json<User>() to api.get<User>(url).
  • It renames axios params: to query: only at the top level of the config, not inside request bodies.
  • It puts the new import after 'use client'.
  • generate (OpenAPI) now outputs valid TypeScript: it quotes keys like 'content-type' and de-duplicates colliding names.

Behaviour changes to check

  • Interceptor errors are rethrown as-is, with no retry.
  • abort(reason) rejects with reason.
  • An SSE call with a body sends a POST.
  • sse-resume reconnects after drops and 5xx responses.
  • A WebSocket's reconnect counter resets only after 5s of stable connection.
  • A relative tRPC URL on the server now throws.

Cost

The core grew by about 260 bytes gzip (3.71KB โ†’ 3.97KB).

Known limits

Solid, Svelte and Angular useSse / useWs still don't re-subscribe when path or options change. That's planned for the next minor release.


๐Ÿ“– Full release notes ยท โญ GitHub ยท ๐Ÿ“ฆ npm ยท ๐Ÿ“š Docs

If you use it, I'd love to hear which of these bugs you'd already run into. Issues and stars are very welcome.

Top comments (0)