question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Shallow routing for dynamic routes

See original GitHub issue

Hi. Is there a way with next.js v9 to navigate from /post/123 to /post/124 shallowly? Here’s my page structure:

pages
  post
    [id]

Tried

const href = `/post/124`
const as = href
Router.push(href, as, {shallow: true})

when I’m on /post/123 page with no luck (the page refreshes).

What I need to achieve - I have a sidebar with posts list on the left of the page and selected post on the right. So when clicking a post title in sidebar I show it on the right and I also need to update the url, without refreshing the page.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:21 (11 by maintainers)

github_iconTop GitHub Comments

58reactions
sgmondacommented, Jul 25, 2019

Oh my god, I was understanding [postId] as a macro to be replaced by me when coding but, in fact, It is [postId] literally what have to be wroten. Now that I’ve understood that it works like a charm.

Sorry and thank you very much @Timer @timneutkens for your patience. 😃

For future stupids, like me, what have to be replaced during your program execution is the as part but not the href (that is a literal string declaring your filesystem path):

            Your code shouldn't
                 change this
              during execution
                     |
                     v
<Link href="/post/[postId]" as={`/post/${id}`}>
  <a>First Post</a>                      ^
</Link>                                  |
                                  This is what you
                             have to modify dynamically
                                during the execution
10reactions
timneutkenscommented, Jul 25, 2019

href is the path inside the pages directory as is the url in the browser

See docs here: https://nextjs.org/docs#dynamic-routes

Read more comments on GitHub >

github_iconTop Results From Across the Web

Shallow Routing - Next.js
Shallow routing allows you to change the URL without running data fetching methods again, that includes getServerSideProps , getStaticProps , and ...
Read more >
Next.js - Shallow routing with dynamic routes - Stack Overflow
Shallow Routing gives you the ability to update pathname or query params without losing state i.e., only the state of route is changed.....
Read more >
How to Change URLs with Shallow Routing in Next.js - Netlify
Shallow routing is when you change your website's URL without re-running data fetching methods again. In the case of Next.js, it means you...
Read more >
Routing in Next.js - Topcoder
Routing with pages. Nested routes. Dynamic routes. Catch-all routes. Navigate from UI. To get started, create a Next.js application using ...
Read more >
Understanding Next.js routeChangeStart and router events
Shallow routing allows you to make changes to the path of a page without running data fetching methods again. It helps retain the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found