提交 f2261050 编写于 作者: J Jon Espen Kvisler 提交者: Tim Neutkens

Set cache-control public (again) (#4322)

* set cache-control public

* test for Cache-Control header

* set Cache-Control header for commons/main.js
上级 915673fc
......@@ -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
}
......
/* 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) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册