Skip to content

Commit 9171d17

Browse files
authored
Ignore query strings and fragment identifiers for Redis cache keys (#17205)
* Ignore query strings and fragment identifiers for Redis cache keys * Don't serve cached responses if developer is requesting JSON debugging info
1 parent 4e4c966 commit 9171d17

1 file changed

Lines changed: 8 additions & 3 deletions

File tree

middleware/render-page.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,17 @@ const pageCache = new RedisAccessor({
2020

2121
module.exports = async function renderPage (req, res, next) {
2222
const page = req.context.page
23-
const originalUrl = req.originalUrl
23+
24+
// Remove any query string (?...) and/or fragment identifier (#...)
25+
const originalUrl = new URL(req.originalUrl, 'https://docs.github.com').pathname
2426

2527
// Serve from the cache if possible (skip during tests)
2628
const isCacheable = !process.env.CI && process.env.NODE_ENV !== 'test' && req.method === 'GET'
2729

28-
if (isCacheable) {
30+
// Is the request for JSON debugging info?
31+
const isRequestingJsonForDebugging = 'json' in req.query && process.env.NODE_ENV !== 'production'
32+
33+
if (isCacheable && !isRequestingJsonForDebugging) {
2934
const cachedHtml = await pageCache.get(originalUrl)
3035
if (cachedHtml) {
3136
console.log(`Serving from cached version of ${originalUrl}`)
@@ -76,7 +81,7 @@ module.exports = async function renderPage (req, res, next) {
7681
}
7782

7883
// `?json` query param for debugging request context
79-
if ('json' in req.query && process.env.NODE_ENV !== 'production') {
84+
if (isRequestingJsonForDebugging) {
8085
if (req.query.json.length > 1) {
8186
// deep reference: ?json=page.permalinks
8287
return res.json(get(context, req.query.json))

0 commit comments

Comments
 (0)