简体   繁体   English

如果路由以斜线结尾,则找不到 Next.js 页面

[英]Next.js page not found if route ends with a slash

I am currently working on a next.js application.我目前正在开发next.js应用程序。 The issue i am facing is with next/link .我面临的问题是next/link Link works well if i am navigating through the application.如果我在应用程序中导航,链接效果很好。 Every page is rendered properly.每个页面都正确呈现。 The problem shows up when i refresh a page and the URL ends with a slash.当我刷新页面并且 URL 以斜杠结尾时,问题就会出现。

http://localhost:3000/about/

The above route is showing "Page not found" while the following route will load the page successfully.上面的路由显示“找不到页面”,而下面的路由将成功加载页面。

http://localhost:3000/about

I am using link as follow:我使用的link如下:

<Link href="/about">
   <a>About</a>
</Link>

Any help will be appreciated.任何帮助将不胜感激。 Thanks.谢谢。

I hope there should be an easiest way to do that.我希望应该有一个最简单的方法来做到这一点。 However you can do this using custom server .但是,您可以使用自定义服务器来执行此操作。 Here is an example:-这是一个例子:-

server.get("/about/", (req, res) => {
  return app.render(req, res, "/about")
})

I worked around this issue by switching out the tags for a simple tag.我通过将标签切换为简单标签来解决此问题。 Not ideal but at least it prevented the default handling of the trailling slash by nextjs.不理想,但至少它阻止了 nextjs 对尾部斜杠的默认处理。

<a href="/about">About</a>

In my case I was directing to a page where I had no control over the url format.在我的情况下,我指向一个我无法控制 url 格式的页面。

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

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