[英]Next Js dynamic subpath
I am currently working on a next.js app and so far so good, all has been going on fine but my client wants a feature that's quite difficult for me to implement.我目前正在开发 next.js 应用程序,到目前为止一切顺利,一切顺利,但我的客户想要一个对我来说很难实现的功能。
SO the site is at mysite.com and routes like mysite.com/cars, mysite.com/vehicles, etc所以该站点位于 mysite.com 和诸如 mysite.com/cars、mysite.com/vehicles 等路线
Now my client wants to a dynamic subpath based on the visitor's country eg mysite.com/us/cars or mysite.com/uk/cars or mysite.com/ng/cars现在我的客户想要一个基于访问者国家的动态子路径,例如 mysite.com/us/cars 或 mysite.com/uk/cars 或 mysite.com/ng/cars
I have tried the new i18n feature in next.js 10 but seems like that only works based on user's locale (language).我已经在 next.js 10 中尝试了新的 i18n 功能,但似乎只能根据用户的语言环境(语言)工作。
Is there a way I can programmatically do this or I should leave it to DevOps to deploy to subfolders?有没有办法以编程方式执行此操作,或者我应该将其留给 DevOps 部署到子文件夹?
You can accomplish this with dynamic routing.您可以通过动态路由来实现这一点。
https://nextjs.org/docs/routing/dynamic-routes https://nextjs.org/docs/routing/dynamic-routes
What you'd need to do is to adjust to have a [locale]
folder in your directory structure, so your pages become:您需要做的是调整在您的目录结构中有一个
[locale]
文件夹,这样您的页面就会变成:
pages/[locale]/cars
or pages/[locale]/vehicles
. pages/[locale]/cars
或pages/[locale]/vehicles
。
You can then access the active locale with然后,您可以访问活动语言环境
const router = useRouter();
const locale = router.query.locale;
to determine how to retrieve the content.以确定如何检索内容。 In the event that you need to statically generate said locales, you can do that as well by placing a
如果您需要静态生成所述语言环境,您也可以通过放置一个
getStaticPaths
function in your pages页面中的
getStaticPaths
函数
https://nextjs.org/docs/basic-features/data-fetching#getstaticpaths-static-generation https://nextjs.org/docs/basic-features/data-fetching#getstaticpaths-static-generation
Now then, if you're going to ask how to dynamically route based on their current country ie redirect once they land on the top level pages, well that is a whole different game.现在,如果您要问如何根据他们当前的国家/地区动态路由,即一旦他们登陆顶级页面就重定向,那是一个完全不同的游戏。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.