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) => {
|
'/_next/webpack/chunks/:name': async (req, res, params) => {
|
||||||
// Cache aggressively in production
|
// Cache aggressively in production
|
||||||
if (!this.dev) {
|
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)
|
const p = join(this.dir, this.dist, 'chunks', params.name)
|
||||||
await this.serveStatic(req, res, p)
|
await this.serveStatic(req, res, p)
|
||||||
|
@ -227,8 +227,12 @@ export default class Server {
|
||||||
'/_next/static/:path*': async (req, res, params) => {
|
'/_next/static/:path*': async (req, res, params) => {
|
||||||
// The commons folder holds commonschunk files
|
// The commons folder holds commonschunk files
|
||||||
// In development they don't have a hash, and shouldn't be cached by the browser.
|
// 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')
|
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 || []))
|
const p = join(this.dir, this.dist, 'static', ...(params.path || []))
|
||||||
await this.serveStatic(req, res, p)
|
await this.serveStatic(req, res, p)
|
||||||
|
@ -434,7 +438,7 @@ export default class Server {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
res.setHeader('Cache-Control', 'max-age=31536000, immutable')
|
res.setHeader('Cache-Control', 'public, max-age=31536000, immutable')
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
/* global jasmine, describe, it, expect, beforeAll, afterAll */
|
/* global jasmine, describe, it, expect, beforeAll, afterAll */
|
||||||
|
|
||||||
|
import { readFileSync } from 'fs'
|
||||||
import { join } from 'path'
|
import { join } from 'path'
|
||||||
import {
|
import {
|
||||||
pkg,
|
pkg,
|
||||||
|
@ -52,6 +53,31 @@ describe('Production Usage', () => {
|
||||||
expect(res2.status).toBe(304)
|
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 () => {
|
it('should block special pages', async () => {
|
||||||
const urls = ['/_document', '/_error']
|
const urls = ['/_document', '/_error']
|
||||||
for (const url of urls) {
|
for (const url of urls) {
|
||||||
|
|
Loading…
Reference in a new issue