【发布时间】:2022-09-28 02:47:05
【问题描述】:
我在 next-js 中间件中有一个错误
在中间件函数中返回一个 NextRequest 婴儿车 在 nextjs 文档中说:
The NextRequest object is an extension of the native Request interface, with the
following added methods and properties:
- cookies - A Map with cookies from the Request. See Using cookies in Middleware
- nextUrl: Includes an extended, parsed, URL object that gives you access to Next.js
- specific properties such as pathname, basePath, trailingSlash and i18n. Includes the
- following properties:
- basePath (string)
- buildId (string || undefined)
- defaultLocale (string || undefined)
- domainLocale
- defaultLocale: (string)
- domain: (string)
- http: (boolean || undefined)
- locales: (string[] || undefined)
- locale (string || undefined)
这意味着我可以从 NextRequest.nextUrl.locale 访问当前语言环境。 很好,这在 localhost 中工作,我已经获得了一个语言环境。
但是在netlify中部署项目并在控制台中打印NextRequest.nextUrl.locale之后
console.log({locale: NextRequest.nextUrl.locale});
被退回给我
{ locale: \"\" }
含义 NextRequest.nextUrl.locale = \"\" & 空字符串
为什么这个错误?
那是我的代码
const middleware = async (req) => {
if (
req.nextUrl.pathname.startsWith(\"/_next\") ||
req.nextUrl.pathname.includes(\"/api/\") ||
PUBLIC_FILE.test(req.nextUrl.pathname)
) {
return;
}
console.log({locale: req.nextUrl.locale});
return;
};
export { middleware };
标签: javascript node.js next.js middleware i18next