1
0
mirror of synced 2025-12-20 10:28:40 -05:00

Combine cache control (#33067)

This commit is contained in:
Kevin Heis
2022-11-29 16:04:34 -08:00
committed by GitHub
parent 9cd9f87df2
commit f161f86ca1
11 changed files with 25 additions and 45 deletions

View File

@@ -4,10 +4,9 @@ import Ajv from 'ajv'
import addFormats from 'ajv-formats' import addFormats from 'ajv-formats'
import { eventSchema, hydroNames } from '../../lib/schema-event.js' import { eventSchema, hydroNames } from '../../lib/schema-event.js'
import catchMiddlewareError from '../catch-middleware-error.js' import catchMiddlewareError from '../catch-middleware-error.js'
import { cacheControlFactory } from '../cache-control.js' import { noCacheControl } from '../cache-control.js'
const router = express.Router() const router = express.Router()
const noCacheControl = cacheControlFactory(0)
const ajv = new Ajv() const ajv = new Ajv()
addFormats(ajv) addFormats(ajv)
const OMIT_FIELDS = ['type'] const OMIT_FIELDS = ['type']

View File

@@ -5,9 +5,7 @@ import got from 'got'
import patterns from '../lib/patterns.js' import patterns from '../lib/patterns.js'
import isArchivedVersion from '../lib/is-archived-version.js' import isArchivedVersion from '../lib/is-archived-version.js'
import { setFastlySurrogateKey, SURROGATE_ENUMS } from './set-fastly-surrogate-key.js' import { setFastlySurrogateKey, SURROGATE_ENUMS } from './set-fastly-surrogate-key.js'
import { cacheControlFactory } from './cache-control.js' import { archivedCacheControl } from './cache-control.js'
const cacheControl = cacheControlFactory(60 * 60 * 24 * 365)
// This module handles requests for the CSS and JS assets for // This module handles requests for the CSS and JS assets for
// deprecated GitHub Enterprise versions by routing them to static content in // deprecated GitHub Enterprise versions by routing them to static content in
@@ -67,7 +65,7 @@ export default async function archivedEnterpriseVersionsAssets(req, res, next) {
// This cache configuration should match what we do for archived // This cache configuration should match what we do for archived
// enterprise version URLs that are not assets. // enterprise version URLs that are not assets.
cacheControl(res) archivedCacheControl(res)
setFastlySurrogateKey(res, SURROGATE_ENUMS.MANUAL) setFastlySurrogateKey(res, SURROGATE_ENUMS.MANUAL)
return res.send(r.body) return res.send(r.body)

View File

@@ -13,7 +13,7 @@ import isArchivedVersion from '../lib/is-archived-version.js'
import { setFastlySurrogateKey, SURROGATE_ENUMS } from './set-fastly-surrogate-key.js' import { setFastlySurrogateKey, SURROGATE_ENUMS } from './set-fastly-surrogate-key.js'
import got from 'got' import got from 'got'
import { readCompressedJsonFileFallbackLazily } from '../lib/read-json-file.js' import { readCompressedJsonFileFallbackLazily } from '../lib/read-json-file.js'
import { cacheControlFactory } from './cache-control.js' import { archivedCacheControl, noCacheControl } from './cache-control.js'
import { pathLanguagePrefixed, languagePrefixPathRegex } from '../lib/languages.js' import { pathLanguagePrefixed, languagePrefixPathRegex } from '../lib/languages.js'
import getRedirect, { splitPathByLanguage } from '../lib/get-redirect.js' import getRedirect, { splitPathByLanguage } from '../lib/get-redirect.js'
@@ -39,13 +39,10 @@ const archivedFrontmatterValidURLS = readCompressedJsonFileFallbackLazily(
'./lib/redirects/static/archived-frontmatter-valid-urls.json' './lib/redirects/static/archived-frontmatter-valid-urls.json'
) )
const cacheControl = cacheControlFactory(60 * 60 * 24 * 365)
const noCacheControl = cacheControlFactory(0)
// Combine all the things you need to make sure the response is // Combine all the things you need to make sure the response is
// aggresively cached. // aggresively cached.
const cacheAggressively = (res) => { const cacheAggressively = (res) => {
cacheControl(res) archivedCacheControl(res)
// This sets a custom Fastly surrogate key so that this response // This sets a custom Fastly surrogate key so that this response
// won't get updated in every deployment. // won't get updated in every deployment.
@@ -104,7 +101,7 @@ export default async function archivedEnterpriseVersions(req, res, next) {
const redirectTo = getRedirect(req.path, req.context) const redirectTo = getRedirect(req.path, req.context)
if (redirectTo) { if (redirectTo) {
if (redirectCode === 301) { if (redirectCode === 301) {
cacheControl(res) archivedCacheControl(res)
} else { } else {
noCacheControl(res) noCacheControl(res)
} }
@@ -124,7 +121,7 @@ export default async function archivedEnterpriseVersions(req, res, next) {
const newRedirectTo = redirectJson[withoutLanguage] const newRedirectTo = redirectJson[withoutLanguage]
if (newRedirectTo) { if (newRedirectTo) {
if (redirectCode === 301) { if (redirectCode === 301) {
cacheControl(res) archivedCacheControl(res)
} else { } else {
noCacheControl(res) noCacheControl(res)
} }
@@ -137,7 +134,7 @@ export default async function archivedEnterpriseVersions(req, res, next) {
req.path.startsWith('/en/') && req.path.startsWith('/en/') &&
versionSatisfiesRange(requestedVersion, `<${firstVersionDeprecatedOnNewSite}`) versionSatisfiesRange(requestedVersion, `<${firstVersionDeprecatedOnNewSite}`)
) { ) {
cacheControl(res) archivedCacheControl(res)
return res.redirect(redirectCode, req.baseUrl + req.path.replace(/^\/en/, '')) return res.redirect(redirectCode, req.baseUrl + req.path.replace(/^\/en/, ''))
} }

View File

@@ -1,7 +1,6 @@
import { cacheControlFactory } from './cache-control.js' import { noCacheControl } from './cache-control.js'
const BUILD_SHA = process.env.BUILD_SHA const BUILD_SHA = process.env.BUILD_SHA
const noCacheControl = cacheControlFactory(0)
export default function buildInfo(req, res) { export default function buildInfo(req, res) {
res.type('text/plain') res.type('text/plain')

View File

@@ -8,13 +8,6 @@
// cacheControlYear(res) // cacheControlYear(res)
// res.send(body) // res.send(body)
// //
// Or, if you want to make it definitely not cache:
//
// const noCacheControl = getCacheControl(0) // you can use `false` too
// ...
// noControlYear(res)
// res.send(body)
//
// Max age is in seconds // Max age is in seconds
export function cacheControlFactory( export function cacheControlFactory(
maxAge = 60 * 60, maxAge = 60 * 60,
@@ -56,3 +49,9 @@ export function defaultCacheControl(res) {
defaultCDNCacheControl(res) defaultCDNCacheControl(res)
defaultBrowserCacheControl(res) defaultBrowserCacheControl(res)
} }
// If you do not want caching
export const noCacheControl = cacheControlFactory(0)
// Long caching for archived pages and assets
export const archivedCacheControl = cacheControlFactory(60 * 60 * 24 * 365)

View File

@@ -1,11 +1,9 @@
import path from 'path' import path from 'path'
import { cacheControlFactory } from './cache-control.js' import { defaultCacheControl } from './cache-control.js'
const renderOpts = { textOnly: true, encodeEntities: true } const renderOpts = { textOnly: true, encodeEntities: true }
const cacheControl = cacheControlFactory(60 * 60 * 24)
// This middleware exposes a list of all categories and child articles at /categories.json. // This middleware exposes a list of all categories and child articles at /categories.json.
// GitHub Support uses this for internal ZenDesk search functionality. // GitHub Support uses this for internal ZenDesk search functionality.
export default async function categoriesForSupport(req, res) { export default async function categoriesForSupport(req, res) {
@@ -34,7 +32,7 @@ export default async function categoriesForSupport(req, res) {
// Cache somewhat aggressively but note that it will be soft-purged // Cache somewhat aggressively but note that it will be soft-purged
// in every prod deployment. // in every prod deployment.
cacheControl(res) defaultCacheControl(res)
return res.json(allCategories) return res.json(allCategories)
} }

View File

@@ -1,6 +1,4 @@
import { cacheControlFactory } from './cache-control.js' import { defaultCacheControl } from './cache-control.js'
const cacheControl = cacheControlFactory(60 * 60 * 24)
export default function fastHead(req, res, next) { export default function fastHead(req, res, next) {
const { context } = req const { context } = req
@@ -8,7 +6,7 @@ export default function fastHead(req, res, next) {
if (page) { if (page) {
// Since the *presence* is not affected by the request, we can cache // Since the *presence* is not affected by the request, we can cache
// this and allow the CDN to hold on to it. // this and allow the CDN to hold on to it.
cacheControl(res) defaultCacheControl(res)
return res.status(200).send('') return res.status(200).send('')
} }

View File

@@ -1,10 +1,8 @@
import express from 'express' import express from 'express'
import { cacheControlFactory } from './cache-control.js' import { noCacheControl } from './cache-control.js'
const router = express.Router() const router = express.Router()
const noCacheControl = cacheControlFactory(0)
/** /**
* Returns the healthiness of the service. * Returns the healthiness of the service.
* This may be used by azure app service (forthcoming) to determine whether this * This may be used by azure app service (forthcoming) to determine whether this

View File

@@ -1,7 +1,5 @@
import languages from '../../lib/languages.js' import languages from '../../lib/languages.js'
import { cacheControlFactory } from '../cache-control.js' import { defaultCacheControl } from '../cache-control.js'
const cacheControl = cacheControlFactory(24 * 60 * 60)
const redirectPatterns = Object.values(languages) const redirectPatterns = Object.values(languages)
.map((language) => language.redirectPatterns || []) .map((language) => language.redirectPatterns || [])
@@ -36,7 +34,7 @@ export default function languageCodeRedirects(req, res, next) {
// particularly smart or fast. // particularly smart or fast.
const [code, pattern] = allRedirectPatterns.find(([, pattern]) => pattern.test(req.path)) const [code, pattern] = allRedirectPatterns.find(([, pattern]) => pattern.test(req.path))
if (code && pattern) { if (code && pattern) {
cacheControl(res) defaultCacheControl(res)
return res.redirect(301, req.path.replace(pattern, `/${code}`)) return res.redirect(301, req.path.replace(pattern, `/${code}`))
} }
return next() return next()

View File

@@ -1,6 +1,4 @@
import { cacheControlFactory } from './cache-control.js' import { noCacheControl } from './cache-control.js'
const noCacheControl = cacheControlFactory(0)
export default function remoteIp(req, res) { export default function remoteIp(req, res) {
noCacheControl(res) noCacheControl(res)

View File

@@ -1,6 +1,4 @@
import { cacheControlFactory } from './cache-control.js' import { defaultCacheControl } from './cache-control.js'
const cacheControl = cacheControlFactory(60 * 60)
export default function trailingSlashes(req, res, next) { export default function trailingSlashes(req, res, next) {
if (req.method === 'GET' || req.method === 'HEAD' || req.method === 'OPTIONS') { if (req.method === 'GET' || req.method === 'HEAD' || req.method === 'OPTIONS') {
@@ -14,7 +12,7 @@ export default function trailingSlashes(req, res, next) {
if (split.length) { if (split.length) {
url += `?${split.join('?')}` url += `?${split.join('?')}`
} }
cacheControl(res) defaultCacheControl(res)
return res.redirect(301, url) return res.redirect(301, url)
} }
} }