[英]Can I use Next.js Link to navigate from slug page to the default one?
So I have the default page and some 'slug' dynamic pages, the urls look like this:所以我有默认页面和一些“slug”动态页面,网址如下所示:
default page locally: localhost:3000/doc/default
本地默认页面:
localhost:3000/doc/default
slug page locally: localhost:3000/doc/[slug]
本地 slug 页面:
localhost:3000/doc/[slug]
default page in production: default.com/doc
生产中的默认页面:
default.com/doc
slug page in production: [slug].default.com/doc
生产中的 slug 页面:
[slug].default.com/doc
The link I need should be inside the slug pages and lead to the default one.我需要的链接应该在 slug 页面内并指向默认链接。 So far I tried getting the
basePath
from nextRouter
to use as a href
.到目前为止,我尝试从
nextRouter
获取basePath
以用作href
。 But that returns the slug url ( [slug].default.com/doc
.但这会返回 slug url (
[slug].default.com/doc
。
Can I use the Next.js Link
component and point it to /doc/default
?我可以使用 Next.js
Link
组件并将其指向/doc/default
吗? It works locally, but I don't want to push it to dev/prod
just to test if it works.它在本地工作,但我不想将它推送到
dev/prod
只是为了测试它是否有效。
Have you tried redirects
?您是否尝试过
redirects
?
Next.js has an experimental feature to redirect pages ahead of time inside its next.config.js
file. Next.js 具有实验性功能,可在其
next.config.js
文件中提前重定向页面。
Let's say we'd like to redirect our root path site.com/
to site.com/en-us/
.假设我们想将根路径
site.com/
重定向到site.com/en-us/
。 We could use useRouter
whenever we needed to redirect the user or we could add directly into next.config.js
:我们可以在需要重定向用户时使用
useRouter
,也可以直接添加到next.config.js
:
const nextConfig = {
experimental: true,
async redirects() {
return [
{
source: '/',
permanent: false,
destination: '/en-us',
},
]
}
}
Now anytime the user navigates to site.com/
he'll be redirected to site.com/en-us
which will render the page inside pages/en-us/index.(jsx|tsx|mdx)
.现在,只要用户导航到
site.com/
,他就会被重定向到site.com/en-us
,这将在pages/en-us/index.(jsx|tsx|mdx)
中呈现页面。 It is also possible to use RegEx for matching as described on this issue .也可以使用 RegEx 进行匹配,如本期所述。
For your use case, would be the the other way around.对于您的用例,情况正好相反。 We'd probably have to use absolute paths, since we're redirecting to another domain.
我们可能不得不使用绝对路径,因为我们正在重定向到另一个域。 In this case, a subdomain of our root
default.com
domain.在这种情况下,我们的根
default.com
域的子域。
// const getSlugs = fs.readdir(path.resolve(...), () => {})
const slugs = ['slug1', 'slug2']
const slugRedirects = slugs.map(slug => ({
source: `/doc/${slug}`,
destination: `https://${slug}.default.com/doc`,
permanent: false
}))
const nextConfig = {
async redirects () {
return [...slugRedirects]
}
}
module.exports = nextConfig
Now, whenever the user navigates to www.default.com/doc/slug1
, either by typing the URL or clicking on a button that points to the pages/doc/[slug]
pages, he will be redirected to slug1.default.com/doc
.现在,每当用户导航到
www.default.com/doc/slug1
时,通过键入 URL 或单击指向pages/doc/[slug]
页面的按钮,他将被重定向到slug1.default.com/doc
。
Let me know if that helps.让我知道这是否有帮助。 Cheers!
干杯!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.