cache rest data for translated pages (#25784)

This commit is contained in:
Rachael Sewell 2022-03-02 20:21:02 -08:00 коммит произвёл GitHub
Родитель 2f4fb547d0
Коммит 764966735b
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
1 изменённых файлов: 26 добавлений и 8 удалений

Просмотреть файл

@ -30,8 +30,10 @@ type CategoryDataT = {
}
type RestDataT = {
[version: string]: {
[category: string]: CategoryDataT
[language: string]: {
[version: string]: {
[category: string]: CategoryDataT
}
}
}
@ -91,6 +93,7 @@ export const getServerSideProps: GetServerSideProps<Props> = async (context) =>
// e.g. the `activity` from `/en/rest/reference/activity#events`
const category = context.params!.category as string
const currentVersion = context.params!.versionId as string
const currentLanguage = req.context.currentLanguage as string
// Use a local cache to store all of the REST operations, so
// we only read the directory of static/decorated files once
@ -98,15 +101,29 @@ export const getServerSideProps: GetServerSideProps<Props> = async (context) =>
rest = (await getRest()) as RestOperationsT
}
/* This sets up a skeleton object in the format:
{
'en': { free-pro-team@latest: {}, enterprise-cloud@latest: {}},
'ja': { free-pro-team@latest: {}, enterprise-cloud@latest: {}}
}
*/
if (!restOperationData) {
restOperationData = {}
Object.keys(req.context.allVersions).forEach((version) => (restOperationData![version] = {}))
Object.keys(req.context.languages).forEach((language) => {
restOperationData![language] = {}
Object.keys(req.context.allVersions).forEach(
(version) => (restOperationData![language][version] = {})
)
})
}
const restOperations = rest[currentVersion][category]
if (!(category in restOperationData[currentVersion])) {
restOperationData[currentVersion][category] = (await getRestOperationData(
// The context passed will have the Markdown content for the language
// of the page being requested and the Markdown will be rendered
// using the `currentVersion`
if (!(category in restOperationData[currentLanguage][currentVersion])) {
restOperationData[currentLanguage][currentVersion][category] = (await getRestOperationData(
category,
restOperations,
req.context
@ -118,15 +135,16 @@ export const getServerSideProps: GetServerSideProps<Props> = async (context) =>
// are undefined. We need to populate those properties with the static
// data read from the decorated schema files.
const articleContext = getArticleContextFromRequest(req)
articleContext.miniTocItems = restOperationData[currentVersion][category].miniTocItems
articleContext.miniTocItems =
restOperationData[currentLanguage][currentVersion][category].miniTocItems
return {
props: {
restOperations,
mainContext: getMainContext(req, res),
descriptions: restOperationData[currentVersion][category].descriptions,
descriptions: restOperationData[currentLanguage][currentVersion][category].descriptions,
articleContext: articleContext,
introContent: restOperationData[currentVersion][category].introContent,
introContent: restOperationData[currentLanguage][currentVersion][category].introContent,
},
}
}