Start SSR enters infinite 307 loop when custom search serializer emits comma-delimited arrays #8777
Which project does this relate to?#
Router
Describe the bug#
TanStack Start SSR can enter an infinite self-redirect loop when a custom stringifySearch emits comma-delimited arrays.
For GET /test?tags=1,2, server URL normalization serializes the query as tags=1%2C2. The configured parser and route's validateSearch turn this into tags: ['1', '2'], then the configured stringifier canonicalizes it back to the literal-comma form, /test?tags=1,2.
loadServerRoute compares the normalized next.publicHref with the canonical publicHref, sees %2C and , as different, and redirects to canonical publicHref with 307 Location: /test?tags=1,2. The redirected request repeats the same process indefinitely. Browser show ERR_TOO_MANY_REDIRECTS.
Complete minimal reproducer#
https://stackblitz.com/edit/github-tnsncrms-xorkcit6?file=src%2Frouter.tsx
Steps to Reproduce the Bug#
- Visit reproduction link https://stackblitz.com/edit/github-tnsncrms-xorkcit6?file=src%2Frouter.tsx
- Click the link in the landing page to navigate to
/test?tags=1,2 - Observe "redirected you too many times" error in browser and document response:
HTTP/1.1 307
Location: /test?tags=1,2
Reproducible with curl:
4. Open new terminal in sandbox
5. Run curl -sL --max-redirs 3 'http://localhost:3000/test?tags=1,2'
6. Observe curl exits with: curl: (47) Maximum (3) redirects followed
Expected behavior#
The request should converge—ideally return 200 for /test?tags=1,2; at minimum it must not redirect to the identical public URL forever.
Equivalent encodings of a query value, such as , and %2C, should not cause a canonicalization redirect
Screenshots or Videos#
No response
Platform#
@tanstack/react-router:1.170.45@tanstack/react-start:1.169.3- React:
19.3.1 - Node:
22.16.0 - OS: macOS 15.6 (Darwin 24.6.0)
- Bundler: Vite
8.3.4 - Browser: any; reproducible with
curl
Additional context#
getNormalizedURL rebuilds the query with URLSearchParams(...).toString(), which encodes , as %2C, before loadServerRoute compares next.publicHref with the serializer's canonical output.
Workaround:#
Split parsed params by comma in stringifySearch: Request search string is ?tags=1,2, after server normalization becomes ?tags=1%2C2, in stringifySearch is is parsed into { 'tags' : '1,2' }, after splitting { 'tags' : ['1', '2'] }
This is not a proper solution, but a way to mitigate the redirect loop while keeping the functionality as close as possible. It splits already escaped characters ?tag=1%2C2,3&input=foo%2Cbar -> { tags: ['1', '2', '3'], input: ['foo', 'bar'] } which is undesired.
This workaround doesn't work when rewrite option is provided in router. In that case stringifySearch doesn't affect the publicHref which is built from the history's href (see code) and SSR enters the infinite redirect loop.
pavstev(GitHub profile, opens in a new tab) linked this to #8848
Sign in to join this conversation.
I can reproduce this on
mainwith a router-core test and withcurlagainst a Start dev server: aparseSearchandstringifySearchpair that writes arrays astags=1,2gives a 307 from/test?tags=1,2to the same URL, and a no-oprewriteor a%20in a string value loops the same way. The cause is thatgetNormalizedURLre-encodes the query beforeloadServerRoutecomparesnext.publicHrefwithcanonical.publicHref, so the two sides are never in the same form. My fix runs both hrefs through the sameURLSearchParamsnormalization inside that comparison, about 30 lines acrossload-server.ts,ssr-server.tsandutils.ts; with it the request returns 200, a real canonical difference still redirects once and then converges, and the router-core, react-router and start-server-core suites pass. #8452 covers this too but no longer merges cleanly withmain, so @schiller-manuel, do you want this small change as its own PR, or should it wait for #8452 to be rebased?