Skip to content

refactor(history): use TSR_key namespace in state for hash #4356

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

Merged
merged 10 commits into from
Jun 23, 2025

Conversation

naoya7076
Copy link
Contributor

@naoya7076 naoya7076 commented Jun 10, 2025

Updated description by @SeanCassiere

The scope of this pull request has changed to the following:

  • Add __TSR_key into location.state.
  • __TSR_key mirrors the existing functionality of location.state.key.
  • Update internal functions to location.state.key to favor location.state.__TSR_key.
  • Update documentation to reflect location.state.__TSR_key.

Original description

Currently, when using TanStack Router, if users define a property named key within the navigation state, its value is overridden by the router's internal logic, which assigns a random key.

For example:
#284 (comment)

const navigate = useNavigate();
navigate({ to: '/', state: { color: 'blue', key: 123 } });

// Defined type
declare module '@tanstack/react-router' {
  interface HistoryState {
    key?: number
    otherValue?: string
  }
}

// On navigation target
const location = useLocation();
console.log(location.state.color); // correctly "blue"
console.log(location.state.key);   // incorrectly overridden by a random string like "p7axhg"

Expected Behavior

When explicitly providing a key within the navigation state, it should preserve the user's defined value rather than being overridden by the router's internal random key.

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.

@naoya7076 thanks for opening a PR for this, but we can't ship this in within the 1.0 release-cycle since this'd be considered a breaking change.
Since we have user-facing functions (like getScrollRestorationKey) which rely on location.state.key being defined/accessible.

I'll add this to the v2 list for it to be renamed to __TSR_key.

Edit: I've added this to the v2 list.

@naoya7076
Copy link
Contributor Author

@SeanCassiere Thank you again for your feedback. I've changed __key to __TSR_key.  If merging it for v2 is not too much trouble, I would be very happy to see it included.

@naoya7076 naoya7076 changed the title fix: update state key references from 'key' to '__key' fix: update state key references from 'key' to '__TSR_key' Jun 13, 2025
@SeanCassiere
Copy link
Member

@SeanCassiere Thank you again for your feedback. I've changed __key to __TSR_key.  If merging it for v2 is not too much trouble, I would be very happy to see it included.

@naoya7076 we currently do not have a v2 development branch open. As I mentioned earlier, I have added it onto the v2 list so that it gets done for its development.

The only path forward here, in having this merged into main at the moment, would be to have both key and __TSR_key be added in and have their functionality/values be synced during the v1 cycle. The key property would be marked as TODO: Remove in v2 using a comment. Then, when it comes time to begin working on v2, we'd go ahead and act on the TODO.

@naoya7076
Copy link
Contributor Author

@SeanCassiere Thank you for the guidance! I've updated the implementation as requested:

  • Added both key and __TSR_key properties with synced values
  • Marked key with TODO: Remove in v2 comments
  • Updated all affected packages and documentation

Thanks for the clear direction! 🙏

@naoya7076 naoya7076 requested a review from SeanCassiere June 17, 2025 23:55
@SeanCassiere SeanCassiere changed the title fix: update state key references from 'key' to '__TSR_key' refactor(history): use TSR_key namespace in state for hash Jun 22, 2025
Copy link

nx-cloud bot commented Jun 22, 2025

View your CI Pipeline Execution ↗ for commit 5962a34.

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

☁️ Nx Cloud last updated this comment at 2025-06-23 02:47:53 UTC

Copy link

pkg-pr-new bot commented Jun 22, 2025

More templates

@tanstack/arktype-adapter

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

@tanstack/directive-functions-plugin

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

@tanstack/eslint-plugin-router

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

@tanstack/history

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

@tanstack/react-router

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

@tanstack/react-router-devtools

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

@tanstack/react-router-with-query

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

@tanstack/react-start

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

@tanstack/react-start-client

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

@tanstack/react-start-plugin

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

@tanstack/react-start-server

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

@tanstack/router-cli

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

@tanstack/router-core

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

@tanstack/router-devtools

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

@tanstack/router-devtools-core

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

@tanstack/router-generator

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

@tanstack/router-plugin

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

@tanstack/router-utils

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

@tanstack/router-vite-plugin

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

@tanstack/server-functions-plugin

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

@tanstack/solid-router

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

@tanstack/solid-router-devtools

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

@tanstack/solid-start

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

@tanstack/solid-start-client

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

@tanstack/solid-start-plugin

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

@tanstack/solid-start-server

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

@tanstack/start-client-core

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

@tanstack/start-plugin-core

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

@tanstack/start-server-core

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

@tanstack/start-server-functions-client

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

@tanstack/start-server-functions-fetcher

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

@tanstack/start-server-functions-server

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

@tanstack/valibot-adapter

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

@tanstack/virtual-file-routes

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

@tanstack/zod-adapter

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

commit: 5962a34

@SeanCassiere

This comment was marked as resolved.

@SeanCassiere SeanCassiere merged commit 980159a into TanStack:main Jun 23, 2025
5 checks passed
@SeanCassiere
Copy link
Member

I've updated the description and added the removal into the v2 checklist.

@naoya7076 thank you for taking this up and working with us on this one!

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.

2 participants