perf: Server-Side Data Fetching in App Router: SettingsLayout / AdminLayout (#16537)

Co-authored-by: Joe Au-Yeung <[email protected]>
This commit is contained in:
Benny JooandJoe Au-Yeung authored and GitHub committed 2024-09-09 14:38:51 -07:00
1 parent 71443ef23d
commit 706d7dff42
25 files changed
+929 -874

No files matched your search

+3 -9
View File
@@ -1,11 +1,10 @@
import type { PageProps } from "app/_types";
import { _generateMetadata } from "app/_utils";
import { WithLayout } from "app/layoutHOC";
import { cookies, headers } from "next/headers";
import { buildLegacyCtx } from "@lib/buildLegacyCtx";
import PageWrapper from "@components/PageWrapperAppDir";
import { ssrInit } from "@server/lib/ssr";
import Logout from "~/auth/logout-view";
@@ -20,7 +19,6 @@ export const generateMetadata = async () => {
const Page = async ({ params, searchParams }: PageProps) => {
// cookie will be cleared in `/apps/web/middleware.ts`
const h = headers();
const nonce = h.get("x-nonce") ?? undefined;
const context = buildLegacyCtx(h, cookies(), params, searchParams);
const ssr = await ssrInit(context);
const props = {
@@ -28,11 +26,7 @@ const Page = async ({ params, searchParams }: PageProps) => {
query: context.query,
};
return (
<PageWrapper requiresLicense={false} getLayout={null} nonce={nonce} themeBasis={null}>
<Logout {...props} />
</PageWrapper>
);
return <Logout {...props} />;
};
export default Page;
export default WithLayout({ getLayout: null, ServerPage: Page })<"P">;
@@ -10,4 +10,4 @@ export const generateMetadata = async () =>
() => "admin_description"
);
export default WithLayout({ getLayout, Page: LegacyPage })<"P">;
export default WithLayout({ getServerLayout: getLayout, Page: LegacyPage })<"P">;
@@ -2,4 +2,4 @@ import { WithLayout } from "app/layoutHOC";
import { getLayout } from "@components/auth/layouts/AdminLayoutAppDir";
export default WithLayout({ getLayout })<"L">;
export default WithLayout({ getServerLayout: getLayout })<"L">;
@@ -2,4 +2,4 @@ import { WithLayout } from "app/layoutHOC";
import { getLayout } from "@calcom/features/settings/appDir/SettingsLayoutAppDir";
export default WithLayout({ getLayout: getLayout })<"L">;
export default WithLayout({ getServerLayout: getLayout })<"L">;
@@ -3,9 +3,7 @@ import LegacyPage, {
} from "@pages/settings/organizations/[id]/onboard-members";
import type { PageProps } from "app/_types";
import { _generateMetadata } from "app/_utils";
import { headers } from "next/headers";
import PageWrapper from "@components/PageWrapperAppDir";
import { WithLayout } from "app/layoutHOC";
export const generateMetadata = async () =>
await _generateMetadata(
@@ -14,18 +12,7 @@ export const generateMetadata = async () =>
);
const Page = ({ params }: PageProps) => {
const h = headers();
const nonce = h.get("x-nonce") ?? undefined;
return (
<PageWrapper
getLayout={(page: React.ReactElement) => buildWrappedOnboardTeamMembersPage(params.id, page)}
requiresLicense={false}
nonce={nonce}
themeBasis={null}>
<LegacyPage />
</PageWrapper>
);
return buildWrappedOnboardTeamMembersPage(params.id, <LegacyPage />);
};
export default Page;
export default WithLayout({ Page });
@@ -2,4 +2,4 @@ import { WithLayout } from "app/layoutHOC";
import { getLayout } from "@calcom/features/settings/appDir/SettingsLayoutAppDir";
export default WithLayout({ getLayout });
export default WithLayout({ getServerLayout: getLayout });
@@ -2,4 +2,4 @@ import { WithLayout } from "app/layoutHOC";
import { getLayout } from "@calcom/features/settings/appDir/SettingsLayoutAppDir";
export default WithLayout({ getLayout });
export default WithLayout({ getServerLayout: getLayout });
@@ -2,4 +2,4 @@ import { WithLayout } from "app/layoutHOC";
import { getLayout } from "@calcom/features/settings/appDir/SettingsLayoutAppDir";
export default WithLayout({ getLayout });
export default WithLayout({ getServerLayout: getLayout });
@@ -2,4 +2,4 @@ import { WithLayout } from "app/layoutHOC";
import { getLayout } from "@calcom/features/settings/appDir/SettingsLayoutAppDir";
export default WithLayout({ getLayout });
export default WithLayout({ getServerLayout: getLayout });
@@ -2,4 +2,4 @@ import { WithLayout } from "app/layoutHOC";
import { getLayout } from "@calcom/features/settings/appDir/SettingsLayoutAppDir";
export default WithLayout({ getLayout });
export default WithLayout({ getServerLayout: getLayout });
@@ -2,4 +2,4 @@ import { WithLayout } from "app/layoutHOC";
import { getLayout } from "@calcom/features/settings/appDir/SettingsLayoutAppDir";
export default WithLayout({ getLayout });
export default WithLayout({ getServerLayout: getLayout });
@@ -2,4 +2,4 @@ import { WithLayout } from "app/layoutHOC";
import { getLayout } from "@calcom/features/settings/appDir/SettingsLayoutAppDir";
export default WithLayout({ getLayout });
export default WithLayout({ getServerLayout: getLayout });
@@ -2,4 +2,4 @@ import { WithLayout } from "app/layoutHOC";
import { getLayout } from "@calcom/features/settings/appDir/SettingsLayoutAppDir";
export default WithLayout({ getLayout });
export default WithLayout({ getServerLayout: getLayout });
@@ -2,4 +2,4 @@ import { WithLayout } from "app/layoutHOC";
import { getLayout } from "@calcom/features/settings/appDir/SettingsLayoutAppDir";
export default WithLayout({ getLayout });
export default WithLayout({ getServerLayout: getLayout });
@@ -2,4 +2,4 @@ import { WithLayout } from "app/layoutHOC";
import { getLayout } from "@calcom/features/settings/appDir/SettingsLayoutAppDir";
export default WithLayout({ getLayout })<"L">;
export default WithLayout({ getServerLayout: getLayout })<"L">;
+16 -3
View File
@@ -7,8 +7,10 @@ import { buildLegacyCtx } from "@lib/buildLegacyCtx";
import PageWrapper from "@components/PageWrapperAppDir";
type WithLayoutParams<T extends Record<string, any>> = {
getLayout: ((page: React.ReactElement) => React.ReactNode) | null;
getLayout?: ((page: React.ReactElement) => React.ReactNode) | null;
getServerLayout?: (page: React.ReactElement) => Promise<React.ReactNode | null>;
Page?: (props: T) => React.ReactElement | null;
ServerPage?: (props: T) => Promise<React.ReactElement> | null;
getData?: (arg: GetServerSidePropsContext) => Promise<T | undefined>;
isBookingPage?: boolean;
requiresLicense?: boolean;
@@ -16,7 +18,9 @@ type WithLayoutParams<T extends Record<string, any>> = {
export function WithLayout<T extends Record<string, any>>({
getLayout,
getServerLayout,
getData,
ServerPage,
Page,
isBookingPage,
requiresLicense,
@@ -30,7 +34,16 @@ export function WithLayout<T extends Record<string, any>>({
props = (await getData(buildLegacyCtx(h, cookies(), p.params, p.searchParams))) ?? ({} as T);
}
const children = "children" in p ? p.children : null;
// `p.children` exists only for layout.tsx files
const childrenFromLayoutFile = "children" in p ? p.children : null;
const page = ServerPage ? (
await ServerPage({ ...props, ...p })
) : Page ? (
<Page {...props} />
) : (
childrenFromLayoutFile
);
const pageWithServerLayout = page ? (getServerLayout ? await getServerLayout(page) : page) : null;
return (
<PageWrapper
@@ -40,7 +53,7 @@ export function WithLayout<T extends Record<string, any>>({
themeBasis={null}
isBookingPage={isBookingPage || !!(Page && "isBookingPage" in Page && Page.isBookingPage)}
{...props}>
{Page ? <Page {...props} /> : children}
{pageWithServerLayout}
</PageWrapper>
);
};