Skip to content

Cache entire rendered response #192

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions docs/guide/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module.exports = {
// Listening host for `serve` command
host: null,
// Specify public file paths to disable resource prefetch hints for
// set to true to disable all prefetches.
shouldNotPrefetch: [],
// Specify public file paths to disable resource preload hints for
shouldNotPreload: [],
Expand All @@ -39,6 +40,28 @@ module.exports = {
lruCacheOptions: {
// See https://ssr.vuejs.org/guide/caching.html
},
// enable the entire rendered html & vuex state to be cached
// DO NOT CACHE IF THE USER IS LOGGED IN
cachedRenderResponse(context) {
if (context.req.headers.cookie.contains('access_token')) {
return false
}
if (context.url === '/') {
// only cache the home page
return true
}
// don't cache anything else
return false
},
// set a custom ttl per route
cachedRenderResponseTtl(context) {
if (context.url === '/') {
// cache for 30 minutes
return 1000 * 60 * 30
}
// cache for 15 minutes
return 1000 * 60 * 15
},
// apply default middleware like compression, serving static files
applyDefaultServer: true,
// Function to extend app context object
Expand Down
31 changes: 30 additions & 1 deletion lib/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ module.exports = (app, options) => {

const lruCacheOptions = config.lruCacheOptions || {}

let fullRenderCache;
if (config.cachedRenderResponse) {
fullRenderCache = new LRU({
max: 1000,
maxAge: 1000 * 60 * 15,
...lruCacheOptions,
})
}

const defaultRendererOptions = {
cache: new LRU({
max: 1000,
Expand All @@ -42,7 +51,7 @@ module.exports = (app, options) => {
inject: false,
directives,
shouldPrefetch: (file, type) => {
if (config.shouldNotPrefetch.indexOf(file) > -1) return false
if (config.shouldNotPrefetch === true || config.shouldNotPrefetch.indexOf(file) > -1) return false
if (type === 'script' || type === 'style') return true
},
shouldPreload: (file, type) => {
Expand Down Expand Up @@ -120,6 +129,20 @@ module.exports = (app, options) => {
httpCode: 200,
}, config.extendContext && config.extendContext(req, res, process))

if (config.cachedRenderResponse && config.cachedRenderResponse(context) && fullRenderCache.has(req.url)) {
const cachedRender = fullRenderCache.get(req.url)

if (cachedRender) {
res.status(context.httpCode)

if (config.onRender) {
config.onRender(res, context)
}

return res.send(cachedRender)
}
}

renderer.renderToString(context, (err, renderedHtml) => {
let html = renderedHtml

Expand Down Expand Up @@ -157,6 +180,12 @@ module.exports = (app, options) => {
config.onRender(res, context)
}

if (config.cachedRenderResponse && config.cachedRenderResponse(context) && config.cachedRenderResponseTtl) {
fullRenderCache.set(req.url, html, config.cachedRenderResponseTtl(context))
} else if (config.cachedRenderResponse && config.cachedRenderResponse(context)) {
fullRenderCache.set(req.url, html)
}

res.send(html)
})
}
Expand Down