Skip to content

Commit 0e729d9

Browse files
authored
Cleanup the testing and old code related to Fastly headers (#17643)
1 parent cababa4 commit 0e729d9

2 files changed

Lines changed: 5 additions & 51 deletions

File tree

Lines changed: 4 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,8 @@
1-
const FASTLY_TTL = process.env.FASTLY_TTL || String(60 * 60 * 24) // 24 hours
2-
const STALE_TTL = String(60 * 10) // 10 minutes
3-
41
module.exports = (req, res, next) => {
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
10-
res.set({
11-
'surrogate-control': 'private, no-store',
12-
'cache-control': 'private, no-store'
13-
})
14-
return next()
15-
}
16-
2+
// Disallow both Fastly AND the browser from caching HTML pages
173
res.set({
18-
19-
// Say you want Fastly to cache your content but you don't want it cached by browsers.
20-
// The best way to do this would be to send Fastly both the Cache-Control header as you want
21-
// it to go to the browsers, and use Surrogate-Control to tell us how long to cache for.
22-
23-
// Fastly does not currently respect no-store or no-cache directives.
24-
// Including either or both of these in a Cache-Control header has no effect on
25-
// Fastly's caching decision, unless you alter this behavior using custom VCL.
26-
27-
// https://docs.fastly.com/en/guides/configuring-caching
28-
'cache-control': 'no-store, must-revalidate',
29-
30-
// This header gets stripped and is only visible to Fastly caches.
31-
// https://docs.fastly.com/en/guides/serving-stale-content#manually-enabling-serve-stale
32-
'surrogate-control': `max-age=${FASTLY_TTL}, stale-if-error=${STALE_TTL}, stale-while-revalidate=${STALE_TTL}`,
33-
34-
// Fastly provides a Soft Purge feature that allows you to mark content as outdated (stale) instead of permanently
35-
// purging and thereby deleting it from Fastly's caches. Objects invalidated with Soft Purge will be treated as
36-
// outdated (stale) while Fastly fetches a new version from origin.
37-
//
38-
// Use of a surrogate key is required for soft purging
39-
// https://docs.fastly.com/en/guides/soft-purges
40-
// https://docs.fastly.com/en/guides/getting-started-with-surrogate-keys
41-
'surrogate-key': 'all-the-things'
4+
'surrogate-control': 'private, no-store',
5+
'cache-control': 'private, no-store'
426
})
43-
44-
next()
7+
return next()
458
}

tests/rendering/server.js

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,7 @@ describe('server', () => {
5959
expect(csp.get('style-src').includes("'unsafe-inline'")).toBe(true)
6060
})
6161

62-
test('sets Fastly cache control headers', async () => {
63-
const res = await get('/en')
64-
expect(res.headers['cache-control']).toBe('no-store, must-revalidate')
65-
expect(res.headers['surrogate-control']).toBe('max-age=86400, stale-if-error=600, stale-while-revalidate=600')
66-
expect(res.headers['surrogate-key']).toBe('all-the-things')
67-
})
68-
69-
test('sets Fastly cache control headers to bypass if enabled', async () => {
70-
process.env.TEST_BYPASS_FASTLY = 'true'
71-
62+
test('sets Fastly cache control headers to bypass pages', async () => {
7263
const res = await get('/en')
7364
expect(res.headers['cache-control']).toBe('private, no-store')
7465
expect(res.headers['surrogate-control']).toBe('private, no-store')

0 commit comments

Comments
 (0)