mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-11 08:33:53 +00:00
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:
parent
b4d81dc584
commit
8d21b60a24
@ -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: [],
|
||||||
|
@ -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
|
||||||
})
|
)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
5
test/fixtures/basic/nuxt.config.js
vendored
5
test/fixtures/basic/nuxt.config.js
vendored
@ -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!
|
||||||
|
39
test/unit/dist-options.test.js
Normal file
39
test/unit/dist-options.test.js
Normal 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()
|
||||||
|
})
|
||||||
|
})
|
Loading…
Reference in New Issue
Block a user