Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a16aeac

Browse files
authored
Prepare render-page for re-enabling HTML caching (#29473)
* Prepare render-page for re-enabling HTML caching * Prepare render-page for re-enabling HTML caching * Prepare render-page for re-enabling HTML caching * Update healthz.js * Pre calculate cache control directives * Update render-page.js
1 parent 4a03924 commit a16aeac

6 files changed

Lines changed: 26 additions & 27 deletions

File tree

middleware/cache-control.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,18 @@
1515
// noControlYear(res)
1616
// res.send(body)
1717
//
18+
// Max age is in seconds
1819
export function cacheControlFactory(maxAge = 60 * 60, { public_ = true, immutable = false } = {}) {
20+
const directives = [
21+
maxAge && public_ && 'public',
22+
maxAge && `max-age=${maxAge}`,
23+
maxAge && immutable && 'immutable',
24+
!maxAge && 'private',
25+
!maxAge && 'no-store',
26+
]
27+
.filter(Boolean)
28+
.join(', ')
1929
return (res) => {
20-
const directives = []
21-
if (maxAge) {
22-
if (public_) directives.push('public')
23-
directives.push(`max-age=${maxAge}`)
24-
if (immutable) directives.push('immutable')
25-
} else {
26-
directives.push('private')
27-
directives.push('no-store')
28-
}
29-
res.set('cache-control', directives.join(', '))
30+
res.set('cache-control', directives)
3031
}
3132
}

middleware/healthz.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
import express from 'express'
2+
import { cacheControlFactory } from './cache-control.js'
23

34
const router = express.Router()
45

6+
const noCacheControl = cacheControlFactory(0)
7+
58
/**
69
* Returns the healthiness of the service.
710
* This may be used by azure app service (forthcoming) to determine whether this
811
* instance remains in the pool to handle requests
912
* For example: if we have a failing database connection we may return a 500 status here.
1013
*/
1114
router.get('/', function healthz(req, res, next) {
12-
res.set({
13-
'surrogate-control': 'private, no-store',
14-
'cache-control': 'private, no-store',
15-
})
15+
noCacheControl(res)
1616

1717
res.sendStatus(200)
1818
})

middleware/index.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import cookieParser from './cookie-parser.js'
1414
import csrf from './csrf.js'
1515
import handleCsrfErrors from './handle-csrf-errors.js'
1616
import { setDefaultFastlySurrogateKey } from './set-fastly-surrogate-key.js'
17-
import setFastlyCacheHeaders from './set-fastly-cache-headers.js'
1817
import reqUtils from './req-utils.js'
1918
import recordRedirect from './record-redirect.js'
2019
import handleErrors from './handle-errors.js'
@@ -303,9 +302,6 @@ export default function (app) {
303302
app.use('/fastly-cache-test', fastlyCacheTest)
304303
}
305304

306-
// *** Headers for pages only ***
307-
app.use(setFastlyCacheHeaders)
308-
309305
// handle serving NextJS bundled code (/_next/*)
310306
app.use(instrument(next, './next'))
311307

middleware/render-page.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ import statsd from '../lib/statsd.js'
77
import { allVersions } from '../lib/all-versions.js'
88
import { isConnectionDropped } from './halt-on-dropped-connection.js'
99
import { nextApp, nextHandleRequest } from './next.js'
10+
import { cacheControlFactory } from './cache-control.js'
11+
12+
const noCacheControl = cacheControlFactory(0)
13+
14+
const htmlCacheControl = cacheControlFactory(0)
15+
// We'll start with no cache, the increase to one minute (60),
16+
// then five minutes (60 * 5), finally ten minutes (60 * 10)
1017

1118
async function buildRenderedPage(req) {
1219
const { context } = req
@@ -54,6 +61,7 @@ export default async function renderPage(req, res, next) {
5461

5562
// render a 404 page
5663
if (!page) {
64+
noCacheControl(res)
5765
if (process.env.NODE_ENV !== 'test' && context.redirectNotFound) {
5866
console.error(
5967
`\nTried to redirect to ${context.redirectNotFound}, but that page was not found.\n`
@@ -64,9 +72,12 @@ export default async function renderPage(req, res, next) {
6472

6573
// Just finish fast without all the details like Content-Length
6674
if (req.method === 'HEAD') {
75+
noCacheControl(res)
6776
return res.status(200).send('')
6877
}
6978

79+
htmlCacheControl(res)
80+
7081
// Updating the Last-Modified header for substantive changes on a page for engineering
7182
// Docs Engineering Issue #945
7283
if (page.effectiveDate) {

middleware/set-fastly-cache-headers.js

Lines changed: 0 additions & 8 deletions
This file was deleted.

tests/rendering/server.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,6 @@ describe('server', () => {
132132
test('sets Fastly cache control headers to bypass pages', async () => {
133133
const res = await get('/en')
134134
expect(res.headers['cache-control']).toBe('private, no-store')
135-
expect(res.headers['surrogate-control']).toBe('private, no-store')
136135
expect(res.headers['surrogate-key']).toBe(SURROGATE_ENUMS.DEFAULT)
137136
})
138137

0 commit comments

Comments
 (0)