Skip to content

[Start] Encoded URL params cause redirect loop on page refresh #4514

Closed

Description

@mantey-github

Which project does this relate to?

Start

Describe the bug

Is this supposed to happen?

When using encoded parameters in routes, client-side navigation works fine, but hard refresh/page reload causes ERR_TOO_MANY_REDIRECTS.

Your Example Website or App

https://github.com/TanStack/router/tree/main/examples/react/start-basic

Steps to Reproduce the Bug or Issue

  1. Use start-basic https://github.com/tanstack/router/tree/main/examples/react/start-basic example app
  2. In post.tsx, change params for postId from post.id to post.title
<Link
   to="/posts/$postId"
   params={{ postId: post.title}}
   ...
  1. Uncomment fetchPost({ data: postId }) in the loader and just return {} so you do not get a post not found error.
export const Route = createFileRoute('/posts/$postId')({
  loader: ({ params: { postId } }) => {
    console.info(`Loading post with id ${postId}...`)
    // return fetchPost({ data: postId })
    return {}
  },
 ...
  1. Navigate to a post with spaces in title
  2. ✅ Client navigation works - URL shows encoded params, console shows decoded
  3. ❌ Hard refresh the page → infinite redirect loop

Expected behavior

Page should load normally on refresh, just like client navigation
Actual: ERR_TOO_MANY_REDIRECTS error

Screenshots or Videos

Thank+You.mp4

Platform

  • OS: macOS
  • Browser: Chrome
  • Version: 1.121.35

Additional context

No response

Activity

  1. 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
  2. andyabih commented on Jun 27, 2025

    @andyabih
    Contributor

    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 matches
        this.cancelMatches()
        this.latestLocation = this.parseLocation(this.latestLocation)
    
        if (this.isServer) {
          // for SPAs on the initial load, this is handled by the Transitioner
          const nextLocation = this.buildLocation({
            to: this.latestLocation.pathname,
            search: true,
            params: true,
            hash: true,
            state: true,
            _includeValidateSearch: true,
          })
    
          if (trimPath(this.latestLocation.href) !== trimPath(nextLocation.href)) {
            throw redirect({ 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:

      React.useEffect(() => {
        const unsub = router.history.subscribe(router.load)
        
        const nextLocation = router.buildLocation({
          to: router.latestLocation.pathname,
          search: true,
          params: true,
          hash: true,
          state: true,
          _includeValidateSearch: true,
        })
        
        if (
          trimPathRight(router.latestLocation.href) !==
          trimPathRight(nextLocation.href)
        ) {
          router.commitLocation({ ...nextLocation, replace: true })
        }
    
        return () => {
          unsub()
        }
      }, [router, router.history])

    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.

    Will revert back when I have more.

  3. tannerlinsley commented on Jul 15, 2025

    @tannerlinsley
    Member

    I think I have a fix for this, but it's currently in a different branch/PR

  4. added 2 commits that reference this issue on Jul 15, 2025
    013155c
    d907674
  5. added a commit that references this issue on Aug 8, 2025
    e755316
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions