Skip to content

Commit 4b7a12f

Browse files
committed
reduce some repitition in prerendering graphql
1 parent 02cfde7 commit 4b7a12f

File tree

3 files changed

+23
-30
lines changed

3 files changed

+23
-30
lines changed

script/graphql/update-files.js

+17-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const processSchemas = require('./utils/process-schemas')
1616
const prerenderObjects = require('./utils/prerender-objects')
1717
const prerenderInputObjects = require('./utils/prerender-input-objects')
1818
const { prependDatedEntry, createChangelogEntry } = require('./build-changelog')
19+
const loadData = require('../../lib/site-data')
1920

2021
// check for required PAT
2122
if (!process.env.GITHUB_TOKEN) {
@@ -37,6 +38,8 @@ const removeHiddenMembersScript = path.join(__dirname, './utils/remove-hidden-sc
3738

3839
const versionsToBuild = Object.keys(allVersions)
3940

41+
const currentLanguage = 'en'
42+
4043
main()
4144

4245
async function main () {
@@ -45,6 +48,14 @@ async function main () {
4548
const prerenderedObjects = {}
4649
const prerenderedInputObjects = {}
4750

51+
const siteData = await loadData()
52+
53+
// create a bare minimum context for rendering the graphql-object.html layout
54+
const context = {
55+
currentLanguage,
56+
site: siteData[currentLanguage].site,
57+
}
58+
4859
for (const version of versionsToBuild) {
4960
// Get the relevant GraphQL name for the current version
5061
// For example, free-pro-team@latest corresponds to dotcom,
@@ -75,13 +86,17 @@ async function main () {
7586
const schemaJsonPerVersion = await processSchemas(safeForPublicSchema, safeForPublicPreviews)
7687
updateStaticFile(schemaJsonPerVersion, path.join(graphqlStaticDir, `schema-${graphqlVersion}.json`))
7788

89+
// Add some version specific data to the context
90+
context.graphql = { schemaForCurrentVersion: schemaJsonPerVersion }
91+
context.currentVersion = version
92+
7893
// 4. PRERENDER OBJECTS HTML
7994
// because the objects page is too big to render on page load
80-
prerenderedObjects[graphqlVersion] = await prerenderObjects(schemaJsonPerVersion, version)
95+
prerenderedObjects[graphqlVersion] = await prerenderObjects(context)
8196

8297
// 5. PRERENDER INPUT OBJECTS HTML
8398
// because the objects page is too big to render on page load
84-
prerenderedInputObjects[graphqlVersion] = await prerenderInputObjects(schemaJsonPerVersion, version)
99+
prerenderedInputObjects[graphqlVersion] = await prerenderInputObjects(context)
85100

86101
// 6. UPDATE CHANGELOG
87102
if (allVersions[version].nonEnterpriseDefault) {

script/graphql/utils/prerender-input-objects.js

+3-14
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,16 @@ const getMiniTocItems = require('../../../lib/get-mini-toc-items')
66
const rewriteLocalLinks = require('../../../lib/rewrite-local-links')
77
const includes = path.join(process.cwd(), 'includes')
88
const inputObjectIncludeFile = fs.readFileSync(path.join(includes, 'graphql-input-object.html'), 'utf8')
9-
// TODO need to localize
10-
const currentLanguage = 'en'
11-
12-
module.exports = async function prerenderInputObjects (schemaJsonPerVersion, version) {
13-
const site = await require('../../../lib/site-data')()
14-
15-
// create a bare minimum context for rendering the graphql-object.html layout
16-
const context = {
17-
currentLanguage,
18-
site: site[currentLanguage].site,
19-
graphql: { schemaForCurrentVersion: schemaJsonPerVersion }
20-
}
219

10+
module.exports = async function prerenderInputObjects (context) {
2211
const inputObjectsArray = []
2312

2413
// render the graphql-object.html layout for every object
25-
for (const inputObject of schemaJsonPerVersion.inputObjects) {
14+
for (const inputObject of context.graphql.schemaForCurrentVersion.inputObjects) {
2615
context.item = inputObject
2716
const inputObjectHtml = await liquid.parseAndRender(inputObjectIncludeFile, context)
2817
const $ = cheerio.load(inputObjectHtml, { xmlMode: true })
29-
rewriteLocalLinks($, version, currentLanguage)
18+
rewriteLocalLinks($, context.currentVersion, context.currentLanguage)
3019
const htmlWithVersionedLinks = $.html()
3120
inputObjectsArray.push(htmlWithVersionedLinks)
3221
}

script/graphql/utils/prerender-objects.js

+3-14
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,16 @@ const getMiniTocItems = require('../../../lib/get-mini-toc-items')
66
const rewriteLocalLinks = require('../../../lib/rewrite-local-links')
77
const includes = path.join(process.cwd(), 'includes')
88
const objectIncludeFile = fs.readFileSync(path.join(includes, 'graphql-object.html'), 'utf8')
9-
// TODO need to localize
10-
const currentLanguage = 'en'
11-
12-
module.exports = async function prerenderObjects (schemaJsonPerVersion, version) {
13-
const site = await require('../../../lib/site-data')()
14-
15-
// create a bare minimum context for rendering the graphql-object.html layout
16-
const context = {
17-
currentLanguage,
18-
site: site[currentLanguage].site,
19-
graphql: { schemaForCurrentVersion: schemaJsonPerVersion }
20-
}
219

10+
module.exports = async function prerenderObjects (context) {
2211
const objectsArray = []
2312

2413
// render the graphql-object.html layout for every object
25-
for (const object of schemaJsonPerVersion.objects) {
14+
for (const object of context.graphql.schemaForCurrentVersion.objects) {
2615
context.item = object
2716
const objectHtml = await liquid.parseAndRender(objectIncludeFile, context)
2817
const $ = cheerio.load(objectHtml, { xmlMode: true })
29-
rewriteLocalLinks($, version, currentLanguage)
18+
rewriteLocalLinks($, context.currentVersion, context.currentLanguage)
3019
const htmlWithVersionedLinks = $.html()
3120
objectsArray.push(htmlWithVersionedLinks)
3221
}

0 commit comments

Comments
 (0)