From 2d1f5353143cd30e88e079a8e10b97b71bf4e4a0 Mon Sep 17 00:00:00 2001 From: Nolan Lawson Date: Fri, 7 Sep 2018 16:46:40 -0700 Subject: [PATCH 1/2] use consistent cache-control:max-age=600 for HTML pages --- src/middleware.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/middleware.ts b/src/middleware.ts index bab65364f..d86a77aee 100644 --- a/src/middleware.ts +++ b/src/middleware.ts @@ -136,7 +136,7 @@ export default function middleware(opts: { fs.existsSync(path.join(output, 'index.html')) && serve({ pathname: '/index.html', - cache_control: 'max-age=600' + cache_control: dev() ? 'no-cache' : 'max-age=600' }), fs.existsSync(path.join(output, 'service-worker.js')) && serve({ @@ -532,6 +532,7 @@ function get_page_handler( .replace('%sapper.styles%', () => styles); res.statusCode = status; + res.setHeader('Cache-Control', dev() ? 'no-cache' : 'max-age=600'); res.end(body); }).catch(err => { if (error) { From 87ff9c2aebe26063fe873ff182fccad5b566f8d5 Mon Sep 17 00:00:00 2001 From: Nolan Lawson Date: Fri, 7 Sep 2018 17:25:17 -0700 Subject: [PATCH 2/2] fix test --- src/middleware.ts | 2 +- test/common/test.js | 12 +++++++++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/middleware.ts b/src/middleware.ts index d86a77aee..eef75ca9a 100644 --- a/src/middleware.ts +++ b/src/middleware.ts @@ -312,6 +312,7 @@ function get_page_handler( } = get_build_info(); res.setHeader('Content-Type', 'text/html'); + res.setHeader('Cache-Control', dev() ? 'no-cache' : 'max-age=600'); // preload main.js and current route // TODO detect other stuff we can preload? images, CSS, fonts? @@ -532,7 +533,6 @@ function get_page_handler( .replace('%sapper.styles%', () => styles); res.statusCode = status; - res.setHeader('Cache-Control', dev() ? 'no-cache' : 'max-age=600'); res.end(body); }).catch(err => { if (error) { diff --git a/test/common/test.js b/test/common/test.js index f14991154..065e22e22 100644 --- a/test/common/test.js +++ b/test/common/test.js @@ -5,6 +5,7 @@ const Nightmare = require('nightmare'); const walkSync = require('walk-sync'); const rimraf = require('rimraf'); const ports = require('port-authority'); +const fetch = require('node-fetch'); Nightmare.action('page', { title(done) { @@ -800,15 +801,20 @@ function run({ mode, basepath = '' }) { }); describe('headers', () => { - it('sets Content-Type and Link...preload headers', () => { - return capture(() => nightmare.goto(base)).then(requests => { - const { headers } = requests[0]; + it('sets Content-Type, Link...preload, and Cache-Control headers', () => { + return capture(() => fetch(base)).then(responses => { + const { headers } = responses[0]; assert.equal( headers['content-type'], 'text/html' ); + assert.equal( + headers['cache-control'], + 'max-age=600' + ); + const str = ['main', '.+?\\.\\d+'] .map(file => { return `<${basepath}/client/[^/]+/${file}\\.js>;rel="preload";as="script"`;