Skip to content
This repository has been archived by the owner on Jan 11, 2023. It is now read-only.

Use consistent cache-control:max-age=600 for HTML pages #429

Merged
merged 2 commits into from
Sep 8, 2018
Merged
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
3 changes: 2 additions & 1 deletion src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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?
Expand Down
12 changes: 9 additions & 3 deletions test/common/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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"`;
Expand Down