简体   繁体   English

不是 Next.js 中的完整文件名的蛞蝓?

[英]Slug that isn't whole file name in Next.js?

I'm attempting to create a route in Next.js that follows a format like:我正在尝试在 Next.js 中创建一个遵循如下格式的路由:

localhost:3000/post-SLUG

By creating a file with the name:通过创建一个文件名:

/pages/post-[slug].js

When I navigate to an example like localhost:3000/post-example I get a 404. Is it possible to do this type of routing?当我导航到localhost:3000/post-example类的示例时,我得到一个 404。是否可以执行这种类型的路由? I realize that this is probably not the best URL setup, but I'm trying to match URLs from an already deployed site as a part of a migration to Next.我意识到这可能不是最好的 URL 设置,但作为迁移到 Next 的一部分,我正在尝试匹配来自已部署站点的 URL。

It's not possible to do it with Next.js routing system because in order to be dynamic the part of URL must match this regular expression:使用 Next.js 路由系统是不可能的,因为为了动态,URL 的部分必须匹配这个正则表达式:

/\/\[[^/]+?\](?=\/|$)/

So the route have to be /post/[slug]/ .所以路线必须是/post/[slug]/

The /pages/post-[slug].js would be accessible as a static route http://localhost:3000/post-[slug] . /pages/post-[slug].js可以作为 static 路由http://localhost:3000/post-[slug]访问。

You can utilize a Custom Server to create this kind of routes.您可以使用自定义服务器来创建这种路由。

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

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