我正在从 Next.js /pages 迁移到 /app,我有 2 种语言 en 和 es,默认语言是 en,url 结构是:
英语
西班牙语
但是当在应用程序路由器上集成 i18n 时,我创建了一个 [lang] 文件夹,并遵循以下任何指南后:
https://locize.com/blog/next-13-app-dir-i18n/
https://nextjs.org/docs/app/building-your-application/routing/internationalization
我在 /app/[lang] 上的结构是:
英语
西班牙语
如果我访问 example.com/,我将被重定向到 example.com/en,如果我不解决这个问题,我们当前的 SEO 将会出现问题,因为大多数 URL 都会发生变化。
检查 NextResponse 的重写 api(https://nextjs.org/docs/pages/api-reference/next-config-js/rewrites)。
所以在nextjs的文档(https://nextjs.org/docs/app/building-your-application/routing/internationalization)中,中间件的返回值是一个 'Response.redirect(request.nextUrl) ' 将客户端重定向到包含区域设置信息的 url。
将其替换为“NextResponse.rewrite(request.nextUrl)”,新的 url 将仅在服务器内部接受,不会影响客户端。在某种程度上有点像“代理”。
* NextResponse 可以从 next/server 导入。