-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
105 improve the dx around the ssr stuff (#107)
* Rework SSR to build up a data object on server This simplifies the calls if you use SsrContextServer * Add params to query key Ensure that params are sorted before stringing
- Loading branch information
Showing
11 changed files
with
105 additions
and
64 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
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,16 @@ | ||
import { getInitialData } from "~/hooks/useQuerySsrServer"; | ||
|
||
import { SsrContextProvider } from "./SsrContext"; | ||
|
||
export function SsrContextServer({ children }: { children: React.ReactNode }) { | ||
// this handles some magic | ||
// the callQuerySsrServer funcs are building up the initial data | ||
// it's all happening in the object held by getInitialData() | ||
const initialData = getInitialData(); | ||
|
||
return ( | ||
<SsrContextProvider initialData={initialData}> | ||
{children} | ||
</SsrContextProvider> | ||
); | ||
} |
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,28 +1,17 @@ | ||
import { callQuerySsrServer } from "~/hooks/useQuerySsrServer"; | ||
import { appRouter } from "~/server/api/root"; | ||
|
||
import { SsrContextProvider } from "../SsrContext"; | ||
import { deepMerge } from "../deepMerge"; | ||
import { SsrContextServer } from "../SsrContextServer"; | ||
|
||
export default async function RootLayout({ | ||
children, | ||
}: { | ||
children: React.ReactNode; | ||
}) { | ||
const initialData = await callQuerySsrServer( | ||
appRouter.awardRouter.getAllAwardsForProfile | ||
); | ||
await callQuerySsrServer(appRouter.awardRouter.getAllAwardsForProfile); | ||
await callQuerySsrServer(appRouter.awardRouter.getAllAwardImages, { | ||
shouldLimitToProfile: true, | ||
}); | ||
|
||
const moreData = await callQuerySsrServer( | ||
appRouter.awardRouter.getAllAwardImages, | ||
{ | ||
shouldLimitToProfile: true, | ||
} | ||
); | ||
|
||
const mergedData = deepMerge(initialData, moreData); | ||
|
||
return ( | ||
<SsrContextProvider initialData={mergedData}>{children}</SsrContextProvider> | ||
); | ||
return <SsrContextServer>{children}</SsrContextServer>; | ||
} |
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
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,20 +1,14 @@ | ||
import { callQuerySsrServer } from "~/hooks/useQuerySsrServer"; | ||
import { appRouter } from "~/server/api/root"; | ||
|
||
import { SsrContextProvider } from "../SsrContext"; | ||
import { SsrContextServer } from "../SsrContextServer"; | ||
|
||
export default async function RootLayout({ | ||
children, | ||
}: { | ||
children: React.ReactNode; | ||
}) { | ||
const initialData = await callQuerySsrServer( | ||
appRouter.planRouter.getAllLearningPlans | ||
); | ||
await callQuerySsrServer(appRouter.planRouter.getAllLearningPlans); | ||
|
||
return ( | ||
<SsrContextProvider initialData={initialData}> | ||
{children} | ||
</SsrContextProvider> | ||
); | ||
return <SsrContextServer>{children}</SsrContextServer>; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
export function deepSortObjectByKeys(obj: any) { | ||
if (typeof obj !== "object") { | ||
return obj; | ||
} | ||
|
||
const sortedObj = {}; | ||
|
||
Object.keys(obj) | ||
.sort() | ||
.forEach((key) => { | ||
(sortedObj as any)[key] = deepSortObjectByKeys(obj[key]); | ||
}); | ||
|
||
return sortedObj; | ||
} |
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