|
1 | | -import { useLocation, useMatches, useNavigate } from '@tanstack/react-router' |
| 1 | +import { useLocation, useNavigate } from '@tanstack/react-router' |
2 | 2 | import { startTransition, useCallback, useMemo } from 'react' |
3 | 3 | import { renderQueryString } from '../lib/url-encoding' |
4 | 4 | import { createAdapterProvider, type AdapterProvider } from './lib/context' |
5 | 5 | import type { AdapterInterface, UpdateUrlFunction } from './lib/defs' |
6 | 6 |
|
7 | 7 | function useNuqsTanstackRouterAdapter(watchKeys: string[]): AdapterInterface { |
| 8 | + const pathname = useLocation({ select: state => state.pathname }) |
8 | 9 | const search = useLocation({ |
9 | 10 | select: state => |
10 | 11 | Object.fromEntries( |
11 | 12 | Object.entries(state.search).filter(([key]) => watchKeys.includes(key)) |
12 | 13 | ) |
13 | 14 | }) |
14 | 15 | const navigate = useNavigate() |
15 | | - const from = useMatches({ |
16 | | - select: matches => |
17 | | - matches.length > 0 |
18 | | - ? (matches[matches.length - 1]?.fullPath as string) |
19 | | - : undefined |
20 | | - }) |
21 | 16 | const searchParams = useMemo( |
22 | 17 | () => |
23 | 18 | // search is a Record<string, string | number | object | Array<string | number>>, |
@@ -56,20 +51,18 @@ function useNuqsTanstackRouterAdapter(watchKeys: string[]): AdapterInterface { |
56 | 51 | // TBC if it causes issues with consuming those search params |
57 | 52 | // in other parts of the app. |
58 | 53 | // |
59 | | - // When we clear the search, passing an empty string causes |
60 | | - // a type error and possible basepath issues, so we switch it to '.' instead. |
61 | | - // See https://github.com/47ng/nuqs/pull/953#issuecomment-3003583471 |
62 | | - to: renderQueryString(search) || '.', |
63 | | - // `from` will be handled by tanstack router match resolver, code snippet: |
64 | | - // https://github.com/TanStack/router/blob/5d940e2d8bdb12e213eede0abe8012855433ec4b/packages/react-router/src/link.tsx#L108-L112 |
65 | | - ...(from ? { from } : {}), |
| 54 | + // Note: we need to specify pathname + search here to avoid TSR appending |
| 55 | + // a trailing slash to the pathname, see https://github.com/47ng/nuqs/issues/1215 |
| 56 | + from: '/', |
| 57 | + to: pathname + renderQueryString(search), |
66 | 58 | replace: options.history === 'replace', |
67 | 59 | resetScroll: options.scroll, |
68 | | - hash: prevHash => prevHash ?? '' |
| 60 | + hash: prevHash => prevHash ?? '', |
| 61 | + state: state => state |
69 | 62 | }) |
70 | 63 | }) |
71 | 64 | }, |
72 | | - [navigate, from] |
| 65 | + [navigate, pathname] |
73 | 66 | ) |
74 | 67 |
|
75 | 68 | return { |
|
0 commit comments