From 8b9f9ec1f097c66ff6062fda9dc0ccb08b8835bd Mon Sep 17 00:00:00 2001 From: Wayde Lyle Date: Fri, 1 May 2020 09:56:52 +0200 Subject: [PATCH 1/6] full render cache and disable prefetch. --- lib/app.js | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/lib/app.js b/lib/app.js index d19e816..7f8a989 100644 --- a/lib/app.js +++ b/lib/app.js @@ -11,6 +11,11 @@ const DEFAULT_OPTIONS = { prodOnly: false, } +const fullRenderCache = new LRU({ + max: 1000, + maxAge: 1000 * 60 * 15, +}) + module.exports = (app, options) => { options = Object.assign({}, DEFAULT_OPTIONS, options) @@ -42,7 +47,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 +125,18 @@ module.exports = (app, options) => { httpCode: 200, }, config.extendContext && config.extendContext(req, res, process)) + 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 +174,7 @@ module.exports = (app, options) => { config.onRender(res, context) } + fullRenderCache.set(req.url, html) res.send(html) }) } From fadf89fb67fc1c797ca6d720664d0a61438ad167 Mon Sep 17 00:00:00 2001 From: Wayde Lyle Date: Fri, 1 May 2020 15:41:44 +0200 Subject: [PATCH 2/6] Do not prefetch. --- lib/app.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/lib/app.js b/lib/app.js index 7f8a989..9ff79d7 100644 --- a/lib/app.js +++ b/lib/app.js @@ -125,6 +125,17 @@ module.exports = (app, options) => { httpCode: 200, }, config.extendContext && config.extendContext(req, res, process)) + fullRenderCache.dispose((url) => { + context.url = url + renderer.renderToString(context, (err, renderedHtml) => { + if (err || context.httpCode === 500) { + return + } else { + fullRenderCache.set(url, renderedHtml) + } + }) + }) + const cachedRender = fullRenderCache.get(req.url) if (cachedRender) { From 58460379c12739963b79bf0c229efb1bfc11ea2d Mon Sep 17 00:00:00 2001 From: Wayde Lyle Date: Tue, 5 May 2020 10:45:02 +0200 Subject: [PATCH 3/6] ssr cache config as a function. --- lib/app.js | 44 ++++++++++++++++++++------------------------ 1 file changed, 20 insertions(+), 24 deletions(-) diff --git a/lib/app.js b/lib/app.js index 9ff79d7..d5c2505 100644 --- a/lib/app.js +++ b/lib/app.js @@ -11,10 +11,13 @@ const DEFAULT_OPTIONS = { prodOnly: false, } -const fullRenderCache = new LRU({ - max: 1000, - maxAge: 1000 * 60 * 15, -}) +let fullRenderCache; +if (config.cachedRenderResponse) { + fullRenderCache = new LRU({ + max: 1000, + maxAge: 1000 * 60 * 15, + }) +} module.exports = (app, options) => { options = Object.assign({}, DEFAULT_OPTIONS, options) @@ -125,27 +128,18 @@ module.exports = (app, options) => { httpCode: 200, }, config.extendContext && config.extendContext(req, res, process)) - fullRenderCache.dispose((url) => { - context.url = url - renderer.renderToString(context, (err, renderedHtml) => { - if (err || context.httpCode === 500) { - return - } else { - fullRenderCache.set(url, renderedHtml) - } - }) - }) - - const cachedRender = fullRenderCache.get(req.url) - - if (cachedRender) { - res.status(context.httpCode) + if (config.cachedRenderResponse && config.cachedRenderResponse(context)) { + const cachedRender = fullRenderCache.get(req.url) - if (config.onRender) { - config.onRender(res, context) + if (cachedRender) { + res.status(context.httpCode) + + if (config.onRender) { + config.onRender(res, context) + } + + return res.send(cachedRender) } - - return res.send(cachedRender) } renderer.renderToString(context, (err, renderedHtml) => { @@ -185,7 +179,9 @@ module.exports = (app, options) => { config.onRender(res, context) } - fullRenderCache.set(req.url, html) + if (config.cachedRenderResponse && config.cachedRenderResponse(context)) { + fullRenderCache.set(req.url, html) + } res.send(html) }) } From 6039567619bc85d3d31965a6f3afbfbbb273553e Mon Sep 17 00:00:00 2001 From: Wayde Lyle Date: Thu, 7 May 2020 14:16:00 +0200 Subject: [PATCH 4/6] Custom cache ttl. --- lib/app.js | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/lib/app.js b/lib/app.js index d5c2505..fc13cba 100644 --- a/lib/app.js +++ b/lib/app.js @@ -11,14 +11,6 @@ const DEFAULT_OPTIONS = { prodOnly: false, } -let fullRenderCache; -if (config.cachedRenderResponse) { - fullRenderCache = new LRU({ - max: 1000, - maxAge: 1000 * 60 * 15, - }) -} - module.exports = (app, options) => { options = Object.assign({}, DEFAULT_OPTIONS, options) @@ -40,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, @@ -128,7 +129,7 @@ module.exports = (app, options) => { httpCode: 200, }, config.extendContext && config.extendContext(req, res, process)) - if (config.cachedRenderResponse && config.cachedRenderResponse(context)) { + if (config.cachedRenderResponse && config.cachedRenderResponse(context) && fullRenderCache.has(req.url)) { const cachedRender = fullRenderCache.get(req.url) if (cachedRender) { @@ -179,9 +180,12 @@ module.exports = (app, options) => { config.onRender(res, context) } - if (config.cachedRenderResponse && config.cachedRenderResponse(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) }) } From 97b9df6ac6176bc0756b750f21035e64f6ea1a28 Mon Sep 17 00:00:00 2001 From: Wayde Lyle Date: Thu, 7 May 2020 14:33:59 +0200 Subject: [PATCH 5/6] Custom render cache documentation. --- docs/guide/configuration.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/docs/guide/configuration.md b/docs/guide/configuration.md index 72bb112..58ad2b3 100644 --- a/docs/guide/configuration.md +++ b/docs/guide/configuration.md @@ -39,6 +39,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 From 543ecc6fa072961a9b236b6f254fe12d4ef19a9e Mon Sep 17 00:00:00 2001 From: Wayde Lyle Date: Thu, 7 May 2020 14:38:21 +0200 Subject: [PATCH 6/6] Add doc to disable all prefetches. --- docs/guide/configuration.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/guide/configuration.md b/docs/guide/configuration.md index 58ad2b3..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: [],