Skip to content

Commit 16bb1aa

Browse files
authored
repo sync
2 parents 67a3699 + 73559d9 commit 16bb1aa

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

middleware/render-page.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,19 @@ const layouts = require('../lib/layouts')
66
const getMiniTocItems = require('../lib/get-mini-toc-items')
77
const Page = require('../lib/page')
88

9+
// We've got lots of memory, let's use it
10+
// We can eventually throw this into redis
11+
const pageCache = {}
12+
913
module.exports = async function renderPage (req, res, next) {
1014
const page = req.context.page
15+
const originalUrl = req.originalUrl
16+
17+
// Serve from the cache if possible
18+
if (req.method === 'GET' && pageCache[originalUrl]) {
19+
console.log(`Serving from cached version of ${originalUrl}`)
20+
return res.send(pageCache[originalUrl])
21+
}
1122

1223
// render a 404 page
1324
if (!page) {
@@ -74,5 +85,11 @@ module.exports = async function renderPage (req, res, next) {
7485

7586
const output = await liquid.parseAndRender(layout, context)
7687

77-
res.send(output)
88+
// Save output to cache for the next time around
89+
if (req.method === 'GET') {
90+
pageCache[originalUrl] = output
91+
}
92+
93+
// send response
94+
return res.send(output)
7895
}

0 commit comments

Comments
 (0)