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.

fail to work with Next.js Client-side navigations ( next/router, next/link )

See original GitHub issue

Hi. I’m using search-ui with my Next.js project. But I found that when I navigate away from the search page to another page using the Client-side navigations, I cannot go back by using the browser “back” button (I’ve only tried chrome.)

It seems to be the only lib that support elastic-app-search. Is it correct?

How to replicate:

  1. Run this demo repo
  2. visit search page localhost:3001/search
  3. click go home page at the top of sidebar
  4. when home page complete rendering, click your browser back button like this: image

Problematic result: The URL do change to localhost:3001/search?...... but page remains in home page. image

I’m not sure this issue come from search-ui, not from Next.js. I guess this issue has something relate to Smart URL – which I want to understand more but couldn’t find much documents.

BTW I also tried search-ui with react router, they worked well with each other.

Any hint?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:14 (2 by maintainers)

github_iconTop GitHub Comments

6reactions
Yurii-Adamenkocommented, Apr 26, 2021

We also have this issue. Our workaround:

  1. Change trackUrlState to false (it actually fixes the issue, but if you want track URL state go to 2 and 3).
  2. Use onSearch to push state to query (you can use https://github.com/elastic/search-ui/blob/cb774e2ab1e6831ff40523c62d76b57d4c71ed5f/packages/search-ui/src/URLManager.js or write your own)
   onSearch: (state, queryConfig, next) => {
      const searchString = stateToQueryString(state)

      router.replace(
        `${window.location.pathname}?${searchString}`,
        undefined,
        { shallow: true }
      )

      return next(state, queryConfig)
    }
  1. Take the query params and set it to initialState on the component mount

Hope this will help

2reactions
jason-dencommented, Apr 28, 2021

@Yurii-Adamenko Nice workaround.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Routing: Linking and Navigating - Next.js beta docs
Learn how to use the Link Component, useRouter Hook, and navigate between routes in your Next.js application.
Read more >
client side navigation using 'next/link' not working #618 - GitHub
In each page I am importing header.js and respective components for that page. Code inside header.js is following : import Link from 'next/link' ......
Read more >
Client-Side Routing In Next.js - Smashing Magazine
Next.js has a file-based routing system in which each page automatically becomes a route based on its file name. This article will guide...
Read more >
External link is not working in Next.js when you want to use ...
To add an external link to the Next.js Link component we should use ... use the next/link for external links, as it's only...
Read more >
Dealing with links in Next.js - LogRocket Blog
I use Next as an example, but they can be applied to pretty much all routing libraries: Use a custom Link component; Have...
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