How to push entries to browser history on button click only? #816
-
Hi, thanks so much for your work on I have a server component passing data to a client component. In the client component, I have an input field that accepts a number: const [numParam, setNumParam] = useQueryState("num", parseAsInteger.withDefault(0)); New data is fetched with the new parameters on button click: <Button
disabled={isLoading}
onClick={() => {
startTransition(() => {
router.push(`/myRoute?${searchParams}`);
});
}}
>
Search
</Button> I want to sync its state with the URL on the client side, but I only want to push entries to the browser history on button click, not on state change. How can I achieve this? If I set This creates a bunch of history entries where data for those parameters was never fetched. For example:
However, if I set I don't want to fetch data with throttling on each state change either. I can't seem to find anything in the docs that helps me achieve this. Am I approaching this problem wrong? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
A combination of things to try:
If you are on Next.js 14, be aware that there is a client cache that causes RSCs to be cached client-side, meaning if going back to a URL that previously fetched data, it won't invalidate/re-render server side unless this cache is cleared or disabled. This was disabled in Next.js 15 IIRC. |
Beta Was this translation helpful? Give feedback.
A combination of things to try:
shallow: false
option in useQueryState to update the server whenever thenum
search param changes (that would take care of the back button giving you the right data)defaultValue
from useQueryState, but URL updates only happen when clicking the button (the serializer in nuqs might help with generating a link from the input's internal value).If you are on Next.js 14, be aware that there is a client cache that causes RSCs to be cached client-side, meaning if going back to a URL that previously fetched data, it won't invalidate/re-render server side unless this cache is cleared or disabled.
https://next…