Skip to content

Commit

Permalink
fix(routing): manually push changed path (#6385)
Browse files Browse the repository at this point in the history
Instead of using next's `router.push` function, we're transitioning back to `history.pushState`. This works better in many edge cases as it prevents a whole page refresh and rerender.

Possible intended side effect of this fix is of course that any global change handlers or code running in render won't run anymore when changing instantsearch state. This can be worked around by using the `onStateChange` function or a middleware.

Back navigation still works as expected.
  • Loading branch information
Haroenv authored Oct 9, 2024
1 parent 39a9107 commit e5f0bdd
Showing 1 changed file with 2 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import historyRouter from 'instantsearch.js/es/lib/routers/history';
import { headers } from 'next/headers';
import { usePathname, useSearchParams, useRouter } from 'next/navigation';
import { usePathname, useSearchParams } from 'next/navigation';
import { useRef, useEffect } from 'react';

import type { InstantSearchNextProps } from './InstantSearchNext';
Expand All @@ -17,7 +17,6 @@ export function useInstantSearchRouting<
) {
const pathname = usePathname();
const searchParams = useSearchParams();
const router = useRouter();
const routingRef =
useRef<InstantSearchProps<TUiState, TRouteState>['routing']>();
const onUpdateRef = useRef<() => void>();
Expand Down Expand Up @@ -54,7 +53,7 @@ export function useInstantSearchRouting<
if (this.isDisposed) {
return;
}
router.push(url, { scroll: false });
history.pushState({}, '', url);
};
browserHistoryOptions.start = function start(onUpdate) {
onUpdateRef.current = onUpdate;
Expand Down

0 comments on commit e5f0bdd

Please sign in to comment.