99import patterns from '../lib/patterns.js'
1010import versionSatisfiesRange from '../lib/version-satisfies-range.js'
1111import isArchivedVersion from '../lib/is-archived-version.js'
12+ import { setFastlySurrogateKey , SURROGATE_ENUMS } from './set-fastly-surrogate-key.js'
1213import got from 'got'
1314import { readCompressedJsonFileFallback } from '../lib/read-json-file.js'
1415import { cacheControlFactory } from './cache-control.js'
@@ -55,6 +56,25 @@ const archivedFrontmatterFallbacks = readJsonFileLazily(
5556
5657const cacheControl = cacheControlFactory ( 60 * 60 * 24 * 365 )
5758
59+ // Combine all the things you need to make sure the response is
60+ // aggresively cached.
61+ const cacheAggressively = ( res ) => {
62+ cacheControl ( res )
63+
64+ // This sets a custom Fastly surrogate key so that this response
65+ // won't get updated in every deployment.
66+ // Essentially, this sets a surrogate key such that Fastly
67+ // doesn't do soft-purges on these responses on every
68+ // automated deployment.
69+ setFastlySurrogateKey ( res , SURROGATE_ENUMS . MANUAL )
70+
71+ // Because this middleware has (quite possibly) been executed before
72+ // the CSRF middleware, that would have set a cookie. Remove that.
73+ // The reason for removing the 'Set-Cookie' header is because
74+ // otherwise Fastly won't cache it.
75+ res . removeHeader ( 'set-cookie' )
76+ }
77+
5878// The way `got` does retries:
5979//
6080// sleep = 1000 * Math.pow(2, retry - 1) + Math.random() * 100
@@ -119,7 +139,7 @@ export default async function archivedEnterpriseVersions(req, res, next) {
119139 // and memoized so calling it is cheap.
120140 const redirect = archivedRedirects ( ) [ req . path ]
121141 if ( redirect && redirect !== req . path ) {
122- cacheControl ( res )
142+ cacheAggressively ( res )
123143 return res . redirect ( redirectCode , redirect )
124144 }
125145 }
@@ -138,7 +158,7 @@ export default async function archivedEnterpriseVersions(req, res, next) {
138158 // make redirects found via redirects.json redirect with a 301
139159 if ( redirectJson [ req . path ] ) {
140160 res . set ( 'x-robots-tag' , 'noindex' )
141- cacheControl ( res )
161+ cacheAggressively ( res )
142162 return res . redirect ( redirectCode , redirectJson [ req . path ] )
143163 }
144164 }
@@ -160,11 +180,14 @@ export default async function archivedEnterpriseVersions(req, res, next) {
160180 // make stubbed redirect files (which exist in versions <2.13) redirect with a 301
161181 const staticRedirect = r . body . match ( patterns . staticRedirect )
162182 if ( staticRedirect ) {
163- cacheControl ( res )
183+ cacheAggressively ( res )
164184 return res . redirect ( redirectCode , staticRedirect [ 1 ] )
165185 }
166186
167187 res . set ( 'content-type' , r . headers [ 'content-type' ] )
188+
189+ cacheAggressively ( res )
190+
168191 return res . send ( r . body )
169192 }
170193
@@ -181,7 +204,7 @@ export default async function archivedEnterpriseVersions(req, res, next) {
181204 `fallback:${ fallbackRedirect } ` ,
182205 ] ) ( )
183206 if ( r . statusCode === 200 ) {
184- cacheControl ( res )
207+ cacheAggressively ( res )
185208 return res . redirect ( redirectCode , fallbackRedirect )
186209 }
187210 }
0 commit comments