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) {