From 525d7daa7e93d2bb466220fc118b5a1741446589 Mon Sep 17 00:00:00 2001 From: Brad Oyler Date: Sat, 26 Aug 2017 18:02:28 -0400 Subject: [PATCH 1/2] use route-cache middleware for microCache --- package.json | 1 + server.js | 32 +++++--------------------------- 2 files changed, 6 insertions(+), 27 deletions(-) diff --git a/package.json b/package.json index 2756e2754..08935247d 100644 --- a/package.json +++ b/package.json @@ -22,6 +22,7 @@ "extract-text-webpack-plugin": "^2.1.0", "firebase": "^3.7.2", "lru-cache": "^4.0.2", + "route-cache": "0.4.2", "serve-favicon": "^2.4.1", "vue": "^2.4.1", "vue-router": "^2.7.0", diff --git a/server.js b/server.js index 1b985b5ef..96215ba41 100644 --- a/server.js +++ b/server.js @@ -4,6 +4,7 @@ const LRU = require('lru-cache') const express = require('express') const favicon = require('serve-favicon') const compression = require('compression') +const microcache = require('route-cache') const resolve = file => path.resolve(__dirname, file) const { createBundleRenderer } = require('vue-server-renderer') @@ -67,16 +68,7 @@ app.use('/service-worker.js', serve('./dist/service-worker.js')) // 1-second microcache. // https://www.nginx.com/blog/benefits-of-microcaching-nginx/ -const microCache = LRU({ - max: 100, - maxAge: 1000 -}) - -// since this app has no user-specific content, every page is micro-cacheable. -// if your app involves user-specific content, you need to implement custom -// logic to determine whether a request is cacheable based on its url and -// headers. -const isCacheable = req => useMicroCache +app.use(microcache.cacheSeconds(1, () => useMicroCache)) function render (req, res) { const s = Date.now() @@ -88,26 +80,15 @@ function render (req, res) { if (err.url) { res.redirect(err.url) } else if(err.code === 404) { - res.status(404).end('404 | Page Not Found') + res.status(404).send('404 | Page Not Found') } else { // Render Error Page or Redirect - res.status(500).end('500 | Internal Server Error') + res.status(500).send('500 | Internal Server Error') console.error(`error during render : ${req.url}`) console.error(err.stack) } } - const cacheable = isCacheable(req) - if (cacheable) { - const hit = microCache.get(req.url) - if (hit) { - if (!isProd) { - console.log(`cache hit!`) - } - return res.end(hit) - } - } - const context = { title: 'Vue HN 2.0', // default title url: req.url @@ -116,10 +97,7 @@ function render (req, res) { if (err) { return handleError(err) } - res.end(html) - if (cacheable) { - microCache.set(req.url, html) - } + res.send(html) if (!isProd) { console.log(`whole request: ${Date.now() - s}ms`) } From f986547f69666e819ee998f29992c74af39fe424 Mon Sep 17 00:00:00 2001 From: Brad Oyler Date: Fri, 1 Sep 2017 17:19:10 -0400 Subject: [PATCH 2/2] add back comment block --- server.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/server.js b/server.js index 96215ba41..898ff90bf 100644 --- a/server.js +++ b/server.js @@ -66,6 +66,10 @@ app.use('/public', serve('./public', true)) app.use('/manifest.json', serve('./manifest.json', true)) app.use('/service-worker.js', serve('./dist/service-worker.js')) +// since this app has no user-specific content, every page is micro-cacheable. +// if your app involves user-specific content, you need to implement custom +// logic to determine whether a request is cacheable based on its url and +// headers. // 1-second microcache. // https://www.nginx.com/blog/benefits-of-microcaching-nginx/ app.use(microcache.cacheSeconds(1, () => useMicroCache))