mirror of
https://github.com/BobbyWibowo/lolisafe.git
synced 2025-01-31 07:11:33 +00:00
feat: enable persistent cache for nojs uploader
also slight improvement to NunjucksRenderer class
This commit is contained in:
parent
19eac1d98a
commit
ea30e5dee5
@ -23,21 +23,28 @@ class NunjucksRenderer {
|
|||||||
|
|
||||||
#middleware (req, res, next) {
|
#middleware (req, res, next) {
|
||||||
// Inject render() method into Response on each requests
|
// Inject render() method into Response on each requests
|
||||||
// If usePersistentCache, the rendered template will be cached forever (thus only use for absolutely static pages)
|
res.render = (...args) => this.#render(res, ...args)
|
||||||
res.render = (path, params, usePersistentCache) => this.#render(res, path, params, usePersistentCache)
|
|
||||||
return next()
|
return next()
|
||||||
}
|
}
|
||||||
|
|
||||||
#render (res, path, params, usePersistentCache) {
|
#render (res, path, context, usePersistentCache = false) {
|
||||||
|
// If usePersistentCache is true, the rendered templates will be cached forever,
|
||||||
|
// thus only use for absolutely static pages
|
||||||
|
if (!path) {
|
||||||
|
throw new Error('Missing Nunjucks template name.')
|
||||||
|
}
|
||||||
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
const template = `${path}.njk`
|
const template = `${path}.njk`
|
||||||
|
|
||||||
|
if (usePersistentCache) {
|
||||||
const cached = this.#persistentCaches.get(template)
|
const cached = this.#persistentCaches.get(template)
|
||||||
if (usePersistentCache && cached) {
|
if (cached) {
|
||||||
return resolve(cached)
|
return resolve(cached)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
this.environment.render(template, params, (err, html) => {
|
this.environment.render(template, context, (err, html) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
return reject(err)
|
return reject(err)
|
||||||
}
|
}
|
||||||
|
@ -5,11 +5,9 @@ const utils = require('./../controllers/utilsController')
|
|||||||
const config = require('./../config')
|
const config = require('./../config')
|
||||||
|
|
||||||
routes.get('/nojs', async (req, res) => {
|
routes.get('/nojs', async (req, res) => {
|
||||||
// TODO: Update res.render() to allow bypassing cache on demand,
|
|
||||||
// so that this GET route can instead re-use persistent cache
|
|
||||||
return res.render('nojs', {
|
return res.render('nojs', {
|
||||||
config, utils, versions: utils.versionStrings
|
config, utils, versions: utils.versionStrings
|
||||||
})
|
}, !utils.devmode)
|
||||||
})
|
})
|
||||||
|
|
||||||
// HyperExpress defaults to 250kb
|
// HyperExpress defaults to 250kb
|
||||||
|
Loading…
Reference in New Issue
Block a user