-
Notifications
You must be signed in to change notification settings - Fork 499
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(next-app-router): listen to external URL changes (#6107)
- Loading branch information
1 parent
1778b31
commit 6f1f40b
Showing
2 changed files
with
78 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
packages/react-instantsearch-nextjs/src/useInstantSearchRouting.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import historyRouter from 'instantsearch.js/es/lib/routers/history'; | ||
import { headers } from 'next/headers'; | ||
import { usePathname, useSearchParams, useRouter } from 'next/navigation'; | ||
import { useRef, useEffect } from 'react'; | ||
|
||
import type { InstantSearchNextProps } from './InstantSearchNext'; | ||
import type { UiState } from 'instantsearch.js'; | ||
import type { BrowserHistoryArgs } from 'instantsearch.js/es/lib/routers/history'; | ||
import type { InstantSearchProps } from 'react-instantsearch-core'; | ||
|
||
export function useInstantSearchRouting< | ||
TUiState extends UiState = UiState, | ||
TRouteState = TUiState | ||
>( | ||
passedRouting: InstantSearchNextProps<TUiState, TRouteState>['routing'], | ||
isMounting: React.MutableRefObject<boolean> | ||
) { | ||
const pathname = usePathname(); | ||
const searchParams = useSearchParams(); | ||
const router = useRouter(); | ||
const routingRef = | ||
useRef<InstantSearchProps<TUiState, TRouteState>['routing']>(); | ||
const onUpdateRef = useRef<() => void>(); | ||
useEffect(() => { | ||
if (onUpdateRef.current) { | ||
onUpdateRef.current(); | ||
} | ||
}, [pathname, searchParams]); | ||
|
||
if (passedRouting && !routingRef.current) { | ||
let browserHistoryOptions: Partial<BrowserHistoryArgs<TRouteState>> = {}; | ||
|
||
browserHistoryOptions.getLocation = () => { | ||
if (typeof window === 'undefined') { | ||
const url = `${ | ||
headers().get('x-forwarded-proto') || 'http' | ||
}://${headers().get('host')}${pathname}?${searchParams}`; | ||
return new URL(url) as unknown as Location; | ||
} | ||
|
||
if (isMounting.current) { | ||
return new URL( | ||
`${window.location.protocol}//${window.location.host}${pathname}?${searchParams}` | ||
) as unknown as Location; | ||
} | ||
|
||
return window.location; | ||
}; | ||
browserHistoryOptions.push = function push( | ||
this: ReturnType<typeof historyRouter>, | ||
url | ||
) { | ||
// This is to skip the push with empty routeState on dispose as it would clear params set on a <Link> | ||
if (this.isDisposed) { | ||
return; | ||
} | ||
router.push(url, { scroll: false }); | ||
}; | ||
browserHistoryOptions.start = function start(onUpdate) { | ||
onUpdateRef.current = onUpdate; | ||
}; | ||
|
||
routingRef.current = {}; | ||
if (typeof passedRouting === 'object') { | ||
browserHistoryOptions = { | ||
...browserHistoryOptions, | ||
...passedRouting.router, | ||
}; | ||
routingRef.current.stateMapping = passedRouting.stateMapping; | ||
} | ||
routingRef.current.router = historyRouter(browserHistoryOptions); | ||
} | ||
|
||
return routingRef.current; | ||
} |