Shallow routing on Next.js 13's next/navigation #48110
-
SummaryCurrently in Next.js 13 when adding / removing the search params, I always get a hard-reload. In Next.js 12, one could do something like
How can one achieve the Next.js 12 shallow routing in the current version? Additional informationNo response ExampleNo response |
Beta Was this translation helpful? Give feedback.
Replies: 43 comments 142 replies
-
Hey, you can achieve the same result by putting the parts you don't want to re-render into the layout file. |
Beta Was this translation helpful? Give feedback.
-
Hey. I've just faced the same behavior. const params = new URLSearchParams(searchParams);
params.set('countryCode', newCode);
const newParams = params.toString();
router.push(`${pathname}?${newParams}`); And it completely reloads the page and triggers the data fetching in my server component container. |
Beta Was this translation helpful? Give feedback.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
-
Would also be nice to be able to change a dynamic route segment without triggering a reload. For example if there is an overlay component we want to show or hide based on the dynamic route segment, we don't want to make this a route because then every time the route changes we need to wait for the page's markup to come from the server, despite having both the component and data already on the client. So an option to navigate between the dynamic segments without triggering a page reload would be nice. Unfortunatly the workaround is a catch-all route and a client-side router (in this case, Wouter). |
Beta Was this translation helpful? Give feedback.
This comment was marked as off-topic.
This comment was marked as off-topic.
-
I have a similar problem with current routing behavior or rather the lack of shallow routing. My use case: I display a forum divided into three components, which are a list of topics of a selected board, a comments-list for a selected topic and finally a post for the selected comment, therefore i have an url similar to /board/[boardId]/topic/[topicId]/post/[postId] Now, clicking on a topic item should reload the comments list but not re-render the topics list itself which would also change the location within the list to the top position again which is not desired. Clicking on a comment-link in the comment list should also not re-render the topics-list but only update the post component which the new data. Right now, I must simulate the routing by pushing the new url after clicking on an item in either list to the windows.history object using onClick-handlers in all links. Then I evaluate the parameters by parsing that new url and finally I put those values into a context which the three components consume. Clearly a cumbersome and awkward workaround to only get the specific components updated where the relevant url parameter changed. I also checked the latest introduced router features (parallel routing and intercepting) but I did not manage to apply either for my use case. If anyone have a different suggestion on how to handle that case, kindly let me know. -act |
Beta Was this translation helpful? Give feedback.
-
This was fixed in |
Beta Was this translation helpful? Give feedback.
This comment was marked as off-topic.
This comment was marked as off-topic.
-
Hard reload does not happen anymore but still you cannot do shallow routing. If I change search param, all the server code is rerun and fetches data on the server again. I created separate discussion but @AndreSilva1993 consider opening this one up again. |
Beta Was this translation helpful? Give feedback.
-
I was doing a migration from my pages folder to the new app folder and used shallow routing a lot to get the functionality I desired. Is there a reason shallow routing functionality does not exist in the new App folder with NextJS Router (next/navigation) anymore and can this functionality be added to it? Until then I have managed to get a workaround to this problem by using |
Beta Was this translation helpful? Give feedback.
-
I have released It does so by using the web history API directly in shallow mode, and the Next.js router when in non-shallow mode. Everything plays nice with standard There's a playground to try it out: https://nuqs.47ng.com I wrote a blog post on how it works under the hood, there were a few interesting findings along the way in building it. |
Beta Was this translation helpful? Give feedback.
-
Why was my comment marked off-topic when it offered a solution to this problem? If my comment is considered off topic then please mark all other topics that try to offer solutions as off topic as well. |
Beta Was this translation helpful? Give feedback.
-
In the meanwhile, for anyone who simply wants to change the URL without any additional functionality and just for the sake of showing a different URL, keep in mind that you can always change the URL with the native browser history api like so:
Hope this helps for some of you! |
Beta Was this translation helpful? Give feedback.
-
Is there solution for this problem? |
Beta Was this translation helpful? Give feedback.
-
Any updates on this issue ? |
Beta Was this translation helpful? Give feedback.
-
+1️⃣ on this issue, major blocker |
Beta Was this translation helpful? Give feedback.
This comment was marked as spam.
This comment was marked as spam.
This comment was marked as off-topic.
This comment was marked as off-topic.
-
Voi di Vercel siete un po' scappati di casa, ne? @timneutkens @leerob |
Beta Was this translation helpful? Give feedback.
-
edit: misread the pr |
Beta Was this translation helpful? Give feedback.
-
Hey all, I've landed #58335 on canary which allows you to get pretty much the same behavior as Edit: This is now live: https://nextjs.org/docs/app/building-your-application/routing/linking-and-navigating#using-the-native-history-api |
Beta Was this translation helpful? Give feedback.
-
I also dont understand the implementation of searchparams for state when any state changes and transitions are lagged out to the max. The app router is such a cataclysmic disappointment in 8 of 10 cases that I seriously considering supporting alternatives. |
Beta Was this translation helpful? Give feedback.
-
I have successfully been using nuqs and next-intl together in production,
since about the time nuqs came out in a pages router project, and a little
more recently in an app router project. Both have been without any
particular issue related to nuqs.
…On Reiwa 6 Sep 23, Mon at 5:39 François Best ***@***.***> wrote:
@abhion <https://github.com/abhion> I did a quick test and nuqs (latest,
1.19.1) & next-intl seem to work fine together (source
<https://github.com/franky47/nuqs-next-intl>):
https://github.com/user-attachments/assets/bf7d9708-378c-48c9-b235-aecabbba6dee
—
Reply to this email directly, view it on GitHub
<#48110 (reply in thread)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAAPSMVELCNCLXVH77527GLZX4TIHAVCNFSM6AAAAAAWW72FSKVHI2DSMVQWIX3LMV43URDJONRXK43TNFXW4Q3PNVWWK3TUHMYTANZSGAYTONQ>
.
You are receiving this because you commented.Message ID: <vercel/next.
***@***.***>
|
Beta Was this translation helpful? Give feedback.
Hey all, I've landed #58335 on canary which allows you to get pretty much the same behavior as
shallow
inpages
where it keeps the internal route state up-to-date but doesn't trigger an external fetch to the server. The same plumbing was needed for thenavigate
event in the future so this is an early version that solves most "shallow routing" requirements too.Edit: This is now live: https://nextjs.org/docs/app/building-your-application/routing/linking-and-navigating#using-the-native-history-api