feat: Single domain setup when only one org in the system (#18383)
Co-authored-by: Omar López <[email protected]>
This commit is contained in:
1 parent
d7b723c914
commit
6f531d0be5
8 files changed
+208
-138
No files matched your search
@@ -0,0 +1,48 @@
|
||||
const isSingleOrgModeEnabled = !!process.env.NEXT_PUBLIC_SINGLE_ORG_SLUG;
|
||||
const orgSlugCaptureGroupName = "orgSlug";
|
||||
/**
|
||||
* Returns the leftmost subdomain from a given URL.
|
||||
* It needs the URL domain to have atleast two dots.
|
||||
* app.cal.com -> app
|
||||
* app.company.cal.com -> app
|
||||
* app.company.com -> app
|
||||
*/
|
||||
const getLeftMostSubdomain = (url) => {
|
||||
if (!url.startsWith("http:") && !url.startsWith("https:")) {
|
||||
// Make it a valid URL. Mabe we can simply return null and opt-out from orgs support till the use a URL scheme.
|
||||
url = `https://${url}`;
|
||||
}
|
||||
const _url = new URL(url);
|
||||
const regex = new RegExp(/^([a-z]+\:\/{2})?((?<subdomain>[\w-.]+)\.[\w-]+\.\w+)$/);
|
||||
//console.log(_url.hostname, _url.hostname.match(regex));
|
||||
return _url.hostname.match(regex)?.groups?.subdomain || null;
|
||||
};
|
||||
|
||||
const getRegExpNotMatchingLeftMostSubdomain = (url) => {
|
||||
const leftMostSubdomain = getLeftMostSubdomain(url);
|
||||
const subdomain = leftMostSubdomain ? `(?!${leftMostSubdomain})[^.]+` : "[^.]+";
|
||||
return subdomain;
|
||||
};
|
||||
|
||||
// For app.cal.com, it will match all domains that are not starting with "app". Technically we would want to match domains like acme.cal.com, dunder.cal.com and not app.cal.com
|
||||
const getRegExpThatMatchesAllOrgDomains = (exports.getRegExpThatMatchesAllOrgDomains = ({ webAppUrl }) => {
|
||||
if (isSingleOrgModeEnabled) {
|
||||
console.log("Single-Org-Mode enabled - Consider all domains to be org domains");
|
||||
// It works in combination with next.config.js where in this case we use orgSlug=NEXT_PUBLIC_SINGLE_ORG_SLUG
|
||||
return `.*`;
|
||||
}
|
||||
const subdomainRegExp = getRegExpNotMatchingLeftMostSubdomain(webAppUrl);
|
||||
return `^(?<${orgSlugCaptureGroupName}>${subdomainRegExp})\\.(?!vercel\.app).*`;
|
||||
});
|
||||
|
||||
const nextJsOrgRewriteConfig = {
|
||||
// :orgSlug is special value which would get matching group from the regex in orgHostPath
|
||||
orgSlug: process.env.NEXT_PUBLIC_SINGLE_ORG_SLUG || `:${orgSlugCaptureGroupName}`,
|
||||
orgHostPath: getRegExpThatMatchesAllOrgDomains({
|
||||
webAppUrl: process.env.NEXT_PUBLIC_WEBAPP_URL || `https://${process.env.VERCEL_URL}`,
|
||||
}),
|
||||
// We disable root path rewrite because we want to serve dashboard on root path
|
||||
disableRootPathRewrite: isSingleOrgModeEnabled,
|
||||
};
|
||||
|
||||
exports.nextJsOrgRewriteConfig = nextJsOrgRewriteConfig;
|
||||
Reference in new issue
Block a user