Skip to content

Commit

Permalink
fix: Use optimistic search params in app router (#718)
Browse files Browse the repository at this point in the history
* fix: Use optimistic search params

See #714.
  • Loading branch information
franky47 authored Oct 29, 2024
1 parent c0bb912 commit b5c0168
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions packages/nuqs/src/adapters/next/impl.app.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import { useRouter, useSearchParams } from 'next/navigation'
import { useCallback } from 'react'
import { useCallback, useOptimistic } from 'react'
import { debug } from '../../debug'
import type { AdapterInterface, UpdateUrlFunction } from '../defs'
import { renderURL } from './shared'

export function useNuqsNextAppRouterAdapter(): AdapterInterface {
const router = useRouter()
const searchParams = useSearchParams()
const [optimisticSearchParams, setOptimisticSearchParams] =
useOptimistic<URLSearchParams>(searchParams)
const updateUrl: UpdateUrlFunction = useCallback((search, options) => {
// App router
setOptimisticSearchParams(search)
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,
Expand All @@ -35,7 +38,7 @@ export function useNuqsNextAppRouterAdapter(): AdapterInterface {
}
}, [])
return {
searchParams,
searchParams: optimisticSearchParams,
updateUrl,
// See: https://github.com/47ng/nuqs/issues/603#issuecomment-2317057128
rateLimitFactor: 2
Expand Down

0 comments on commit b5c0168

Please sign in to comment.