简体   繁体   English

我可以使用 Next.js 链接从 slug 页面导航到默认页面吗?

[英]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.

相关问题 如何在 next.js 中的 patName 之前使用 slug? - how can I use slug before patName in next.js? 在 React/Next.js 中,如何简单地将数据从一个页面的组件传递到另一个页面上的组件? - How can I simply pass data from a component one page to a component on another page in React/Next.js? 即使我在页面上,链接在 next.js 中也无效 - Link not active in next.js even when I am on the page Next.js 链接未从具有不同参数的同一页面路由 - Next.js link not routing from the same page with different parameters 如何通过来自 Next.js 的独特 slug 在 MongoDB 中找到一个文档? - How to find one document in MongoDB by unique slug from Next.js? Next.js 来自“pages/[...slug].js”的路由不起作用 - Next.js routing from "pages/[...slug].js" not working 有没有办法从嵌套的 [slug] 路径重定向 404 以使用 Next.js 中的父文件? - Is there a way to redirect 404s from a nested [slug] path to use the parent file in Next.js? 无法从当前 slug 内容中获取项目 - Sanity/Next.js - Can't fetch item from current slug content- Sanity/Next.js 如何在 Next.js 中设置默认 Head 标签的样式 - How can I style the default Head tag in Next.js 如何根据 GraphQL API 的 slug 使用 Next.js 进行动态路由? - How do I do dynamic routing with Next.js based on slug from GraphQL API?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM