mirror of
https://github.com/BobbyWibowo/lolisafe.git
synced 2025-01-18 17:21: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) {
|
||||
// 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 = (path, params, usePersistentCache) => this.#render(res, path, params, usePersistentCache)
|
||||
res.render = (...args) => this.#render(res, ...args)
|
||||
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) => {
|
||||
const template = `${path}.njk`
|
||||
|
||||
const cached = this.#persistentCaches.get(template)
|
||||
if (usePersistentCache && cached) {
|
||||
return resolve(cached)
|
||||
if (usePersistentCache) {
|
||||
const cached = this.#persistentCaches.get(template)
|
||||
if (cached) {
|
||||
return resolve(cached)
|
||||
}
|
||||
}
|
||||
|
||||
this.environment.render(template, params, (err, html) => {
|
||||
this.environment.render(template, context, (err, html) => {
|
||||
if (err) {
|
||||
return reject(err)
|
||||
}
|
||||
|
@ -5,11 +5,9 @@ const utils = require('./../controllers/utilsController')
|
||||
const config = require('./../config')
|
||||
|
||||
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', {
|
||||
config, utils, versions: utils.versionStrings
|
||||
})
|
||||
}, !utils.devmode)
|
||||
})
|
||||
|
||||
// HyperExpress defaults to 250kb
|
||||
|
Loading…
Reference in New Issue
Block a user