NextJS 10 存在区分大小写的 URL 问题

Pet*_*ner 6 reactjs next.js

在 NextJS 的最新版本中,路由器已变得区分大小写。我一直在慢慢地用 NextJS 重写我们的会议网站,我注意到我们重视的 SEO 的大部分内容都由 google 和其他人存储,这会导致 404。

例如,可以通过谷歌搜索“Douglas Crockford Silicon Valley Code Camp”来发现该 URL。

https://www.siliconvalley-codecamp.com/Session/2018/qa-with-douglas-crockford

NextJS 有没有办法在生产中运行时以某种方式小写所有传入的 URL,即使是某种重定向?

我遵循他们在这里的模式: https: //nextjs.org/docs/routing/dynamic-routes并且我的应用程序正在使用 GetStaticPaths,因为我计划使用 ISR(增量静态再生),因此它需要与之配合使用还。

另外,由于该网站始终不区分大小写,因此 URL 以各种方式存储,因此我不能只按照 Google 存储的方式进行存储。

Mih*_*aru 5

我假设你有类似的东西:/articles/:slug。我将使用路由器获取 slug,然后在安装时创建一个钩子,如果 slug 有任何大写字母,则重定向到“/articles/:lowercaseSlug”。

import { useRouter } from 'next/router'
const router = useRouter;
const { slug } = router.query;

useEffect(() => {
    if (slug.toLowerCase() !== slug) // Check if it has any capital letters
        router.push(`/articles/${slug.toLowerCase()}`) // Redirect to the working path
}, [])
Run Code Online (Sandbox Code Playgroud)