URL state "flickers" before settling in Next.js App Router #714
-
Hello! I'm experiencing the state in the URL "flickering" before it settles. I'm using the Next.js App Router. The URL params are being used to render a server component which retriggers on URL change. Pay attention to the URL after I click. Oct-27-2024.14-06-13.mp4// CategoryFilter.tsx
const categoriesMap = use(categoriesPromise);
const [isPending, startTransition] = useTransition();
const [categories, setCategories] = useQueryState(
'category',
searchParams.category.withOptions({
shallow: false,
startTransition,
}),
);
return (
<div data-pending={isPending ? '' : undefined}>
<ToggleGroup
toggleKey="category"
options={Object.values(categoriesMap).map(category => {
return {
label: category.name,
value: category.id.toString(),
};
})}
selectedValues={categories}
onToggle={newCategories => {
setCategories(newCategories);
}}
/> //searchParams.ts
import { parseAsString, createSearchParamsCache, parseAsArrayOf } from 'nuqs/server';
export const searchParams = {
category: parseAsArrayOf(parseAsString).withDefault([]),
q: parseAsString.withDefault(''),
};
export const searchParamsCache = createSearchParamsCache(searchParams); I'm not sure what I'm doing wrong here; however, I'm pretty new to Nuqs. ...
const [optimisticCategories, setOptimisticCategories] = useOptimistic(categories);
...
selectedValues={optimisticCategories}
onToggle={newCategories => {
startTransition(() => {
setOptimisticCategories(newCategories);
setCategories(newCategories);
}); Repo with full code: https://github.com/aurorascharff/next15-filterlist/blob/filter-nuqs/components/CategoryFilter.tsx |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 7 replies
-
Thanks, I managed to reproduce it locally. TL;DR: I think this can be escalated to a bug, which has to do with the sync mechanism nuqs uses internally to follow updates of the internal state of the app router (ie: when Let's take the example of starting from a blank URL Looking at the logs, we can see the following: Sequence of eventsFirst button clicked (Infrastructure)
At this stage:
Second button clicked (Frontend)
At this stage:
First RSC request resolvesThis is where the problems start to occur. The first RSC request (with
Those incorrect values for useSearchParams could be due to the RSC payload changing the internal state of the app router. We can actually see the URL flickering too. The problem that causes the visual flicker of the button is that nuqs picks up the incorrect update and syncs onto it (setting the categories state to Second RSC request resolvesAnd resets everything back to normal (eventual consistency) |
Beta Was this translation helpful? Give feedback.
-
I'm not entirely sure if I can help here... If you set multiple query params inside a transition, they're supposed to be batched before the UI updates. I did however experience bugs with this recently, and Sam filed a bug report on it: vercel/next.js#70977 |
Beta Was this translation helpful? Give feedback.
-
I think I have a fix in #718 ! Could you try this please and let me know if it works for you? (it works on my local copy of your filter list app): npm i https://pkg.pr.new/nuqs@9b07e04ec6295f592d2e4624f0837166f96928c1 The fix was to wrap the |
Beta Was this translation helpful? Give feedback.
-
That makes sense! That's what I did to fix it as well! :D I will try it now. |
Beta Was this translation helpful? Give feedback.
🎉 This issue has been resolved in version 2.1.0 🎉
The release is available on: