mirror of
https://github.com/terribleplan/next.js.git
synced 2024-01-19 02:48:18 +00:00
Set cache-control public (again) (#4322)
* set cache-control public * test for Cache-Control header * set Cache-Control header for commons/main.js
This commit is contained in:
parent
915673fcd6
commit
f2261050a0
|
@ -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') {
|
||||
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
|
||||
}
|
||||
|
||||
|
|
|
@ -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) {
|
||||
|
|
Loading…
Reference in a new issue