(
surrogateKey,
{
purgeTwice = true,
delayBeforeFirstPurge = DELAY_BEFORE_FIRST_PURGE,
delayBeforeSecondPurge = DELAY_BEFORE_SECOND_PURGE,
} = {}
)
| 28 | } |
| 29 | |
| 30 | export default async function purgeEdgeCache( |
| 31 | surrogateKey, |
| 32 | { |
| 33 | purgeTwice = true, |
| 34 | delayBeforeFirstPurge = DELAY_BEFORE_FIRST_PURGE, |
| 35 | delayBeforeSecondPurge = DELAY_BEFORE_SECOND_PURGE, |
| 36 | } = {} |
| 37 | ) { |
| 38 | if (!surrogateKey) { |
| 39 | throw new Error('No key set and/or no FASTLY_SURROGATE_KEY env var set') |
| 40 | } |
| 41 | console.log(`Fastly purgeEdgeCache initialized for: '${surrogateKey}'`) |
| 42 | |
| 43 | const { FASTLY_TOKEN, FASTLY_SERVICE_ID } = process.env |
| 44 | if (!FASTLY_TOKEN || !FASTLY_SERVICE_ID) { |
| 45 | throw new Error('Fastly env vars not detected; skipping purgeEdgeCache step') |
| 46 | } |
| 47 | |
| 48 | const purgingParams = { |
| 49 | apiToken: FASTLY_TOKEN, |
| 50 | serviceId: FASTLY_SERVICE_ID, |
| 51 | surrogateKey, |
| 52 | } |
| 53 | |
| 54 | // Give the app some extra time to wake up before the thundering herd of |
| 55 | // Fastly requests. |
| 56 | if (delayBeforeFirstPurge) { |
| 57 | console.log('Waiting extra time to prevent a Thundering Herd problem...') |
| 58 | await sleep(delayBeforeFirstPurge) |
| 59 | } |
| 60 | |
| 61 | console.log('Attempting first Fastly purge...') |
| 62 | const firstPurge = await purgeFastlyBySurrogateKey(purgingParams) |
| 63 | console.log('First Fastly purge result:', firstPurge.body || firstPurge) |
| 64 | |
| 65 | // Evidence has shown that it's necessary to purge twice to ensure all |
| 66 | // customers see fresh content. |
| 67 | if (purgeTwice) { |
| 68 | console.log('Waiting to purge a second time...') |
| 69 | await sleep(delayBeforeSecondPurge) |
| 70 | |
| 71 | console.log('Attempting second Fastly purge...') |
| 72 | const secondPurge = await purgeFastlyBySurrogateKey(purgingParams) |
| 73 | console.log('Second Fastly purge result:', secondPurge.body || secondPurge) |
| 74 | } |
| 75 | } |
nothing calls this directly
no test coverage detected