MCPcopy Index your code
hub / github.com/github/docs / precompileRedirects

Function precompileRedirects

lib/redirects/precompile.js:9–58  ·  view source on GitHub ↗
(pageList)

Source from the content-addressed store, hash-verified

7// This function runs at server warmup and precompiles possible redirect routes.
8// It outputs them in key-value pairs within a neat Javascript object: { oldPath: newPath }
9export async function precompileRedirects(pageList) {
10 const allRedirects = readCompressedJsonFileFallback('./lib/redirects/static/developer.json')
11
12 const externalRedirects = readCompressedJsonFileFallback('./lib/redirects/external-sites.json')
13 Object.assign(allRedirects, externalRedirects)
14
15 // CURRENT PAGES PERMALINKS AND FRONTMATTER
16 // create backwards-compatible old paths for page permalinks and frontmatter redirects
17 pageList
18 .filter((page) => page.languageCode === 'en')
19 .forEach((page) => Object.assign(allRedirects, page.buildRedirects()))
20
21 // NOTE: Exception redirects **MUST COME AFTER** pageList redirects above in order
22 // to properly override them. Exception redirects are unicorn one-offs that are not
23 // otherwise handled by the versionless redirect fallbacks (see lib/all-versions.js).
24 //
25 // Examples of exceptions:
26 // * We deprecate the FPT version of a page, and we want the FPT version to redirect
27 // to a different version that goes against the order in lib/all-versions.js.
28 // * We deprecate a non-FPT version of a page, and we want the old version to redirect
29 // to a different version. Because the order in lib/all-versions.js only covers
30 // versionless links (like `/foo`), we need to specify an exception for the old
31 // versioned links (like `/enterprise-cloud@latest/foo`).
32 // * We deprecate a version of a page, and instead of falling back to the next
33 // available version, we want to redirect that version to a different page entirely.
34 //
35 // The advantage of the exception redirects file is that it's encoded in plain
36 // text so it's possible to write comments and it's also possible to write 1
37 // destination URL once for each N redirect origins.
38 const exceptions = getExceptionRedirects(EXCEPTIONS_FILE)
39 Object.assign(allRedirects, exceptions)
40
41 Object.entries(allRedirects).forEach(([fromURI, toURI]) => {
42 // If the destination URL has a hardcoded `enterprise-server@latest` in
43 // it we need to rewrite that now.
44 // We never want to redirect to that as the final URL (in the 301 response)
45 // but it might make sense for it to be in the `developer.json`
46 // file since that it static.
47 //
48 //
49 if (toURI.includes('/enterprise-server@latest')) {
50 allRedirects[fromURI] = toURI.replace(
51 '/enterprise-server@latest',
52 `/enterprise-server@${latest}`
53 )
54 }
55 })
56
57 return allRedirects
58}
59
60export default precompileRedirects

Calls 3

getExceptionRedirectsFunction · 0.85
buildRedirectsMethod · 0.80

Tested by

no test coverage detected