Skip to content

Commit 5c2777a

Browse files
author
Peter Bengtsson
authored
aggressive cache-control on archived redirects (#23091)
Part of #1271
1 parent 7b7d78e commit 5c2777a

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

middleware/archived-enterprise-versions-assets.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import path from 'path'
2+
3+
import got from 'got'
4+
25
import patterns from '../lib/patterns.js'
36
import isArchivedVersion from '../lib/is-archived-version.js'
4-
import got from 'got'
7+
import { cacheControlFactory } from './cache-control.js'
58

6-
const ONE_DAY = 24 * 60 * 60 // 1 day in seconds
9+
const cacheControl = cacheControlFactory(60 * 60 * 24)
710

811
// This module handles requests for the CSS and JS assets for
912
// deprecated GitHub Enterprise versions by routing them to static content in
@@ -31,7 +34,7 @@ export default async function archivedEnterpriseVersionsAssets(req, res, next) {
3134
res.set('x-is-archived', 'true')
3235
res.set('x-robots-tag', 'noindex')
3336
// Allow the browser and Fastly to cache these
34-
res.set('cache-control', `public, max-age=${ONE_DAY}`)
37+
cacheControl(res)
3538
return res.send(r.body)
3639
} catch (err) {
3740
return next(404)

middleware/archived-enterprise-versions.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,16 @@ import versionSatisfiesRange from '../lib/version-satisfies-range.js'
99
import isArchivedVersion from '../lib/is-archived-version.js'
1010
import got from 'got'
1111
import readJsonFile from '../lib/read-json-file.js'
12+
import { cacheControlFactory } from './cache-control.js'
1213
const archivedRedirects = readJsonFile(
1314
'./lib/redirects/static/archived-redirects-from-213-to-217.json'
1415
)
1516
const archivedFrontmatterFallbacks = readJsonFile(
1617
'./lib/redirects/static/archived-frontmatter-fallbacks.json'
1718
)
1819

20+
const cacheControl = cacheControlFactory(60 * 60 * 24 * 365)
21+
1922
async function getRemoteJSON(url) {
2023
if (_getRemoteJSONCache.has(url)) {
2124
return _getRemoteJSONCache.get(url)
@@ -42,6 +45,7 @@ export default async function archivedEnterpriseVersions(req, res, next) {
4245
req.path.startsWith('/en/') &&
4346
versionSatisfiesRange(requestedVersion, `<${firstVersionDeprecatedOnNewSite}`)
4447
) {
48+
cacheControl(res)
4549
return res.redirect(301, req.baseUrl + req.path.replace(/^\/en/, ''))
4650
}
4751

@@ -53,6 +57,7 @@ export default async function archivedEnterpriseVersions(req, res, next) {
5357
) {
5458
const redirect = archivedRedirects[req.path]
5559
if (redirect && redirect !== req.path) {
60+
cacheControl(res)
5661
return res.redirect(301, redirect)
5762
}
5863
}
@@ -64,6 +69,7 @@ export default async function archivedEnterpriseVersions(req, res, next) {
6469
// make redirects found via redirects.json redirect with a 301
6570
if (redirectJson[req.path]) {
6671
res.set('x-robots-tag', 'noindex')
72+
cacheControl(res)
6773
return res.redirect(301, redirectJson[req.path])
6874
}
6975
} catch (err) {
@@ -78,6 +84,7 @@ export default async function archivedEnterpriseVersions(req, res, next) {
7884
// make stubbed redirect files (which exist in versions <2.13) redirect with a 301
7985
const staticRedirect = r.body.match(patterns.staticRedirect)
8086
if (staticRedirect) {
87+
cacheControl(res)
8188
return res.redirect(301, staticRedirect[1])
8289
}
8390

@@ -87,6 +94,7 @@ export default async function archivedEnterpriseVersions(req, res, next) {
8794
for (const fallbackRedirect of getFallbackRedirects(req, requestedVersion) || []) {
8895
try {
8996
await got(getProxyPath(fallbackRedirect, requestedVersion))
97+
cacheControl(res)
9098
return res.redirect(301, fallbackRedirect)
9199
} catch (err) {
92100
// noop

middleware/cache-control.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Return a function you can pass a Response object to and it will
2+
// set the `Cache-Control` header.
3+
//
4+
// For example:
5+
//
6+
// const cacheControlYear = getCacheControl(60 * 60 * 24 * 365)
7+
// ...
8+
// cacheControlYear(res)
9+
// res.send(body)
10+
//
11+
export function cacheControlFactory(maxAge = 60 * 60, public_ = true) {
12+
return (res) => res.set('cache-control', `${public_ ? 'public, ' : ''}max-age=${maxAge}`)
13+
}

0 commit comments

Comments
 (0)