Render dist options (#3671)

Picking up on [yet another abandoned PR](https://github.com/nuxt/nuxt.js/pull/2933/files). Made all changes suggested by @clarkdo and wrote a test.
This commit is contained in:
Jonas Galvez 2018-08-10 06:34:55 -03:00 committed by Clark Du
parent b4d81dc584
commit 8d21b60a24
4 changed files with 55 additions and 5 deletions

View File

@ -190,7 +190,13 @@ export default {
etag: { etag: {
weak: false weak: false
}, },
csp: false csp: false,
dist: {
// Don't serve index.html template
index: false,
// 1 year in production
maxAge: '1y'
}
}, },
// User-defined changes // User-defined changes
watch: [], watch: [],

View File

@ -251,10 +251,10 @@ export default class Renderer {
const distDir = path.resolve(this.options.buildDir, 'dist') const distDir = path.resolve(this.options.buildDir, 'dist')
this.useMiddleware({ this.useMiddleware({
path: this.publicPath, path: this.publicPath,
handler: serveStatic(distDir, { handler: serveStatic(
index: false, // Don't serve index.html template distDir,
maxAge: '1y' // 1 year in production this.options.render.dist
}) )
}) })
} }

View File

@ -1,6 +1,11 @@
import path from 'path' import path from 'path'
export default { export default {
render: {
dist: {
maxAge: ((60 * 60 * 24 * 365) * 2)
}
},
generate: { generate: {
routes: [ routes: [
// TODO: generate with {build: false} does not scans pages! // TODO: generate with {build: false} does not scans pages!

View File

@ -0,0 +1,39 @@
import { loadFixture, getPort, Nuxt, rp } from '../utils'
let port
const url = route => 'http://localhost:' + port + route
let nuxt = null
describe('dist options', () => {
beforeAll(async () => {
const options = loadFixture('basic')
nuxt = new Nuxt(Object.assign(options, {dev: false}))
port = await getPort()
await nuxt.listen(port, '0.0.0.0')
})
test('Specify maxAge/index in render.dist options', async () => {
const { body } = await rp(url('/'), {
resolveWithFullResponse: true
})
try {
await rp(url('/_nuxt/'), {
resolveWithFullResponse: true
})
} catch (err) {
expect(err.toString().includes('StatusCodeError'))
}
const distFile = body.match(/\/_nuxt\/.+?\.js/)[0]
const { headers } = await rp(url(distFile), {
resolveWithFullResponse: true
})
const twoYears = (((60 * 60 * 24 * 365) * 2) / 1000).toString()
expect(headers['cache-control'].includes(twoYears)).toBe(true)
})
// Close server and ask nuxt to stop listening to file changes
afterAll(async () => {
await nuxt.close()
})
})