Skip to content

Commit 8f133cb

Browse files
authored
Disallow both Fastly AND the browser from caching HTML pages (#17641)
1 parent 558858c commit 8f133cb

3 files changed

Lines changed: 17 additions & 6 deletions

File tree

middleware/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ module.exports = function (app) {
3838

3939
// *** Headers ***
4040
app.use(require('compression')())
41-
app.use(require('./set-fastly-cache-headers'))
4241
app.use(require('./disable-caching-on-safari'))
4342

4443
// *** Config and context for redirects ***
@@ -93,6 +92,9 @@ module.exports = function (app) {
9392
app.use(instrument('./featured-links'))
9493
app.use(instrument('./learning-track'))
9594

95+
// *** Headers for pages only ***
96+
app.use(require('./set-fastly-cache-headers'))
97+
9698
// *** Rendering, must go last ***
9799
app.get('/*', asyncMiddleware(instrument('./render-page')))
98100
app.use(require('./handle-errors'))

middleware/set-fastly-cache-headers.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
const FASTLY_TTL = process.env.FASTLY_TTL || String(60 * 60 * 24) // 24 hours
22
const STALE_TTL = String(60 * 10) // 10 minutes
33

4-
const BYPASS_FASTLY = process.env.TEST_BYPASS_FASTLY === 'true'
5-
const BYPASS_PRODUCTS = /^\/([a-z]{2})\/([a-z0-9._-]+@[a-z0-9._-]+\/)?github(\/.*|$)/i
6-
74
module.exports = (req, res, next) => {
8-
// Test bypassing Fastly for all pages inside of the Discussions product
9-
if (BYPASS_FASTLY && !BYPASS_PRODUCTS.test(req.originalUrl)) {
5+
const BYPASS_FASTLY = process.env.TEST_BYPASS_FASTLY === 'true'
6+
7+
// Bypass Fastly caching for all rendered pages
8+
if (BYPASS_FASTLY) {
9+
// Disallow both Fastly AND the browser from caching HTML pages
1010
res.set({
1111
'surrogate-control': 'private, no-store',
1212
'cache-control': 'private, no-store'

tests/rendering/server.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,15 @@ describe('server', () => {
6666
expect(res.headers['surrogate-key']).toBe('all-the-things')
6767
})
6868

69+
test('sets Fastly cache control headers to bypass if enabled', async () => {
70+
process.env.TEST_BYPASS_FASTLY = 'true'
71+
72+
const res = await get('/en')
73+
expect(res.headers['cache-control']).toBe('private, no-store')
74+
expect(res.headers['surrogate-control']).toBe('private, no-store')
75+
expect(res.headers).not.toHaveProperty('surrogate-key')
76+
})
77+
6978
test('does not render duplicate <html> or <body> tags', async () => {
7079
const $ = await getDOM('/en')
7180
expect($('html').length).toBe(1)

0 commit comments

Comments
 (0)