From f2261050a0bc67e470dd18fa3fb70c507175012e Mon Sep 17 00:00:00 2001 From: Jon Espen Kvisler Date: Sat, 19 May 2018 21:43:18 +0200 Subject: [PATCH] Set cache-control public (again) (#4322) * set cache-control public * test for Cache-Control header * set Cache-Control header for commons/main.js --- server/index.js | 12 ++++++--- .../integration/production/test/index.test.js | 26 +++++++++++++++++++ 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/server/index.js b/server/index.js index c5f28bcb..c248c370 100644 --- a/server/index.js +++ b/server/index.js @@ -161,7 +161,7 @@ export default class Server { '/_next/webpack/chunks/:name': async (req, res, params) => { // Cache aggressively in production if (!this.dev) { - res.setHeader('Cache-Control', 'max-age=31536000, immutable') + res.setHeader('Cache-Control', 'public, max-age=31536000, immutable') } const p = join(this.dir, this.dist, 'chunks', params.name) await this.serveStatic(req, res, p) @@ -227,8 +227,12 @@ export default class Server { '/_next/static/:path*': async (req, res, params) => { // The commons folder holds commonschunk files // In development they don't have a hash, and shouldn't be cached by the browser. - if (this.dev && params.path[0] === 'commons') { - res.setHeader('Cache-Control', 'no-store, must-revalidate') + if (params.path[0] === 'commons') { + if (this.dev) { + res.setHeader('Cache-Control', 'no-store, must-revalidate') + } else { + res.setHeader('Cache-Control', 'public, max-age=31536000, immutable') + } } const p = join(this.dir, this.dist, 'static', ...(params.path || [])) await this.serveStatic(req, res, p) @@ -434,7 +438,7 @@ export default class Server { return false } - res.setHeader('Cache-Control', 'max-age=31536000, immutable') + res.setHeader('Cache-Control', 'public, max-age=31536000, immutable') return true } diff --git a/test/integration/production/test/index.test.js b/test/integration/production/test/index.test.js index f298ad93..febc553b 100644 --- a/test/integration/production/test/index.test.js +++ b/test/integration/production/test/index.test.js @@ -1,5 +1,6 @@ /* global jasmine, describe, it, expect, beforeAll, afterAll */ +import { readFileSync } from 'fs' import { join } from 'path' import { pkg, @@ -52,6 +53,31 @@ describe('Production Usage', () => { expect(res2.status).toBe(304) }) + it('should set Cache-Control header', async () => { + const buildId = readFileSync(join(__dirname, '../.next/BUILD_ID'), 'utf8') + const buildManifest = require('../.next/build-manifest.json') + const url = `http://localhost:${appPort}/_next/` + + const resources = [] + + // test a regular page + resources.push(`${url}${buildId}/page/index.js`) + + // test dynamic chunk + const chunkKey = Object.keys(buildManifest).find((x) => x.includes('chunks/')) + resources.push(url + 'webpack/' + buildManifest[chunkKey]) + + // test main.js + const mainJsKey = Object.keys(buildManifest).find((x) => x === 'main.js') + resources.push(url + buildManifest[mainJsKey]) + + const responses = await Promise.all(resources.map((resource) => fetch(resource))) + + responses.forEach((res) => { + expect(res.headers.get('Cache-Control')).toBe('public, max-age=31536000, immutable') + }) + }) + it('should block special pages', async () => { const urls = ['/_document', '/_error'] for (const url of urls) {