Skip to content

Commit

Permalink
fix: Reorder optimistic shallow URL update & router call
Browse files Browse the repository at this point in the history
  • Loading branch information
franky47 committed Oct 28, 2024
1 parent cdbe5ec commit a5434ac
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions packages/nuqs/src/adapters/next/impl.app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,15 @@ export function useNuqsNextAppRouterAdapter(): AdapterInterface {
// App router
const url = renderURL(location.origin + location.pathname, search)
debug('[nuqs queue (app)] Updating url: %s', url)
// First, update the URL locally without triggering a network request,
// this allows keeping a reactive URL if the network is slow.
if (!options.shallow) {
// Call the Next.js router to perform a network request
// and re-render server components.
router.replace(url, {
scroll: false
})
}
// Then, optimistically update the URL locally without triggering a network
// request, this allows keeping a reactive URL if the network is slow.
const updateMethod =
options.history === 'push' ? history.pushState : history.replaceState
updateMethod.call(
Expand All @@ -26,13 +33,6 @@ export function useNuqsNextAppRouterAdapter(): AdapterInterface {
if (options.scroll) {
window.scrollTo(0, 0)
}
if (!options.shallow) {
// Call the Next.js router to perform a network request
// and re-render server components.
router.replace(url, {
scroll: false
})
}
}, [])
return {
searchParams,
Expand Down

0 comments on commit a5434ac

Please sign in to comment.