From 3f650e154923f4d03ff868a63891c5a89b91bf56 Mon Sep 17 00:00:00 2001 From: Lin Qiu Date: Wed, 12 Sep 2018 08:44:15 -0400 Subject: [PATCH] Update 404 static cache header to not cache (#5146) --- server/index.js | 1 + test/integration/production/test/index.test.js | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/server/index.js b/server/index.js index 401773c6..eb158b42 100644 --- a/server/index.js +++ b/server/index.js @@ -310,6 +310,7 @@ export default class Server { async render404 (req, res, parsedUrl = parseUrl(req.url, true)) { const { pathname, query } = parsedUrl res.statusCode = 404 + res.setHeader('Cache-Control', 'no-cache, no-store, max-age=0, must-revalidate') return this.renderError(null, req, res, pathname, query) } diff --git a/test/integration/production/test/index.test.js b/test/integration/production/test/index.test.js index 04c4e237..18a3425b 100644 --- a/test/integration/production/test/index.test.js +++ b/test/integration/production/test/index.test.js @@ -91,6 +91,14 @@ describe('Production Usage', () => { }) }) + it('should set correct Cache-Control header for static 404s', async () => { + // this is to fix where 404 headers are set to 'public, max-age=31536000, immutable' + const res = await fetch(`http://localhost:${appPort}/_next//static/common/bad-static.js`) + + expect(res.status).toBe(404) + expect(res.headers.get('Cache-Control')).toBe('no-cache, no-store, max-age=0, must-revalidate') + }) + it('should block special pages', async () => { const urls = ['/_document', '/_error'] for (const url of urls) {