Files
calendar/apps/web/app/WithEmbedSSR.tsx
Benny JooandGitHub 75c79f76c8 perf: Remove ssrInit completely (#20439)
* remove ssrInit completely

* remove instances of dehydratedState

* fix

* refactor

* fix type checks

* fix linting

* Refactor

* Refactor

* remove log
2025-04-01 19:14:46 -04:00

50 lines
1.7 KiB
TypeScript

import type { GetServerSideProps, GetServerSidePropsContext } from "next";
import { notFound, redirect } from "next/navigation";
import { WebAppURL } from "@calcom/lib/WebAppURL";
export type EmbedProps = {
isEmbed?: boolean;
};
const withEmbedSsrAppDir =
<T extends Record<string, any>>(getServerSideProps: GetServerSideProps<T>) =>
async (context: GetServerSidePropsContext): Promise<T> => {
const { embed, layout } = context.query;
const ssrResponse = await getServerSideProps(context);
if ("redirect" in ssrResponse) {
const destinationUrl = ssrResponse.redirect.destination;
let urlPrefix = "";
// Get the URL parsed from URL so that we can reliably read pathname and searchParams from it.
const destinationUrlObj = new WebAppURL(ssrResponse.redirect.destination);
// 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}`;
redirect(newDestinationUrl);
}
if ("notFound" in ssrResponse) {
notFound();
}
return {
...ssrResponse.props,
isEmbed: true,
};
};
export default withEmbedSsrAppDir;