Skip to content

Commit 127fab0

Browse files
authored
Branch was updated using the 'autoupdate branch' Actions workflow.
2 parents 54190f2 + 29be6be commit 127fab0

2 files changed

Lines changed: 17 additions & 5 deletions

File tree

middleware/rate-limit.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,22 @@ const Redis = require('ioredis')
55
const isProduction = process.env.NODE_ENV === 'production'
66
const { REDIS_URL } = process.env
77
const rateLimitDatabaseNumber = 0
8+
const EXPIRES_IN_AS_SECONDS = 60
89

910
module.exports = rateLimit({
1011
// 1 minute (or practically unlimited outside of production)
11-
windowMs: isProduction ? (60 * 1000) : 1,
12+
windowMs: isProduction ? (EXPIRES_IN_AS_SECONDS * 1000) : 1, // Non-Redis configuration in `ms`. Used as a fallback when Redis is not working or active.
1213
// limit each IP to X requests per windowMs
1314
max: 250,
1415
// Don't rate limit requests for 200s and redirects
1516
// Or anything with a status code less than 400
1617
skipSuccessfulRequests: true,
1718
// When available, use Redis
18-
store: REDIS_URL && new RedisStore({ client: new Redis(REDIS_URL, { db: rateLimitDatabaseNumber }) })
19+
store: REDIS_URL && new RedisStore({
20+
client: new Redis(REDIS_URL, {
21+
db: rateLimitDatabaseNumber
22+
}),
23+
// 1 minute (or practically unlimited outside of production)
24+
expiry: isProduction ? EXPIRES_IN_AS_SECONDS : 1 // Redis configuration in `s`
25+
})
1926
})

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)