Replies: 3 comments 6 replies
-
The issue occurs because Next.js does not automatically trigger a Why
|
Beta Was this translation helpful? Give feedback.
-
@lsli8888 You’re seeing empty router.push(`/details/${id}?${searchParams.toString()}`); |
Beta Was this translation helpful? Give feedback.
-
Hi, did you find any reason why popstate event is not getting triggered in next14? |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Summary
I'm currently using nextjs 14.2.3 (app router).
I have an item search page which contains a datagrid, with each row a clickable item that when clicked takes you to an item details page. There are numerous search filters in the datagrid and I have a useEffect hook that automatically pushes an updated URL with the query parameters after a change in any of the search filter options. For example, if the user clicks on two options that selects regions AK and AF, the URL becomes something like https://myexample.com/?regions=AK%2CAF. It makes use of window.history.pushState to modify the browser URL. Unfortunately, I cannot use router.push as that causes a full page refresh - and my filter options are in a small side pane that opens and closes. router.push would close the side pane rather than leave it open.
I also have the following code that allows one to press the back button among changes to the filter options and keeps the URL in sync with the data that's shown:
This works well to allow a user to go back and forth in this search page between changes in filter options.
The problem comes when the user clicks on an item in the search list which takes him to the details page, and then clicks on the back button. When this occurs, the user is not taken to the previous search page in the history stack, but rather the initial search page before all the filter options changes are made.
Here's the code snippet that shows the MUI Link component used:
I intentionally call router.push here so that I keep some of state on the router so that it can help it with the back button. I've also tried replacing router.push with window.history.pushState with the same result.
My details page has a URL slug pattern like so (not sure if it makes a difference):
src/app/details/[id]
The details page also has a similar useEffect hook that adds the event listener to listen to popstate:
Again, this doesn't work when I press the back button from the details page in an attempt to go back to the latest search page. I can tell the handlePopState function is never called (when I added logs to handlePopState). What am I doing wrong here?
Additional information
No response
Example
No response
Beta Was this translation helpful? Give feedback.
All reactions