Skip to content

Commit e5eca07

Browse files
authored
Branch was updated using the 'autoupdate branch' Actions workflow.
2 parents ed712d5 + 4dea277 commit e5eca07

7 files changed

Lines changed: 38 additions & 9 deletions

File tree

middleware/archived-enterprise-versions-assets.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ const patterns = require('../lib/patterns')
33
const isArchivedVersion = require('../lib/is-archived-version')
44
const got = require('got')
55

6+
const ONE_DAY = 24 * 60 * 60 // 1 day in seconds
7+
68
// This module handles requests for the CSS and JS assets for
79
// deprecated GitHub Enterprise versions by routing them to static content in
810
// help-docs-archived-enterprise-versions
@@ -26,8 +28,10 @@ module.exports = async (req, res, next) => {
2628
res.set('content-length', r.headers['content-length'])
2729
res.set('x-is-archived', 'true')
2830
res.set('x-robots-tag', 'noindex')
29-
res.send(r.body)
31+
// Allow the browser and Fastly to cache these
32+
res.set('cache-control', `public, max-age=${ONE_DAY}`)
33+
return res.send(r.body)
3034
} catch (err) {
31-
next()
35+
return next()
3236
}
3337
}

middleware/archived-enterprise-versions.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,15 @@ module.exports = async (req, res, next) => {
4646
res.set('location', staticRedirect[1])
4747
}
4848

49-
res.send(r.body)
49+
return res.send(r.body)
5050
} catch (err) {
5151
for (const fallbackRedirect of getFallbackRedirects(req, requestedVersion) || []) {
5252
try {
5353
await got(getProxyPath(fallbackRedirect, requestedVersion))
5454
return res.redirect(301, fallbackRedirect)
5555
} catch (err) { } // noop
5656
}
57-
next()
57+
return next()
5858
}
5959
}
6060

middleware/disable-caching-on-safari.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ module.exports = (req, res, next) => {
33
if (isSafari) {
44
res.header('Last-Modified', (new Date()).toUTCString())
55
}
6-
next()
6+
return next()
77
}

middleware/index.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,10 @@ module.exports = function (app) {
3737
app.use(require('./handle-csrf-errors')) // Must come before regular handle-errors
3838

3939
// *** Headers ***
40+
app.set('etag', false) // We will manage our own ETags if desired
4041
app.use(require('compression')())
4142
app.use(require('./disable-caching-on-safari'))
43+
app.use(require('./set-fastly-surrogate-key'))
4244

4345
// *** Config and context for redirects ***
4446
app.use(require('./req-utils')) // Must come before record-redirect and events
@@ -66,10 +68,20 @@ module.exports = function (app) {
6668
etag: false,
6769
immutable: true,
6870
lastModified: false,
69-
maxAge: '28 days'
71+
maxAge: '28 days' // Could be infinite given our fingerprinting
72+
}))
73+
app.use('/assets', express.static('assets', {
74+
index: false,
75+
etag: false,
76+
lastModified: false,
77+
maxAge: '1 day' // Relatively short in case we update images
78+
}))
79+
app.use('/public', express.static('data/graphql', {
80+
index: false,
81+
etag: false,
82+
lastModified: false,
83+
maxAge: '7 days' // A bit longer since releases are more sparse
7084
}))
71-
app.use('/assets', express.static('assets'))
72-
app.use('/public', express.static('data/graphql'))
7385
app.use('/events', instrument('./events'))
7486
app.use('/csrf', instrument('./csrf-route'))
7587
app.use('/search', instrument('./search'))

middleware/render-page.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ module.exports = async function renderPage (req, res, next) {
115115
const output = await liquid.parseAndRender(layout, context)
116116

117117
// First, send the response so the user isn't waiting
118+
// NOTE: Do NOT `return` here as we still need to cache the response afterward!
118119
res.send(output)
119120

120121
// Finally, save output to cache for the next time around
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module.exports = (req, res, next) => {
2+
// Fastly provides a Soft Purge feature that allows you to mark content as outdated (stale) instead of permanently
3+
// purging and thereby deleting it from Fastly's caches. Objects invalidated with Soft Purge will be treated as
4+
// outdated (stale) while Fastly fetches a new version from origin.
5+
//
6+
// Use of a surrogate key is required for soft purging
7+
// https://docs.fastly.com/en/guides/soft-purges
8+
// https://docs.fastly.com/en/guides/getting-started-with-surrogate-keys
9+
res.set('surrogate-key', 'all-the-things')
10+
11+
return next()
12+
}

tests/rendering/server.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ describe('server', () => {
6666
const res = await get('/en')
6767
expect(res.headers['cache-control']).toBe('private, no-store')
6868
expect(res.headers['surrogate-control']).toBe('private, no-store')
69-
expect(res.headers).not.toHaveProperty('surrogate-key')
69+
expect(res.headers['surrogate-key']).toBe('all-the-things')
7070
})
7171

7272
test('does not render duplicate <html> or <body> tags', async () => {

0 commit comments

Comments
 (0)