防止Get -Statatic Paths运行特定语言环境

发布于 2025-01-27 15:18:48 字数 1671 浏览 2 评论 0 原文

您是否有可能防止Next.js Getstatic Paths 用于为特定语言环境生成静态页面?

在我的下一个配置中:

  i18n: {
    locales: ['default', 'en', 'hu', 'de', 'cz', 'eu', 'sl'],
    defaultLocale: 'default',
    localeDetection: true,
  },

这仅仅是因为我们一直都需要语言环境,并且默认情况下,Next.js不支持它,因此我们必须使用中间件技巧,然后是官方的Next.js文档: https://github.com/github.com/vercel/vercel/next.js/discussions/18419

现在我想生成站点,我不想生成诸如

  • /默认/products/id1
  • /default/default/products/id2

之类的页面,我该如何防止下一步。因为这行不通: 对于我的常见问题页面,其中/help/[main category]/[sub category]/[help]

使用 slice (我会跳过默认的语言环境):

 locales.slice(1).map((locale) => {
        pages.map((item) => {
          item.data.mainTabList?.[locale]?.map((main, mainidx) => {
            main.subTabList?.map((sub, subidx) => {
              questions.map((help) => {
                help.data.helplist?.[locale]?.map((title) => {
                  const urllink = {
                    maincategory: urlConverter(main.tab),
                    subcategory: urlConverter(sub.tab),
                    help: title.url
                  }
    
                  routes.push(urllink)
                })
              })
            })
          })
        })
      })

  const paths = routes.map((doc) => ({
    params: {
      maincategory: `${doc.maincategory}`,
      subcategory: `${doc.subcategory}`,
      help: doc.help?.toLowerCase(),
    },
  }))

任何人都可以帮助我解决这个问题吗?对于我的网站前缀,我们不会在任何地方使用它。

Is it possible that you prevent Next.js getStaticPaths for generating static pages for a specific locale?

In my next config:

  i18n: {
    locales: ['default', 'en', 'hu', 'de', 'cz', 'eu', 'sl'],
    defaultLocale: 'default',
    localeDetection: true,
  },

That's just because we need the locale all the time, and by the default, Next.js does not support it, so we have to use the middleware trick followed by the official Next.js docs: https://github.com/vercel/next.js/discussions/18419

But now when I want to generate sites, I don't want to generate pages like

  • /default/products/id1
  • /default/products/id2

How can I prevent Next.js doing this? Because this does not work:
For my faq page where /help/[maincategory]/[subcategory]/[help].

Using slice (I will skip the default locale):

 locales.slice(1).map((locale) => {
        pages.map((item) => {
          item.data.mainTabList?.[locale]?.map((main, mainidx) => {
            main.subTabList?.map((sub, subidx) => {
              questions.map((help) => {
                help.data.helplist?.[locale]?.map((title) => {
                  const urllink = {
                    maincategory: urlConverter(main.tab),
                    subcategory: urlConverter(sub.tab),
                    help: title.url
                  }
    
                  routes.push(urllink)
                })
              })
            })
          })
        })
      })

  const paths = routes.map((doc) => ({
    params: {
      maincategory: `${doc.maincategory}`,
      subcategory: `${doc.subcategory}`,
      help: doc.help?.toLowerCase(),
    },
  }))

Can anyone help me how to solve this, that /default pages won't get generated, because that's just a hacky way for my locale prefix, we won't use it anywhere.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小清晰的声音 2025-02-03 15:18:48

可以控制哪些Locales GetStatic Paths 将通过返回 Paths array中的所需erentes来生成路径。

来自

使用 getStaticProps 使用 getstaticpaths 。以及 params 对象返回 paths
您还可以返回 locale 字段指定您要哪个语言环境
渲染。



在您的情况下,您的 getstaticpaths 功能应大致看起来像以下内容。

export const getStaticPaths = ({ locales }) => {
    // Your own logic
    
    // Filter out the `default` locale, and map through the remaining locales
    locales.filter((locale) => locale !== 'default').map((locale) => {
        pages.map((item) => {
            item.data.mainTabList?.[locale]?.map((main, mainidx) => {
                main.subTabList?.map((sub, subidx) => {
                    questions.map((help) => {
                        help.data.helplist?.[locale]?.map((title) => {
                            const urlLink = {
                                maincategory: urlConverter(main.tab),
                                subcategory: urlConverter(sub.tab),
                                help: title.url,
                                locale // Also push current `locale` value to be used in `paths` array
                            }
                            routes.push(urlLink)
                        })
                    })
                })
            })
        })
    })

    const paths = routes.map((doc) => ({
        params: {
            maincategory: `${doc.maincategory}`,
            subcategory: `${doc.subcategory}`,
            help: doc.help?.toLowerCase(),
        },
        locale: doc.locale // Pass `locale` value here
    }))

    return {
        paths,
        fallback: false
    }
}

It's possible to control which locales getStaticPaths will generate paths for by returning the desired locales in the paths array.

From the i18n Dynamic Routing documentation:

For pages using getStaticProps with Dynamic Routes, all locale
variants of the page desired to be prerendered need to be returned
from getStaticPaths. Along with the params object returned for paths,
you can also return a locale field specifying which locale you want to
render.


In your case, your getStaticPaths function should roughly look like the following.

export const getStaticPaths = ({ locales }) => {
    // Your own logic
    
    // Filter out the `default` locale, and map through the remaining locales
    locales.filter((locale) => locale !== 'default').map((locale) => {
        pages.map((item) => {
            item.data.mainTabList?.[locale]?.map((main, mainidx) => {
                main.subTabList?.map((sub, subidx) => {
                    questions.map((help) => {
                        help.data.helplist?.[locale]?.map((title) => {
                            const urlLink = {
                                maincategory: urlConverter(main.tab),
                                subcategory: urlConverter(sub.tab),
                                help: title.url,
                                locale // Also push current `locale` value to be used in `paths` array
                            }
                            routes.push(urlLink)
                        })
                    })
                })
            })
        })
    })

    const paths = routes.map((doc) => ({
        params: {
            maincategory: `${doc.maincategory}`,
            subcategory: `${doc.subcategory}`,
            help: doc.help?.toLowerCase(),
        },
        locale: doc.locale // Pass `locale` value here
    }))

    return {
        paths,
        fallback: false
    }
}
满栀 2025-02-03 15:18:48

来自官方

对于非动态GetStaticProps页面,每个版本都是为每个版本生成的
如上所述。每个语言环境都调用getstaticprops
被渲染。

如果您想从中退出某个地方
被预渲染,您可以返回NOT FOUND:TRUE
从GetStaticProps中
并且该页面的这个变体将不会生成。

export async function getStaticProps(ctx: GetStaticPropsContext) {
  if (ctx.locale === 'none') { // or check "default"
    return {
      notFound: true,
    }
  }

  return {
    props: {},
    revalidate: 3600,
  }
}

From the official documentation about getStaticProps:

For non-dynamic getStaticProps pages, a version is generated for each
locale like above. getStaticProps is called with each locale that is
being rendered.

If you would like to opt-out of a certain locale from
being pre-rendered, you can return notFound: true
from getStaticProps
and this variant of the page will not be generated.

export async function getStaticProps(ctx: GetStaticPropsContext) {
  if (ctx.locale === 'none') { // or check "default"
    return {
      notFound: true,
    }
  }

  return {
    props: {},
    revalidate: 3600,
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文