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 while using withRouter not working

See original GitHub issue

Bug report

Describe the bug

Using withRouter as a wrapper, shallow routing doesn’t seem to be working.

To Reproduce

I currently use this method to change the route:

        this.props.router.push({
            pathname: currentPath,
            query: currentQuery,
        });

And couldn’t figure where to put the shallow flag. So I switched to the method mentioned in the docs: this.props.router.push('/post/[pid]?hello=123', '/post/abc?hello=123', { shallow: true })

So I did that manually, but I started getting 404s.

http://localhost:3000/_next/static/development/pages/search/%5Btype%5D/%5Bcat%5D/%5Barea%5D.js net::ERR_ABORTED 404 (Not Found)

decoded: "http://localhost:3000/_next/static/development/pages/search/[type]/[cat]/[area].js"

I tried using :type instead of [type] but it also didn’t work.

This is how it’s configured on the server:

                if ('/search/:type/:cat/:area' === route.path) {
                    return app.render(req, res, route.page);
                }

Folder Structure: /pages/search/index.js

Expected behavior

It should not reload the page while changing the route, that’s the main thing I’m trying to accomplish.

System information

  • OS: [Ubuntu 18.04]
  • Browser (if applies) [chrome]
  • Version of Next.js: [9.2.1]
  • Version of Node.js: [12.18.0]

Additional context

I’m implementing SSR pagination, and I’m planning to use shallow routing to make page changes happen on the client instead of the server. Meaning achieve SSR of first load only, keep user in without refreshing.

This is might be related #4545

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
jamesmosiercommented, Jul 14, 2020

My working test case just displays what you mentioned originally in this issue that shallow rendering works with a custom server and dynamic routes. It:

added a query param without reloading the page

which should resolve this issue.


As for your other issue, I’m not sure what that tests case is supposes to do.

`/search?type=foo&cat=bar&area=baz&counter=${counter}`,
`/search/foo/bar/baz?counter=${counter + 1}`,

would be

`/search?type=foo&cat=bar&area=baz&counter=0`,
`/search/foo/bar/baz?counter=1`,

which would be different counter values

1reaction
jamesmosiercommented, Jul 14, 2020

This should work:

  server.get('/search/:type/:cat/:area', (req, res) => {
    return app.render(req, res, '/search', {
      ...req.params,
      ...req.query,
    });
  });

props.router.push(
  '/search?type=foo&cat=bar&area=baz&counter=10',
  '/search/foo/bar/baz?counter=10',
  { shallow: true }
);
Read more comments on GitHub >

github_iconTop Results From Across the Web

Shallow routing using withRouter and custom server not working
I'm implementing SSR pagination, and I'm planning to use shallow routing to make page changes happen on the client instead of the server. ......
Read more >
Shallow routing using withRouter and custom server not ...
Coding example for the question Shallow routing using withRouter and custom server not working-Reactjs.
Read more >
Shallow Routing - Next.js
When shallow routing is used with middleware it will not ensure the new page matches the current page like previously done without middleware....
Read more >
Routing in Next.js - Telerik
In cases where the rendering of an application involves fetching data from some external sources, shallow routing involves changing the URL ...
Read more >
Understanding Next.js routeChangeStart and router events
Routing in Next.js is based on the concept of pages. ... weird naming structure in the image above — we will fix that...
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