Skip to content

Commit ddd1447

Browse files
author
Peter Bengtsson
authored
24h cache all internal redirects without language prefix (#23354)
Part of #1271
1 parent 9e40051 commit ddd1447

3 files changed

Lines changed: 40 additions & 4 deletions

File tree

middleware/cache-control.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,19 @@
88
// cacheControlYear(res)
99
// res.send(body)
1010
//
11+
// Or, if you want to make it definitely not cache:
12+
//
13+
// const noCacheControl = getCacheControl(0) // you can use `false` too
14+
// ...
15+
// noControlYear(res)
16+
// res.send(body)
17+
//
1118
export function cacheControlFactory(maxAge = 60 * 60, public_ = true) {
12-
return (res) => res.set('cache-control', `${public_ ? 'public, ' : ''}max-age=${maxAge}`)
19+
return (res) => {
20+
if (maxAge) {
21+
res.set('cache-control', `${public_ ? 'public, ' : ''}max-age=${maxAge}`)
22+
} else {
23+
res.set('cache-control', 'private, no-store')
24+
}
25+
}
1326
}

middleware/redirects/handle-redirects.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import patterns from '../../lib/patterns.js'
22
import { URL } from 'url'
3-
import languages from '../../lib/languages.js'
3+
import languages, { languageKeys } from '../../lib/languages.js'
4+
import { cacheControlFactory } from '../cache-control.js'
5+
6+
const cacheControl = cacheControlFactory(60 * 60 * 24) // one day
7+
const noCacheControl = cacheControlFactory(0)
8+
9+
const languagePrefixURIRegex = new RegExp(`^/(${languageKeys.join('|')})/`)
410

511
export default function handleRedirects(req, res, next) {
612
// never redirect assets
@@ -19,7 +25,7 @@ export default function handleRedirects(req, res, next) {
1925
language = req.context.userLanguage
2026
}
2127

22-
res.set('cache-control', 'private, no-store')
28+
noCacheControl(res)
2329
return res.redirect(302, `/${language}`)
2430
}
2531

@@ -63,7 +69,11 @@ export default function handleRedirects(req, res, next) {
6369
return next()
6470
}
6571

66-
// do the redirect!
72+
// do the redirect if the from-URL already had a language in it
73+
if (languagePrefixURIRegex.test(req.path)) {
74+
cacheControl(res)
75+
}
76+
6777
return res.redirect(301, redirect)
6878
}
6979

tests/rendering/server.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,8 @@ describe('server', () => {
609609
test('redirects old articles to their English URL', async () => {
610610
const res = await get('/articles/deleting-a-team')
611611
expect(res.statusCode).toBe(301)
612+
// no cache control because a language prefix had to be injected
613+
expect(res.headers['cache-control']).toBeUndefined()
612614
})
613615

614616
test('redirects old articles to their slugified URL', async () => {
@@ -629,12 +631,21 @@ describe('server', () => {
629631
const res = await get('/articles/deleting-a-team')
630632
expect(res.statusCode).toBe(301)
631633
expect(res.headers.location.startsWith('/en/')).toBe(true)
634+
expect(res.headers['cache-control']).toBeUndefined()
635+
})
636+
637+
test('redirects that not only injects /en/ should have cache-control', async () => {
638+
const res = await get('/en/articles/deleting-a-team')
639+
expect(res.statusCode).toBe(301)
640+
expect(res.headers['cache-control']).toContain('public')
641+
expect(res.headers['cache-control']).toMatch(/max-age=\d+/)
632642
})
633643

634644
test('redirects Desktop Classic paths to desktop.github.com', async () => {
635645
const res = await get('/desktop-classic')
636646
expect(res.statusCode).toBe(301)
637647
expect(res.headers.location).toBe('https://desktop.github.com')
648+
expect(res.headers['cache-control']).toBeUndefined()
638649
})
639650

640651
// this oneoff redirect is temporarily disabled because it introduces too much complexity
@@ -646,6 +657,8 @@ describe('server', () => {
646657
expect(res.headers.location).toBe(
647658
'/en/github/managing-subscriptions-and-notifications-on-github'
648659
)
660+
expect(res.headers['cache-control']).toContain('public')
661+
expect(res.headers['cache-control']).toMatch(/max-age=\d+/)
649662
})
650663
})
651664

0 commit comments

Comments
 (0)