2023-02-16 22:39:57 +00:00
|
|
|
import type { GetServerSideProps, GetServerSidePropsContext, GetServerSidePropsResult } from "next";
|
2022-10-19 21:25:03 +00:00
|
|
|
|
2023-10-26 08:12:05 +00:00
|
|
|
import { WEBAPP_URL } from "@calcom/lib/constants";
|
|
|
|
|
2022-10-19 21:25:03 +00:00
|
|
|
export type EmbedProps = {
|
|
|
|
isEmbed?: boolean;
|
|
|
|
};
|
|
|
|
|
|
|
|
export default function withEmbedSsr(getServerSideProps: GetServerSideProps) {
|
|
|
|
return async (context: GetServerSidePropsContext): Promise<GetServerSidePropsResult<EmbedProps>> => {
|
|
|
|
const ssrResponse = await getServerSideProps(context);
|
2022-12-07 20:08:34 +00:00
|
|
|
const embed = context.query.embed;
|
2023-06-14 09:22:44 +00:00
|
|
|
const layout = context.query.layout;
|
2022-12-07 20:08:34 +00:00
|
|
|
|
|
|
|
if ("redirect" in ssrResponse) {
|
2023-10-26 08:12:05 +00:00
|
|
|
const destinationUrl = ssrResponse.redirect.destination;
|
|
|
|
let urlPrefix = "";
|
2022-12-07 20:08:34 +00:00
|
|
|
|
2023-10-26 08:12:05 +00:00
|
|
|
// Get the URL parsed from URL so that we can reliably read pathname and searchParams from it.
|
|
|
|
const destinationUrlObj = new URL(ssrResponse.redirect.destination, WEBAPP_URL);
|
2022-12-07 20:08:34 +00:00
|
|
|
|
2023-10-26 08:12:05 +00:00
|
|
|
// If it's a complete URL, use the origin as the prefix to ensure we redirect to the same domain.
|
|
|
|
if (destinationUrl.search(/^(http:|https:).*/) !== -1) {
|
|
|
|
urlPrefix = destinationUrlObj.origin;
|
|
|
|
} else {
|
|
|
|
// Don't use any prefix for relative URLs to ensure we stay on the same domain
|
|
|
|
urlPrefix = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
const destinationQueryStr = destinationUrlObj.searchParams.toString();
|
|
|
|
// Make sure that redirect happens to /embed page and pass on embed query param as is for preserving Cal JS API namespace
|
|
|
|
const newDestinationUrl = `${urlPrefix}${destinationUrlObj.pathname}/embed?${
|
|
|
|
destinationQueryStr ? `${destinationQueryStr}&` : ""
|
|
|
|
}layout=${layout}&embed=${embed}`;
|
2022-12-07 20:08:34 +00:00
|
|
|
return {
|
|
|
|
...ssrResponse,
|
|
|
|
redirect: {
|
|
|
|
...ssrResponse.redirect,
|
|
|
|
destination: newDestinationUrl,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-10-19 21:25:03 +00:00
|
|
|
if (!("props" in ssrResponse)) {
|
|
|
|
return ssrResponse;
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
...ssrResponse,
|
|
|
|
props: {
|
|
|
|
...ssrResponse.props,
|
|
|
|
isEmbed: true,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|