forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathset-fastly-cache-headers.js
33 lines (26 loc) · 1.62 KB
/
set-fastly-cache-headers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
const FASTLY_TTL = process.env.FASTLY_TTL || String(60 * 60 * 24) // 24 hours
const STALE_TTL = String(60 * 10) // 10 minutes
module.exports = (req, res, next) => {
res.set({
// Say you want Fastly to cache your content but you don't want it cached by browsers.
// The best way to do this would be to send Fastly both the Cache-Control header as you want
// it to go to the browsers, and use Surrogate-Control to tell us how long to cache for.
// Fastly does not currently respect no-store or no-cache directives.
// Including either or both of these in a Cache-Control header has no effect on
// Fastly's caching decision, unless you alter this behavior using custom VCL.
// https://docs.fastly.com/en/guides/configuring-caching
'cache-control': 'no-store, must-revalidate',
// This header gets stripped and is only visible to Fastly caches.
// https://docs.fastly.com/en/guides/serving-stale-content#manually-enabling-serve-stale
'surrogate-control': `max-age=${FASTLY_TTL}, stale-if-error=${STALE_TTL}, stale-while-revalidate=${STALE_TTL}`,
// Fastly provides a Soft Purge feature that allows you to mark content as outdated (stale) instead of permanently
// purging and thereby deleting it from Fastly's caches. Objects invalidated with Soft Purge will be treated as
// outdated (stale) while Fastly fetches a new version from origin.
//
// Use of a surrogate key is required for soft purging
// https://docs.fastly.com/en/guides/soft-purges
// https://docs.fastly.com/en/guides/getting-started-with-surrogate-keys
'surrogate-key': 'all-the-things'
})
next()
}