## What does this PR do? Fix 404 booking pages when org support is enabled locally ## Type of change <!-- Please delete bullets that are not relevant. --> - Bug fix (non-breaking change which fixes an issue) ## How should this be tested? <!-- Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration --> - Add localhost:3000 to ALLOWED_HOSTNAMES which is the default recommendation for enabling org support locally. <img width="1035" alt="Screenshot 2023-07-10 at 11 04 35 AM" src="https://github.com/calcom/cal.com/assets/1780212/7272437b-afae-4073-905b-eafc39f9fafc"> - Simply restart the server and visit http://localhost:3000/pro. It will give 404. After this change it would work ## Mandatory Tasks - [x] Make sure you have self-reviewed the code. A decent size PR without self-review might be rejected.
42 lines
1.4 KiB
TypeScript
42 lines
1.4 KiB
TypeScript
import { ALLOWED_HOSTNAMES, RESERVED_SUBDOMAINS, WEBAPP_URL } from "@calcom/lib/constants";
|
|
|
|
/**
|
|
* return the org slug
|
|
* @param hostname
|
|
*/
|
|
export function getOrgSlug(hostname: string) {
|
|
if (!hostname.includes(".")) {
|
|
// A no-dot domain can never be org domain. It automatically handles localhost
|
|
return null;
|
|
}
|
|
// Find which hostname is being currently used
|
|
const currentHostname = ALLOWED_HOSTNAMES.find((ahn) => {
|
|
const url = new URL(WEBAPP_URL);
|
|
const testHostname = `${url.hostname}${url.port ? `:${url.port}` : ""}`;
|
|
return testHostname.endsWith(`.${ahn}`);
|
|
});
|
|
if (currentHostname) {
|
|
// Define which is the current domain/subdomain
|
|
const slug = hostname.replace(`.${currentHostname}` ?? "", "");
|
|
return slug.indexOf(".") === -1 ? slug : null;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
export function orgDomainConfig(hostname: string) {
|
|
const currentOrgDomain = getOrgSlug(hostname);
|
|
return {
|
|
currentOrgDomain,
|
|
isValidOrgDomain: currentOrgDomain !== null && !RESERVED_SUBDOMAINS.includes(currentOrgDomain),
|
|
};
|
|
}
|
|
|
|
export function subdomainSuffix() {
|
|
const urlSplit = WEBAPP_URL.replace("https://", "")?.replace("http://", "").split(".");
|
|
return urlSplit.length === 3 ? urlSplit.slice(1).join(".") : urlSplit.join(".");
|
|
}
|
|
|
|
export function getOrgFullDomain(slug: string, options: { protocol: boolean } = { protocol: true }) {
|
|
return `${options.protocol ? `${new URL(WEBAPP_URL).protocol}//` : ""}${slug}.${subdomainSuffix()}/`;
|
|
}
|