Next.js shallow routing is not functioning as expected when a query string is added

The issue at hand:

When attempting shallow routing in Next.js by changing the query string (adding or removing, but not updating), the page is reloaded and the shallow option is ignored.

Is there a way to prevent reloading while modifying the query string?


Illustrative Example:

Transforming from: example.com/page1/?a=2

To: example.com/page1/?a=2&b=3


Solution Attempt:

const newSearch = '?a=2&b=3'
router.push(`/page1/${newSearch}`, null, { shallow: true })

I have also tried:

const newSearch = '?a=2&b=3'
router.push(`/page1/${newSearch}`, `/page1/${newSearch}`, { shallow: true })

Answer №1

When using the router.push method in Next.js, make sure to include the final query parameters by joining them with an ampersand (&) and passing them as a URL string.

Answer №2

For this particular scenario where I encountered an Automatic static optimization, I made the decision to disable the use of shallow. This helped me avoid any issues with query string updates. Interestingly, even when disabled, I noticed that the getServerSideProps function still runs consistently.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Django does not support CORS headers

As I work on building an API in Django and utilizing Next.js for the frontend, I consistently encounter an issue when trying to communicate between the backend and frontend. The console displays the following message: Access to XMLHttpRequest at 'http ...

Guide to integrating MPA frontend with SSR backend using Next.js?

I recently purchased an MUI template for my project. Utilizing React with Next.js and Flask as the backend, the MUI template functions as a Multiple-Page Application. In the past, I have utilized Flask for a Single Page Application by simply using return s ...