diff --git a/docs/guide/configuration.md b/docs/guide/configuration.md index 72bb112..2fef546 100644 --- a/docs/guide/configuration.md +++ b/docs/guide/configuration.md @@ -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: [], @@ -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 diff --git a/lib/app.js b/lib/app.js index d19e816..fc13cba 100644 --- a/lib/app.js +++ b/lib/app.js @@ -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, @@ -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) => { @@ -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 @@ -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) }) }