Skip to content

Commit 7649667

Browse files
authored
cache rest data for translated pages (github#25784)
1 parent 2f4fb54 commit 7649667

File tree

1 file changed

+26
-8
lines changed

1 file changed

+26
-8
lines changed

pages/[versionId]/rest/reference/[category].tsx

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,10 @@ type CategoryDataT = {
3030
}
3131

3232
type RestDataT = {
33-
[version: string]: {
34-
[category: string]: CategoryDataT
33+
[language: string]: {
34+
[version: string]: {
35+
[category: string]: CategoryDataT
36+
}
3537
}
3638
}
3739

@@ -91,22 +93,37 @@ export const getServerSideProps: GetServerSideProps<Props> = async (context) =>
9193
// e.g. the `activity` from `/en/rest/reference/activity#events`
9294
const category = context.params!.category as string
9395
const currentVersion = context.params!.versionId as string
96+
const currentLanguage = req.context.currentLanguage as string
9497

9598
// Use a local cache to store all of the REST operations, so
9699
// we only read the directory of static/decorated files once
97100
if (!rest) {
98101
rest = (await getRest()) as RestOperationsT
99102
}
100103

104+
/* This sets up a skeleton object in the format:
105+
{
106+
'en': { free-pro-team@latest: {}, enterprise-cloud@latest: {}},
107+
'ja': { free-pro-team@latest: {}, enterprise-cloud@latest: {}}
108+
}
109+
*/
101110
if (!restOperationData) {
102111
restOperationData = {}
103-
Object.keys(req.context.allVersions).forEach((version) => (restOperationData![version] = {}))
112+
Object.keys(req.context.languages).forEach((language) => {
113+
restOperationData![language] = {}
114+
Object.keys(req.context.allVersions).forEach(
115+
(version) => (restOperationData![language][version] = {})
116+
)
117+
})
104118
}
105119

106120
const restOperations = rest[currentVersion][category]
107121

108-
if (!(category in restOperationData[currentVersion])) {
109-
restOperationData[currentVersion][category] = (await getRestOperationData(
122+
// The context passed will have the Markdown content for the language
123+
// of the page being requested and the Markdown will be rendered
124+
// using the `currentVersion`
125+
if (!(category in restOperationData[currentLanguage][currentVersion])) {
126+
restOperationData[currentLanguage][currentVersion][category] = (await getRestOperationData(
110127
category,
111128
restOperations,
112129
req.context
@@ -118,15 +135,16 @@ export const getServerSideProps: GetServerSideProps<Props> = async (context) =>
118135
// are undefined. We need to populate those properties with the static
119136
// data read from the decorated schema files.
120137
const articleContext = getArticleContextFromRequest(req)
121-
articleContext.miniTocItems = restOperationData[currentVersion][category].miniTocItems
138+
articleContext.miniTocItems =
139+
restOperationData[currentLanguage][currentVersion][category].miniTocItems
122140

123141
return {
124142
props: {
125143
restOperations,
126144
mainContext: getMainContext(req, res),
127-
descriptions: restOperationData[currentVersion][category].descriptions,
145+
descriptions: restOperationData[currentLanguage][currentVersion][category].descriptions,
128146
articleContext: articleContext,
129-
introContent: restOperationData[currentVersion][category].introContent,
147+
introContent: restOperationData[currentLanguage][currentVersion][category].introContent,
130148
},
131149
}
132150
}

0 commit comments

Comments
 (0)