Skip to content

fix(router-core): prevent infinite redirects with encoded URL params #4662

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

tannerlinsley
Copy link
Collaborator

Fixes infinite redirect loops when refreshing pages with encoded URL parameters.

Browser history stores encoded URLs while buildLocation produces decoded URLs, causing URL mismatch in beforeLoad comparison.

Solution: Added URL normalization during comparison to ensure consistent format.

Fixes #4514

- Normalize URLs during comparison in beforeLoad to handle encoding differences
- Browser history stores encoded URLs while buildLocation may produce decoded URLs
- This mismatch was causing ERR_TOO_MANY_REDIRECTS on page refresh with encoded params
- Fixes infinite redirect loops when refreshing pages with special characters in URLs

Fixes #4514
Copy link

nx-cloud bot commented Jul 15, 2025

View your CI Pipeline Execution ↗ for commit 013155c

Command Status Duration Result
nx affected --targets=test:eslint,test:unit,tes... ✅ Succeeded 4m 51s View ↗
nx run-many --target=build --exclude=examples/*... ✅ Succeeded 2m 29s View ↗

☁️ Nx Cloud last updated this comment at 2025-07-15 20:37:11 UTC

Copy link

pkg-pr-new bot commented Jul 15, 2025

More templates

@tanstack/arktype-adapter

npm i https://pkg.pr.new/TanStack/router/@tanstack/arktype-adapter@4662

@tanstack/directive-functions-plugin

npm i https://pkg.pr.new/TanStack/router/@tanstack/directive-functions-plugin@4662

@tanstack/eslint-plugin-router

npm i https://pkg.pr.new/TanStack/router/@tanstack/eslint-plugin-router@4662

@tanstack/history

npm i https://pkg.pr.new/TanStack/router/@tanstack/history@4662

@tanstack/react-router

npm i https://pkg.pr.new/TanStack/router/@tanstack/react-router@4662

@tanstack/react-router-devtools

npm i https://pkg.pr.new/TanStack/router/@tanstack/react-router-devtools@4662

@tanstack/react-router-with-query

npm i https://pkg.pr.new/TanStack/router/@tanstack/react-router-with-query@4662

@tanstack/react-start

npm i https://pkg.pr.new/TanStack/router/@tanstack/react-start@4662

@tanstack/react-start-client

npm i https://pkg.pr.new/TanStack/router/@tanstack/react-start-client@4662

@tanstack/react-start-plugin

npm i https://pkg.pr.new/TanStack/router/@tanstack/react-start-plugin@4662

@tanstack/react-start-server

npm i https://pkg.pr.new/TanStack/router/@tanstack/react-start-server@4662

@tanstack/router-cli

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-cli@4662

@tanstack/router-core

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-core@4662

@tanstack/router-devtools

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-devtools@4662

@tanstack/router-devtools-core

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-devtools-core@4662

@tanstack/router-generator

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-generator@4662

@tanstack/router-plugin

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-plugin@4662

@tanstack/router-utils

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-utils@4662

@tanstack/router-vite-plugin

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-vite-plugin@4662

@tanstack/server-functions-plugin

npm i https://pkg.pr.new/TanStack/router/@tanstack/server-functions-plugin@4662

@tanstack/solid-router

npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-router@4662

@tanstack/solid-router-devtools

npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-router-devtools@4662

@tanstack/solid-start

npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-start@4662

@tanstack/solid-start-client

npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-start-client@4662

@tanstack/solid-start-plugin

npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-start-plugin@4662

@tanstack/solid-start-server

npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-start-server@4662

@tanstack/start-client-core

npm i https://pkg.pr.new/TanStack/router/@tanstack/start-client-core@4662

@tanstack/start-plugin-core

npm i https://pkg.pr.new/TanStack/router/@tanstack/start-plugin-core@4662

@tanstack/start-server-core

npm i https://pkg.pr.new/TanStack/router/@tanstack/start-server-core@4662

@tanstack/start-server-functions-client

npm i https://pkg.pr.new/TanStack/router/@tanstack/start-server-functions-client@4662

@tanstack/start-server-functions-fetcher

npm i https://pkg.pr.new/TanStack/router/@tanstack/start-server-functions-fetcher@4662

@tanstack/start-server-functions-server

npm i https://pkg.pr.new/TanStack/router/@tanstack/start-server-functions-server@4662

@tanstack/valibot-adapter

npm i https://pkg.pr.new/TanStack/router/@tanstack/valibot-adapter@4662

@tanstack/virtual-file-routes

npm i https://pkg.pr.new/TanStack/router/@tanstack/virtual-file-routes@4662

@tanstack/zod-adapter

npm i https://pkg.pr.new/TanStack/router/@tanstack/zod-adapter@4662

commit: 013155c

@SeanCassiere SeanCassiere changed the title fix: prevent infinite redirects with encoded URL parameters fix(router-core): prevent infinite redirects with encoded URL params Jul 15, 2025
@tannerlinsley tannerlinsley requested review from schiller-manuel and SeanCassiere and removed request for schiller-manuel July 15, 2025 22:39
Copy link
Member

@SeanCassiere SeanCassiere left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀


This may not be part of this fix, but should we be doing this normalization at these places as well?

LN#710 (getLocationChangeInfo -> hrefChanged).

// router-core/router.ts
// getLocationChangeInfo function
export function getLocationChangeInfo(routerState: {
  // ...
}) {
  // ...
  const hrefChanged = fromLocation?.href !== toLocation.href // here
  // ...
}

LN#1642 (Router.commitLocation -> const isSameUrl).

// router-core/router.ts
// Router class
  commitLocation: CommitLocationFn = ({
    ...next
  }) => {
    // ...
    const isSameUrl = this.latestLocation.href === next.href // here
    // ...

@nlynzaad
Copy link
Contributor

nlynzaad commented Jul 16, 2025

@SeanCassiere there is a new private function that was added in router.ts, as part of #4581, called comparePaths, that does some cleaning for trailing slashes before comparing them and returning either true or false depending on wether the paths match or not.

your comment had me thinking we should probably add this in there and start using that more broadly.

I will look into that once this gets merged.

@tannerlinsley tannerlinsley deleted the fix/infinite-redirect-encoded-params branch July 18, 2025 20:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Start] Encoded URL params cause redirect loop on page refresh
3 participants