英文:
NextJS 11 rendering dynamic content from getServerSideProps depending on domain?
问题
我有一个多租户网站,其中有多个具有相同顶级域的域名。
例如:
stagingus.sitename.net
staginguk.sitename.net
dev.sitename.co.uk
dev.sitename.com
dev.sitename.co.uk
sitename.com
sitename.co.uk
site-name.com
site-name.co.uk
我最初打算使用next.config.js
中的i18n
设置来进行国际化,但由于不能在相同的区域设置中拥有多个域名,所以我受挫了。例如:
i18n: {
locales: ['en-GB', 'en-US'],
defaultLocale: 'en-GB',
localeDetection: false,
domains: [
{
domain: 'sitename.co.uk',
defaultLocale: 'en-GB',
},
{
domain: 'sitename.com',
defaultLocale: 'en-US',
},
{
domain: 'dev.sitename.com',
defaultLocale: 'en-US',
},
],
},
会抛出以下错误:
Both dev.sitename.com and sitename.com configured the defaultLocale en-US but only one can. Change one item's default locale to continue
Error: Invalid i18n.domains values:
{"domain":"sitename.com","defaultLocale":"en-US"}
{"domain":"dev.sitename.com","defaultLocale":"en-US"}
domains value must follow format { domain: 'example.fr', defaultLocale: 'fr', locales: ['fr'] }.
我也不能依赖顶级域名,因为stagingus.sitename.net
和staginguk.sitename.net
都以.net
结尾,但提供不同的内容。
所以我不确定是否只需在getServerSideProps
函数中检查当前主机是否为英国或美国域名,类似于以下方式:
const isUS = ctx.req.headers.host.match(someUsDomainRegex));
除非还有其他我不知道的方法?
任何帮助将不胜感激。
英文:
I have a multitenancy website that has multiple domains with the same tld.
For example
stagingus.sitename.net
staginguk.sitename.net
dev.sitename.co.uk
dev.sitename.com
dev.sitename.co.uk
sitename.com
sitename.co.uk
site-name.com
site-name.co.uk
I initially was going to go down the route of Internationalization, using i18n
setting in next.config.js
but was thwarted by the fact that you cannot have multiple domains with the same locale, for example
i18n: {
locales: ['en-GB', 'en-US'],
defaultLocale: 'en-GB',
localeDetection: false,
domains: [
{
domain: 'sitename.co.uk',
defaultLocale: 'en-GB',
},
{
domain: 'sitename.com',
defaultLocale: 'en-US',
},
{
domain: 'dev.sitename.com',
defaultLocale: 'en-US',
},
],
},
throws the following error
Both dev.sitename.com and sitename.com configured the defaultLocale en-US but only one can. Change one item's default locale to continue
Error: Invalid i18n.domains values:
{"domain":"sitename.com","defaultLocale":"en-US"}
{"domain":"dev.sitename.com","defaultLocale":"en-US"}
domains value must follow format { domain: 'example.fr', defaultLocale: 'fr', locales: ['fr'] }.
I cannot rely on the TLD either, as stagingus.sitename.net
and staginguk.sitename.net
both end in .net
but would offer different content.
So I am not sure if it's just going to be a case of checking if the current host is a UK or US domain with something like this is in a getServerSideProps
function?
const isUS = ctx.req.headers.host.match(someUsDomainRegex));
Unless there is some other method I am unaware of?
Any help would be appreciated
答案1
得分: 1
是的,这是根据当前域名的子域名有条件地执行代码的方法,你可以像这样编写你的代码。
export async function getServerSideProps(ctx: GetServerSidePropsContext) {
if (ctx.req.headers.host?.startsWith?.("dev")) {
return; // ... 开发环境的数据
}
// ...
}
当然,如果你知道子域名的结构,你可以实现一个辅助函数来简化这个过程,使代码可重用,这样你可以在多个页面中实现它:
export async function getHostType(host: string) {
const separated = host.split(".");
if (host?.startsWith?.("dev")) {
return { type: "dev", country: null };
}
if (host?.startsWith?.("staging")) {
const country = separated[0].replace("staging", "");
return { type: "staging", country };
}
const country = separated[separated.length - 1];
return { type: "production", country: country !== "com" ? country : null };
}
英文:
Yes, this is the way to conditionally execute code based on the subdomain of the current domain, you could write your code something like this.
export async function getServerSideProps(ctx: GetServerSidePropsContext) {
if (ctx.req.headers.host?.startsWith?.("dev")) {
return; // ... data for development
}
// ...
}
Of course if you know the sub-domain structure you can always implement a helper function to make this process less tedious and the code reusable so you can implement it in multiple pages:
export async function getHostType(host: string) {
const separated = host.split(".");
if (host?.startsWith?.("dev")) {
return { type: "dev", country: null };
}
if (host?.startsWith?.("staging")) {
const country = separated[0].replace("staging", "");
return { type: "staging", country };
}
const country = separated[separated.length - 1];
return { type: "production", country: country !== "com" ? country : null };
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论