2023-02-16 22:39:57 +00:00
|
|
|
import type { GetServerSideProps, GetServerSidePropsContext, GetServerSidePropsResult } from "next";
|
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;
|
|
|
|
|
|
|
|
if ("redirect" in ssrResponse) {
|
|
|
|
// Use a dummy URL https://base as the fallback base URL so that URL parsing works for relative URLs as well.
|
|
|
|
const destinationUrlObj = new URL(ssrResponse.redirect.destination, "https://base");
|
|
|
|
|
|
|
|
// Make sure that redirect happens to /embed page and pass on embed query param as is for preserving Cal JS API namespace
|
|
|
|
const newDestinationUrl =
|
|
|
|
destinationUrlObj.pathname +
|
|
|
|
"/embed?" +
|
|
|
|
destinationUrlObj.searchParams.toString() +
|
|
|
|
"&embed=" +
|
|
|
|
embed;
|
|
|
|
|
|
|
|
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,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|