问题描述
我在 user 页面的生命周期方法中有一个条件,如果用户输入带有某些参数的 URL,例如 somePage.com/user/1
将获取ID 为 1 的用户的数据,但是,如果用户输入 URL 作为 somePage.com/user/1ABC
然后我将它们路由到自定义 404 页面.我的问题是,当我在 404 页面中单击后退按钮时,我将返回带有 URL somePage.com/user/1ABC
的页面,然后将路由回与相同的 404 页面URL 参数满足用户页面生命周期方法中的条件.
I have a condition inside my lifecycle method of user Page such that if the user enters an URL with certain parameters such as somePage.com/user/1
will fetch the data of the user with ID 1, however, if the user enters the URL as somePage.com/user/1ABC
then im routing them to a custom 404 page. My issue is when im in the 404-page clicking on the back button will take me back to the page with URL somePage.com/user/1ABC
and then will route back to the same 404-page as the URL param met a condition in the lifecycle method of the user page.
如果用户进入 404 页面并点击浏览器的后退按钮,我想将用户路由到其他某个 URL.
I would like to route the user to some other URL if they land up in the 404-page and click on the back button of the browser.
推荐答案
Router.push 会将新的 URL 添加到历史堆栈中,但是,Router.replace 将重定向到 404 页面,而不会将新的 URL 条目添加到历史堆栈中历史堆栈.所以后退按钮将重定向到历史堆栈中最后一个成功的条目,https://nextjs.org/docs/api-reference/next/router#routerreplace 给出了一个清晰的例子.
Router.push will add the new URL into the history stack, however, Router.replace will redirect to the 404-page without adding a new URL entry into the history stack. So the back button will redirect to the last successful entry in the history stack, https://nextjs.org/docs/api-reference/next/router#routerreplace gives a clear example.
这篇关于NextJs:404页面返回按钮返回404页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!