简体   繁体   English

在 next.js 中使用浏览器后退按钮时,动态路由页面不会出现

[英]Daynamic routing page in not coming, when use browser back button in next js

In a Next.js project, I am coming across a situation where I get a new URL path from an API. And I have to update that URL path in the browser without reloading that page.在一个 Next.js 项目中,我遇到了一种情况,我从 API 获得了一个新的 URL 路径。我必须在浏览器中更新该 URL 路径而不重新加载该页面。

I am doing that with the help of History.pushState()我在History.pushState()的帮助下这样做

window.history.pushState(nextState,nextTitle,nextURL);
window.history.replaceState(nextState,nextTitle,nextURL);

With help of History.pushState() I am able to update the URL in the browser without reloading that page.History.pushState()的帮助下,我能够在浏览器中更新 URL 而无需重新加载该页面。 But If after I change that URL multiple times and change to some other page.但是如果我多次更改 URL 并更改到其他页面。 Then if I start pressing the browser back button, I am not able to get that page with the last URL path showing in the browser.然后,如果我开始按下浏览器后退按钮,我将无法获得浏览器中显示的最后一个 URL 路径的页面。

Only that last URL path shows up in the browser URL input, but that page data is not reflected.只有最后一个 URL 路径显示在浏览器 URL 输入中,但页面数据没有反映出来。 Attaching the sample code and reproducing steps video link below.在下面附上示例代码和重现步骤视频链接。 Any help and suggestions are appreciated.任何帮助和建议表示赞赏。

Source Code源代码

Codesandbox 代码沙盒

Reproducing Steps重现步骤

I am also facing same problem, after using history.pushState() browser back button will not work in that page, because according to browser you never went to that page.我也面临同样的问题,在使用history.pushState()后,浏览器后退按钮将无法在该页面中工作,因为根据浏览器,您从未访问过该页面。 That location doesn't exists in JavaScript history object.该位置在 JavaScript 历史记录 object 中不存在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM