Next js重写不适用于i18n路由



我在下一个js中重写i18n时遇到了一些问题。

在我的next.config.js内部,我有以下

/** @type {import('next').NextConfig} */
module.exports = {
reactStrictMode: true,
i18n: {
locales: ["en", "nl"],
defaultLocale: "en",
},
async rewrites() {
return [
{
source: "/nl/about",
destination: "/nl/over-ons",
locale: false,
},
];
},
};

现在我有以下两页

about.tsx
index.tsx

在我的index.tsx文件中,我有一个<Link>标签:

<Link href="/about">About us</Link>

现在,当我将浏览器语言从EN更改为NL时,我的路由以nl为前缀,因此工作正常,问题是NL中的about页面从未将其url更改为over-ons,它始终保持为about

不确定在这样一个简单的例子中出了什么问题?

也许这对你来说已经很晚了,但这可能仍然会帮助其他人。

你想要实现的也许是redirect,而不是rewrite。了解有关重写和重定向的更多信息。

只需像下面的例子一样将rewrites更改为redirect就可以解决您的问题:

/** @type {import('next').NextConfig} */
module.exports = {
reactStrictMode: true,
i18n: {
locales: ["en", "nl"],
defaultLocale: "en",
},
async redirects() {
return [
{
source: "/nl/about",
destination: "/nl/over-ons",
locale: false,
},
];
},
};

最新更新