You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Start] Encoded URL params cause redirect loop on page refresh #4514
Uncomment fetchPost({ data: postId }) in the loader and just return {} so you do not get a post not found error.
exportconstRoute=createFileRoute('/posts/$postId')({loader: ({params: { postId }})=>{console.info(`Loading post with id ${postId}...`)// return fetchPost({ data: postId })return{}},
...
changed the title [-]Encoded URL params cause redirect loop on page refresh[/-][+][Start] Encoded URL params cause redirect loop on page refresh[/+]on Jun 25, 2025
Trying to dive a bit deeper into this and find the actual issue.
The beforeLoad method in RouterCore is throwing the redirects because of URL encoding when on server:
beforeLoad=()=>{// Cancel any pending matchesthis.cancelMatches()this.latestLocation=this.parseLocation(this.latestLocation)if(this.isServer){// for SPAs on the initial load, this is handled by the TransitionerconstnextLocation=this.buildLocation({to: this.latestLocation.pathname,search: true,params: true,hash: true,state: true,_includeValidateSearch: true,})if(trimPath(this.latestLocation.href)!==trimPath(nextLocation.href)){throwredirect({href: nextLocation.href})}}
this.latestLocation.href (and this.latestLocation.pathname) is URI encoded whereas nextLocation.href is not -- and that's causing the loop.
Latest: posts/dolorem%20eum%20magni%20eos%20aperiam%20quia
Next: posts/dolorem eum magni eos aperiam quia
Same example but with some search params -- notice that the search param is encoded in both here:
Latest: posts/dolorem%20eum%20magni%20eos%20aperiam%20quia?a=i%24m
Next: posts/dolorem eum magni eos aperiam quia?a=i%24m
The same exact issue also happens in the Transitioner as well:
Like I mentioned, still diving deeper into this. It's a pretty easy fix in terms of modifying the equality check to something that decodes the path first and then checks, but probably more ideal to look at why exactly this is happening in the first place.
Given that the search params are both encoded, the issue is probably tied to the nextLocations's path being decoded vs. the latestLocations's not being decoded.
Activity
[-]Encoded URL params cause redirect loop on page refresh[/-][+][Start] Encoded URL params cause redirect loop on page refresh[/+]andyabih commentedon Jun 27, 2025
Trying to dive a bit deeper into this and find the actual issue.
The
beforeLoadmethod inRouterCoreis throwing the redirects because of URL encoding when on server:this.latestLocation.href(andthis.latestLocation.pathname) is URI encoded whereasnextLocation.hrefis not -- and that's causing the loop.Same example but with some search params -- notice that the search param is encoded in both here:
The same exact issue also happens in the
Transitioneras well:Like I mentioned, still diving deeper into this. It's a pretty easy fix in terms of modifying the equality check to something that decodes the path first and then checks, but probably more ideal to look at why exactly this is happening in the first place.
Given that the search params are both encoded, the issue is probably tied to the
nextLocations's path being decoded vs. thelatestLocations's not being decoded.Will revert back when I have more.
tannerlinsley commentedon Jul 15, 2025
I think I have a fix for this, but it's currently in a different branch/PR
fix: prevent infinite redirects with encoded URL parameters
fix: prevent infinite redirects with encoded URL parameters
fix: do not pre-decodeURI before passing to createMemoryHistory (#4893)
fix(budget-explorer): work around TanStack Router SSR redirect loop