Skip to content

Commit 0f5597b

Browse files
authored
repo sync
2 parents ed712d5 + a40ff31 commit 0f5597b

4 files changed

Lines changed: 32 additions & 4 deletions

File tree

middleware/archived-enterprise-versions-assets.js

Lines changed: 4 additions & 0 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,6 +28,8 @@ 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')
31+
// Allow the browser and Fastly to cache these
32+
res.set('cache-control', `public, max-age=${ONE_DAY}`)
2933
res.send(r.body)
3034
} catch (err) {
3135
next()

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'))
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+
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)