1
0
mirror of synced 2026-01-15 06:01:36 -05:00

Merge pull request #13024 from github/repo-sync

repo sync
This commit is contained in:
Octomerger Bot
2021-12-15 13:55:02 -06:00
committed by GitHub

View File

@@ -5,6 +5,8 @@ import Page from '../lib/page.js'
import { isConnectionDropped } from './halt-on-dropped-connection.js'
import { nextApp, nextHandleRequest } from './next.js'
const pageCache = new Map()
export default async function renderPage(req, res, next) {
if (req.path.startsWith('/storybook')) {
return nextHandleRequest(req, res)
@@ -29,6 +31,45 @@ export default async function renderPage(req, res, next) {
// Is the request for JSON debugging info?
const isRequestingJsonForDebugging = 'json' in req.query && process.env.NODE_ENV !== 'production'
// ****** temporary caching measure for holiday spam prevention *********
const isApiPage =
req.context.currentPathWithoutLanguage.includes('rest/reference') ||
req.context.currentPathWithoutLanguage.includes('graphql/reference')
// Serve from the cache if possible
const isCacheable =
// Skip for CI
!process.env.CI &&
// Skip for tests
process.env.NODE_ENV !== 'test' &&
// Skip for HTTP methods other than GET
req.method === 'GET' &&
// Skip for JSON debugging info requests
!isRequestingJsonForDebugging &&
// Only API pages
isApiPage
// don't cache query strings
const originalUrl = req.originalUrl.split('?')[0]
if (isCacheable) {
// Stop processing if the connection was already dropped
if (isConnectionDropped(req, res)) return
const cachedHtml = pageCache.get(originalUrl)
if (cachedHtml) {
// Stop processing if the connection was already dropped
if (isConnectionDropped(req, res)) return
console.log(`Serving from cached version of ${originalUrl}`)
// statsd.increment('page.sent_from_cache')
return res.send(cachedHtml)
}
}
// ****** [end] temporary caching measure for holiday spam prevention *********
// add page context
const context = Object.assign({}, req.context, { page })
@@ -104,5 +145,12 @@ export default async function renderPage(req, res, next) {
// Hand rendering over to NextJS
req.context.renderedPage = context.renderedPage
req.context.miniTocItems = context.miniTocItems
// ****** temporary caching measure for holiday spam prevention *********
if (isCacheable) {
pageCache.set(originalUrl, req.context.renderedPage)
}
// ****** [end] temporary caching measure for holiday spam prevention *********
return nextHandleRequest(req, res)
}