|
1 | | -const FASTLY_TTL = process.env.FASTLY_TTL || String(60 * 60 * 24) // 24 hours |
2 | | -const STALE_TTL = String(60 * 10) // 10 minutes |
3 | | - |
4 | 1 | 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 |
17 | 3 | 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' |
42 | 6 | }) |
43 | | - |
44 | | - next() |
| 7 | + return next() |
45 | 8 | } |
0 commit comments