Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 6 additions & 15 deletions packages/nuqs/src/adapters/tanstack-router.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useLocation, useMatches, useNavigate } from '@tanstack/react-router'
import { useLocation, useNavigate } from '@tanstack/react-router'
import { startTransition, useCallback, useMemo } from 'react'
import { renderQueryString } from '../lib/url-encoding'
import { createAdapterProvider, type AdapterProvider } from './lib/context'
Expand All @@ -12,12 +12,6 @@ function useNuqsTanstackRouterAdapter(watchKeys: string[]): AdapterInterface {
)
})
const navigate = useNavigate()
const from = useMatches({
select: matches =>
matches.length > 0
? (matches[matches.length - 1]?.fullPath as string)
: undefined
})
const searchParams = useMemo(
() =>
// search is a Record<string, string | number | object | Array<string | number>>,
Expand Down Expand Up @@ -56,20 +50,17 @@ function useNuqsTanstackRouterAdapter(watchKeys: string[]): AdapterInterface {
// TBC if it causes issues with consuming those search params
// in other parts of the app.
//
// When we clear the search, passing an empty string causes
// a type error and possible basepath issues, so we switch it to '.' instead.
// See https://github.com/47ng/nuqs/pull/953#issuecomment-3003583471
to: renderQueryString(search) || '.',
// `from` will be handled by tanstack router match resolver, code snippet:
// https://github.com/TanStack/router/blob/5d940e2d8bdb12e213eede0abe8012855433ec4b/packages/react-router/src/link.tsx#L108-L112
...(from ? { from } : {}),
// Note: we need to specify pathname + search here to avoid TSR appending
// a trailing slash to the pathname, see https://github.com/47ng/nuqs/issues/1215
from: '/',
to: location.pathname + renderQueryString(search),

Choose a reason for hiding this comment

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

You might want to pull the pathname out using useLocation. I'd use a selector in there, for fine-grained subscriptions.

replace: options.history === 'replace',
resetScroll: options.scroll,
hash: prevHash => prevHash ?? ''
})
})
},
[navigate, from]
[navigate]
)

return {
Expand Down
Loading