Skip to content

Commit 3aa1b1f

Browse files
authored
lazily load graphql static json files (github#25981)
1 parent 1a6bee4 commit 3aa1b1f

File tree

3 files changed

+51
-48
lines changed

3 files changed

+51
-48
lines changed

lib/read-json-file.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,34 @@ export function readCompressedJsonFileFallback(xpath) {
3030
}
3131
}
3232
}
33+
34+
// Wrapper on readCompressedJsonFileFallback that initially only checks
35+
// if the file exists but doesn't read the content till you call it.
36+
export function readCompressedJsonFileFallbackLazily(xpath) {
37+
const cache = new Map()
38+
// This will throw if the file isn't accessible at all, e.g. ENOENT
39+
// But, the file might have been replaced by one called `SAMENAME.json.br`
40+
// because in staging, we ship these files compressed to make the
41+
// deployment faster. So, in our file-presence check, we need to
42+
// account for that.
43+
try {
44+
fs.accessSync(xpath)
45+
} catch (err) {
46+
if (err.code === 'ENOENT') {
47+
try {
48+
fs.accessSync(xpath + '.br')
49+
} catch (err) {
50+
if (err.code === 'ENOENT') {
51+
throw new Error(`Neither ${xpath} nor ${xpath}.br is accessible`)
52+
}
53+
throw err
54+
}
55+
} else {
56+
throw err
57+
}
58+
}
59+
return () => {
60+
if (!cache.has(xpath)) cache.set(xpath, readCompressedJsonFileFallback(xpath))
61+
return cache.get(xpath)
62+
}
63+
}

middleware/archived-enterprise-versions.js

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import fs from 'fs'
21
import path from 'path'
32
import slash from 'slash'
43
import statsd from '../lib/statsd.js'
@@ -11,7 +10,7 @@ import versionSatisfiesRange from '../lib/version-satisfies-range.js'
1110
import isArchivedVersion from '../lib/is-archived-version.js'
1211
import { setFastlySurrogateKey, SURROGATE_ENUMS } from './set-fastly-surrogate-key.js'
1312
import got from 'got'
14-
import { readCompressedJsonFileFallback } from '../lib/read-json-file.js'
13+
import { readCompressedJsonFileFallbackLazily } from '../lib/read-json-file.js'
1514
import { cacheControlFactory } from './cache-control.js'
1615
import { pathLanguagePrefixed, languagePrefixPathRegex } from '../lib/languages.js'
1716

@@ -25,42 +24,13 @@ function splitByLanguage(uri) {
2524
return [language, withoutLanguage]
2625
}
2726

28-
function readJsonFileLazily(xpath) {
29-
const cache = new Map()
30-
// This will throw if the file isn't accessible at all, e.g. ENOENT
31-
// But, the file might have been replaced by one called `SAMENAME.json.br`
32-
// because in staging, we ship these files compressed to make the
33-
// deployment faster. So, in our file-presence check, we need to
34-
// account for that.
35-
try {
36-
fs.accessSync(xpath)
37-
} catch (err) {
38-
if (err.code === 'ENOENT') {
39-
try {
40-
fs.accessSync(xpath + '.br')
41-
} catch (err) {
42-
if (err.code === 'ENOENT') {
43-
throw new Error(`Neither ${xpath} nor ${xpath}.br is accessible`)
44-
}
45-
throw err
46-
}
47-
} else {
48-
throw err
49-
}
50-
}
51-
return () => {
52-
if (!cache.has(xpath)) cache.set(xpath, readCompressedJsonFileFallback(xpath))
53-
return cache.get(xpath)
54-
}
55-
}
56-
5727
// These files are huge so lazy-load them. But note that the
5828
// `readJsonFileLazily()` function will, at import-time, check that
5929
// the path does exist.
60-
const archivedRedirects = readJsonFileLazily(
30+
const archivedRedirects = readCompressedJsonFileFallbackLazily(
6131
'./lib/redirects/static/archived-redirects-from-213-to-217.json'
6232
)
63-
const archivedFrontmatterFallbacks = readJsonFileLazily(
33+
const archivedFrontmatterFallbacks = readCompressedJsonFileFallbackLazily(
6434
'./lib/redirects/static/archived-frontmatter-fallbacks.json'
6535
)
6636

middleware/contextualizers/graphql.js

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1-
import { readCompressedJsonFileFallback } from '../../lib/read-json-file.js'
1+
import { readCompressedJsonFileFallbackLazily } from '../../lib/read-json-file.js'
22
import { allVersions } from '../../lib/all-versions.js'
3-
const previews = readCompressedJsonFileFallback('./lib/graphql/static/previews.json')
4-
const upcomingChanges = readCompressedJsonFileFallback('./lib/graphql/static/upcoming-changes.json')
5-
const changelog = readCompressedJsonFileFallback('./lib/graphql/static/changelog.json')
6-
const prerenderedObjects = readCompressedJsonFileFallback(
3+
const previews = readCompressedJsonFileFallbackLazily('./lib/graphql/static/previews.json')
4+
const upcomingChanges = readCompressedJsonFileFallbackLazily(
5+
'./lib/graphql/static/upcoming-changes.json'
6+
)
7+
const changelog = readCompressedJsonFileFallbackLazily('./lib/graphql/static/changelog.json')
8+
const prerenderedObjects = readCompressedJsonFileFallbackLazily(
79
'./lib/graphql/static/prerendered-objects.json'
810
)
9-
const prerenderedInputObjects = readCompressedJsonFileFallback(
11+
const prerenderedInputObjects = readCompressedJsonFileFallbackLazily(
1012
'./lib/graphql/static/prerendered-input-objects.json'
1113
)
12-
const prerenderedMutations = readCompressedJsonFileFallback(
14+
const prerenderedMutations = readCompressedJsonFileFallbackLazily(
1315
'./lib/graphql/static/prerendered-mutations.json'
1416
)
1517

@@ -32,16 +34,16 @@ export default function graphqlContext(req, res, next) {
3234
const graphqlVersion = currentVersionObj.miscVersionName
3335

3436
req.context.graphql = {
35-
schemaForCurrentVersion: readCompressedJsonFileFallback(
37+
schemaForCurrentVersion: readCompressedJsonFileFallbackLazily(
3638
`lib/graphql/static/schema-${graphqlVersion}.json`
37-
),
38-
previewsForCurrentVersion: previews[graphqlVersion],
39-
upcomingChangesForCurrentVersion: upcomingChanges[graphqlVersion],
40-
prerenderedObjectsForCurrentVersion: prerenderedObjects[graphqlVersion],
41-
prerenderedInputObjectsForCurrentVersion: prerenderedInputObjects[graphqlVersion],
42-
prerenderedMutationsForCurrentVersion: prerenderedMutations[graphqlVersion],
39+
)(),
40+
previewsForCurrentVersion: previews()[graphqlVersion],
41+
upcomingChangesForCurrentVersion: upcomingChanges()[graphqlVersion],
42+
prerenderedObjectsForCurrentVersion: prerenderedObjects()[graphqlVersion],
43+
prerenderedInputObjectsForCurrentVersion: prerenderedInputObjects()[graphqlVersion],
44+
prerenderedMutationsForCurrentVersion: prerenderedMutations()[graphqlVersion],
4345
explorerUrl,
44-
changelog,
46+
changelog: changelog(),
4547
}
4648

4749
return next()

0 commit comments

Comments
 (0)