Combine cache control (#33067)
This commit is contained in:
@@ -4,10 +4,9 @@ import Ajv from 'ajv'
|
||||
import addFormats from 'ajv-formats'
|
||||
import { eventSchema, hydroNames } from '../../lib/schema-event.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 noCacheControl = cacheControlFactory(0)
|
||||
const ajv = new Ajv()
|
||||
addFormats(ajv)
|
||||
const OMIT_FIELDS = ['type']
|
||||
|
||||
@@ -5,9 +5,7 @@ import got from 'got'
|
||||
import patterns from '../lib/patterns.js'
|
||||
import isArchivedVersion from '../lib/is-archived-version.js'
|
||||
import { setFastlySurrogateKey, SURROGATE_ENUMS } from './set-fastly-surrogate-key.js'
|
||||
import { cacheControlFactory } from './cache-control.js'
|
||||
|
||||
const cacheControl = cacheControlFactory(60 * 60 * 24 * 365)
|
||||
import { archivedCacheControl } from './cache-control.js'
|
||||
|
||||
// This module handles requests for the CSS and JS assets for
|
||||
// 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
|
||||
// enterprise version URLs that are not assets.
|
||||
cacheControl(res)
|
||||
archivedCacheControl(res)
|
||||
setFastlySurrogateKey(res, SURROGATE_ENUMS.MANUAL)
|
||||
|
||||
return res.send(r.body)
|
||||
|
||||
@@ -13,7 +13,7 @@ import isArchivedVersion from '../lib/is-archived-version.js'
|
||||
import { setFastlySurrogateKey, SURROGATE_ENUMS } from './set-fastly-surrogate-key.js'
|
||||
import got from 'got'
|
||||
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 getRedirect, { splitPathByLanguage } from '../lib/get-redirect.js'
|
||||
|
||||
@@ -39,13 +39,10 @@ const archivedFrontmatterValidURLS = readCompressedJsonFileFallbackLazily(
|
||||
'./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
|
||||
// aggresively cached.
|
||||
const cacheAggressively = (res) => {
|
||||
cacheControl(res)
|
||||
archivedCacheControl(res)
|
||||
|
||||
// This sets a custom Fastly surrogate key so that this response
|
||||
// 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)
|
||||
if (redirectTo) {
|
||||
if (redirectCode === 301) {
|
||||
cacheControl(res)
|
||||
archivedCacheControl(res)
|
||||
} else {
|
||||
noCacheControl(res)
|
||||
}
|
||||
@@ -124,7 +121,7 @@ export default async function archivedEnterpriseVersions(req, res, next) {
|
||||
const newRedirectTo = redirectJson[withoutLanguage]
|
||||
if (newRedirectTo) {
|
||||
if (redirectCode === 301) {
|
||||
cacheControl(res)
|
||||
archivedCacheControl(res)
|
||||
} else {
|
||||
noCacheControl(res)
|
||||
}
|
||||
@@ -137,7 +134,7 @@ export default async function archivedEnterpriseVersions(req, res, next) {
|
||||
req.path.startsWith('/en/') &&
|
||||
versionSatisfiesRange(requestedVersion, `<${firstVersionDeprecatedOnNewSite}`)
|
||||
) {
|
||||
cacheControl(res)
|
||||
archivedCacheControl(res)
|
||||
return res.redirect(redirectCode, req.baseUrl + req.path.replace(/^\/en/, ''))
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import { cacheControlFactory } from './cache-control.js'
|
||||
import { noCacheControl } from './cache-control.js'
|
||||
|
||||
const BUILD_SHA = process.env.BUILD_SHA
|
||||
const noCacheControl = cacheControlFactory(0)
|
||||
|
||||
export default function buildInfo(req, res) {
|
||||
res.type('text/plain')
|
||||
|
||||
@@ -8,13 +8,6 @@
|
||||
// cacheControlYear(res)
|
||||
// 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
|
||||
export function cacheControlFactory(
|
||||
maxAge = 60 * 60,
|
||||
@@ -56,3 +49,9 @@ export function defaultCacheControl(res) {
|
||||
defaultCDNCacheControl(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)
|
||||
|
||||
@@ -1,11 +1,9 @@
|
||||
import path from 'path'
|
||||
|
||||
import { cacheControlFactory } from './cache-control.js'
|
||||
import { defaultCacheControl } from './cache-control.js'
|
||||
|
||||
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.
|
||||
// GitHub Support uses this for internal ZenDesk search functionality.
|
||||
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
|
||||
// in every prod deployment.
|
||||
cacheControl(res)
|
||||
defaultCacheControl(res)
|
||||
|
||||
return res.json(allCategories)
|
||||
}
|
||||
|
||||
@@ -1,6 +1,4 @@
|
||||
import { cacheControlFactory } from './cache-control.js'
|
||||
|
||||
const cacheControl = cacheControlFactory(60 * 60 * 24)
|
||||
import { defaultCacheControl } from './cache-control.js'
|
||||
|
||||
export default function fastHead(req, res, next) {
|
||||
const { context } = req
|
||||
@@ -8,7 +6,7 @@ export default function fastHead(req, res, next) {
|
||||
if (page) {
|
||||
// Since the *presence* is not affected by the request, we can cache
|
||||
// this and allow the CDN to hold on to it.
|
||||
cacheControl(res)
|
||||
defaultCacheControl(res)
|
||||
|
||||
return res.status(200).send('')
|
||||
}
|
||||
|
||||
@@ -1,10 +1,8 @@
|
||||
import express from 'express'
|
||||
import { cacheControlFactory } from './cache-control.js'
|
||||
import { noCacheControl } from './cache-control.js'
|
||||
|
||||
const router = express.Router()
|
||||
|
||||
const noCacheControl = cacheControlFactory(0)
|
||||
|
||||
/**
|
||||
* Returns the healthiness of the service.
|
||||
* This may be used by azure app service (forthcoming) to determine whether this
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
import languages from '../../lib/languages.js'
|
||||
import { cacheControlFactory } from '../cache-control.js'
|
||||
|
||||
const cacheControl = cacheControlFactory(24 * 60 * 60)
|
||||
import { defaultCacheControl } from '../cache-control.js'
|
||||
|
||||
const redirectPatterns = Object.values(languages)
|
||||
.map((language) => language.redirectPatterns || [])
|
||||
@@ -36,7 +34,7 @@ export default function languageCodeRedirects(req, res, next) {
|
||||
// particularly smart or fast.
|
||||
const [code, pattern] = allRedirectPatterns.find(([, pattern]) => pattern.test(req.path))
|
||||
if (code && pattern) {
|
||||
cacheControl(res)
|
||||
defaultCacheControl(res)
|
||||
return res.redirect(301, req.path.replace(pattern, `/${code}`))
|
||||
}
|
||||
return next()
|
||||
|
||||
@@ -1,6 +1,4 @@
|
||||
import { cacheControlFactory } from './cache-control.js'
|
||||
|
||||
const noCacheControl = cacheControlFactory(0)
|
||||
import { noCacheControl } from './cache-control.js'
|
||||
|
||||
export default function remoteIp(req, res) {
|
||||
noCacheControl(res)
|
||||
|
||||
@@ -1,6 +1,4 @@
|
||||
import { cacheControlFactory } from './cache-control.js'
|
||||
|
||||
const cacheControl = cacheControlFactory(60 * 60)
|
||||
import { defaultCacheControl } from './cache-control.js'
|
||||
|
||||
export default function trailingSlashes(req, res, next) {
|
||||
if (req.method === 'GET' || req.method === 'HEAD' || req.method === 'OPTIONS') {
|
||||
@@ -14,7 +12,7 @@ export default function trailingSlashes(req, res, next) {
|
||||
if (split.length) {
|
||||
url += `?${split.join('?')}`
|
||||
}
|
||||
cacheControl(res)
|
||||
defaultCacheControl(res)
|
||||
return res.redirect(301, url)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user