update isCalcom for canonicals to handle https://cal.com (#6802)

* update isCalcom for canonicals to handle https://cal.com

* avoid env var

* prettier

* handle window undefined

* handle window undefined during pre-render

* add some logs

* add some logs

* Revert "add some logs"

This reverts commit 9d27de0290.
This commit is contained in:
Richard Poelderl 2023-01-31 16:07:50 -03:00 committed by GitHub
parent 4679c80ea0
commit dcbafced8b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -11,7 +11,6 @@ import {
} from "@calcom/lib/OgImages";
import { getBrowserInfo } from "@calcom/lib/browser/browser.utils";
import { APP_NAME } from "@calcom/lib/constants";
import isCalcom from "@calcom/lib/isCalcom";
import { seoConfig, getSeoImage } from "@calcom/lib/next-seo.config";
import { truncateOnWord } from "@calcom/lib/text";
@ -74,10 +73,18 @@ const buildSeoMeta = (pageProps: {
};
export const HeadSeo = (props: HeadSeoProps): JSX.Element => {
// build the canonical url to ensure it's always cal.com (not app.cal.com)
const router = useRouter();
const calcomUrl = (`https://cal.com` + (router.asPath === "/" ? "" : router.asPath)).split("?")[0]; // cut off search params
const defaultUrl = isCalcom ? calcomUrl : getBrowserInfo()?.url;
// The below code sets the defaultUrl for our canonical tags
// Get the current URL from the window object
const url = getBrowserInfo()?.url;
// Check if the URL is from cal.com
const isCalcom = url && new URL(url).hostname.endsWith("cal.com");
// Get the router's path
const path = useRouter().asPath;
// Build the canonical URL using the router's path, without query parameters. Note: on homepage it omits the trailing slash
const calcomCanonical = `https://cal.com${path === "/" ? "" : path}`.split("?")[0];
// Set the default URL to either the current URL (if self-hosted) or https://cal.com canonical URL
const defaultUrl = isCalcom ? calcomCanonical : url;
const { title, description, siteName, canonical = defaultUrl, nextSeoProps = {}, app, meeting } = props;