-
Notifications
You must be signed in to change notification settings - Fork 8.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: refactor handling logic for embeds in app router (#18362)
* refactor handling logic for embeds in app router * fix type checks * add test for withEmbedSsrAppDir * fix
- Loading branch information
Showing
17 changed files
with
300 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,247 @@ | ||
import type { Request, Response } from "express"; | ||
import type { NextApiRequest, NextApiResponse, Redirect } from "next"; | ||
import { redirect, notFound } from "next/navigation"; | ||
import { createMocks } from "node-mocks-http"; | ||
import { describe, expect, it, vi } from "vitest"; | ||
|
||
import withEmbedSsrAppDir from "./WithEmbedSSR"; | ||
|
||
export type CustomNextApiRequest = NextApiRequest & Request; | ||
export type CustomNextApiResponse = NextApiResponse & Response; | ||
|
||
export function createMockNextJsRequest(...args: Parameters<typeof createMocks>) { | ||
return createMocks<CustomNextApiRequest, CustomNextApiResponse>(...args); | ||
} | ||
|
||
vi.mock("next/navigation", () => ({ | ||
redirect: vi.fn(), | ||
notFound: vi.fn(), | ||
})); | ||
|
||
function getServerSidePropsFnGenerator( | ||
config: | ||
| { redirectUrl: string } | ||
| { props: Record<string, unknown> } | ||
| { | ||
notFound: true; | ||
} | ||
) { | ||
if ("redirectUrl" in config) | ||
return async () => { | ||
return { | ||
redirect: { | ||
permanent: false, | ||
destination: config.redirectUrl, | ||
} satisfies Redirect, | ||
}; | ||
}; | ||
|
||
if ("props" in config) | ||
return async () => { | ||
return { | ||
props: config.props, | ||
}; | ||
}; | ||
|
||
if ("notFound" in config) | ||
return async () => { | ||
return { | ||
notFound: true as const, | ||
}; | ||
}; | ||
|
||
throw new Error("Invalid config"); | ||
} | ||
|
||
interface ServerSidePropsContext { | ||
embedRelatedParams?: Record<string, string>; | ||
} | ||
|
||
function getServerSidePropsContextArg({ embedRelatedParams = {} }: ServerSidePropsContext) { | ||
const { req, res } = createMockNextJsRequest(); | ||
return { | ||
req, | ||
res, | ||
query: { | ||
...embedRelatedParams, | ||
}, | ||
resolvedUrl: "/MOCKED_RESOLVED_URL", | ||
}; | ||
} | ||
|
||
describe("withEmbedSsrAppDir", () => { | ||
describe("when gSSP returns redirect", () => { | ||
describe("when redirect destination is relative", () => { | ||
it("should redirect with layout and embed params from the current query", async () => { | ||
const withEmbedGetSsr = withEmbedSsrAppDir( | ||
getServerSidePropsFnGenerator({ | ||
redirectUrl: "/reschedule", | ||
}) | ||
); | ||
|
||
await withEmbedGetSsr( | ||
getServerSidePropsContextArg({ | ||
embedRelatedParams: { | ||
layout: "week_view", | ||
embed: "namespace1", | ||
}, | ||
}) | ||
).catch(() => {}); | ||
|
||
expect(redirect).toHaveBeenCalledWith("/reschedule/embed?layout=week_view&embed=namespace1"); | ||
}); | ||
|
||
it("should preserve existing query params in redirect URL", async () => { | ||
const withEmbedGetSsr = withEmbedSsrAppDir( | ||
getServerSidePropsFnGenerator({ | ||
redirectUrl: "/reschedule?redirectParam=1", | ||
}) | ||
); | ||
|
||
await withEmbedGetSsr( | ||
getServerSidePropsContextArg({ | ||
embedRelatedParams: { | ||
layout: "week_view", | ||
embed: "namespace1", | ||
}, | ||
}) | ||
).catch(() => {}); | ||
|
||
expect(redirect).toHaveBeenCalledWith( | ||
"/reschedule/embed?redirectParam=1&layout=week_view&embed=namespace1" | ||
); | ||
}); | ||
|
||
it("should handle empty embed namespace", async () => { | ||
const withEmbedGetSsr = withEmbedSsrAppDir( | ||
getServerSidePropsFnGenerator({ | ||
redirectUrl: "/reschedule?redirectParam=1", | ||
}) | ||
); | ||
|
||
await withEmbedGetSsr( | ||
getServerSidePropsContextArg({ | ||
embedRelatedParams: { | ||
layout: "week_view", | ||
embed: "", | ||
}, | ||
}) | ||
).catch(() => {}); | ||
|
||
expect(redirect).toHaveBeenCalledWith("/reschedule/embed?redirectParam=1&layout=week_view&embed="); | ||
}); | ||
}); | ||
|
||
describe("when redirect destination is absolute", () => { | ||
it("should handle HTTPS URLs", async () => { | ||
const withEmbedGetSsr = withEmbedSsrAppDir( | ||
getServerSidePropsFnGenerator({ | ||
redirectUrl: "https://calcom.cal.local/owner", | ||
}) | ||
); | ||
|
||
await withEmbedGetSsr( | ||
getServerSidePropsContextArg({ | ||
embedRelatedParams: { | ||
layout: "week_view", | ||
embed: "namespace1", | ||
}, | ||
}) | ||
).catch(() => {}); | ||
|
||
expect(redirect).toHaveBeenCalledWith( | ||
"https://calcom.cal.local/owner/embed?layout=week_view&embed=namespace1" | ||
); | ||
}); | ||
|
||
it("should handle HTTP URLs", async () => { | ||
const withEmbedGetSsr = withEmbedSsrAppDir( | ||
getServerSidePropsFnGenerator({ | ||
redirectUrl: "http://calcom.cal.local/owner", | ||
}) | ||
); | ||
|
||
await withEmbedGetSsr( | ||
getServerSidePropsContextArg({ | ||
embedRelatedParams: { | ||
layout: "week_view", | ||
embed: "namespace1", | ||
}, | ||
}) | ||
).catch(() => {}); | ||
|
||
expect(redirect).toHaveBeenCalledWith( | ||
"http://calcom.cal.local/owner/embed?layout=week_view&embed=namespace1" | ||
); | ||
}); | ||
|
||
it("should treat URLs without protocol as relative", async () => { | ||
const withEmbedGetSsr = withEmbedSsrAppDir( | ||
getServerSidePropsFnGenerator({ | ||
redirectUrl: "calcom.cal.local/owner", | ||
}) | ||
); | ||
|
||
await withEmbedGetSsr( | ||
getServerSidePropsContextArg({ | ||
embedRelatedParams: { | ||
layout: "week_view", | ||
embed: "namespace1", | ||
}, | ||
}) | ||
).catch(() => {}); | ||
|
||
expect(redirect).toHaveBeenCalledWith( | ||
"/calcom.cal.local/owner/embed?layout=week_view&embed=namespace1" | ||
); | ||
}); | ||
}); | ||
}); | ||
|
||
describe("when gSSP returns props", () => { | ||
it("should add isEmbed=true prop", async () => { | ||
const withEmbedGetSsr = withEmbedSsrAppDir( | ||
getServerSidePropsFnGenerator({ | ||
props: { | ||
prop1: "value1", | ||
}, | ||
}) | ||
); | ||
|
||
const ret = await withEmbedGetSsr( | ||
getServerSidePropsContextArg({ | ||
embedRelatedParams: { | ||
layout: "week_view", | ||
embed: "", | ||
}, | ||
}) | ||
); | ||
|
||
expect(ret).toEqual({ | ||
prop1: "value1", | ||
isEmbed: true, | ||
}); | ||
}); | ||
}); | ||
|
||
describe("when gSSP returns notFound", () => { | ||
it("should throw notFound", async () => { | ||
const withEmbedGetSsr = withEmbedSsrAppDir( | ||
getServerSidePropsFnGenerator({ | ||
notFound: true, | ||
}) | ||
); | ||
|
||
await withEmbedGetSsr( | ||
getServerSidePropsContextArg({ | ||
embedRelatedParams: { | ||
layout: "week_view", | ||
embed: "", | ||
}, | ||
}) | ||
).catch(() => {}); | ||
|
||
expect(notFound).toHaveBeenCalled(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,9 @@ | ||
import { withAppDirSsr } from "app/WithAppDirSsr"; | ||
import withEmbedSsrAppDir from "app/WithEmbedSSR"; | ||
import { WithLayout } from "app/layoutHOC"; | ||
|
||
import OldPage from "~/bookings/views/bookings-single-view"; | ||
import { getServerSideProps, type PageProps } from "~/bookings/views/bookings-single-view.getServerSideProps"; | ||
|
||
const getData = withAppDirSsr<PageProps>(getServerSideProps); | ||
|
||
const getEmbedData = withEmbedSsrAppDir(getData); | ||
const getEmbedData = withEmbedSsrAppDir<PageProps>(getServerSideProps); | ||
|
||
export default WithLayout({ getLayout: null, getData: getEmbedData, Page: OldPage }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,10 @@ | ||
import { withAppDirSsr } from "app/WithAppDirSsr"; | ||
import withEmbedSsrAppDir from "app/WithEmbedSSR"; | ||
import { WithLayout } from "app/layoutHOC"; | ||
|
||
import { getServerSideProps } from "@server/lib/[user]/[type]/getServerSideProps"; | ||
|
||
import LegacyPage, { type PageProps } from "~/users/views/users-type-public-view"; | ||
|
||
export { generateMetadata } from "../page"; | ||
|
||
const getData = withAppDirSsr<PageProps>(getServerSideProps); | ||
|
||
const getEmbedData = withEmbedSsrAppDir(getData); | ||
const getEmbedData = withEmbedSsrAppDir<PageProps>(getServerSideProps); | ||
|
||
export default WithLayout({ getLayout: null, getData: getEmbedData, Page: LegacyPage })<"P">; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.