Add render.static option to customize serve-static middleware

This commit is contained in:
Sebastien Chopin 2017-05-22 12:51:03 +02:00
parent 1f6c3a9a7b
commit eaee5afbde
4 changed files with 16 additions and 2 deletions

View File

@ -50,6 +50,7 @@ class Nuxt {
scrollBehavior: null
},
render: {
static: {},
gzip: {
threshold: 0
},
@ -90,7 +91,7 @@ class Nuxt {
// renderer used by Vue.js (via createBundleRenderer)
this.renderer = null
// For serving static/ files to /
this.serveStatic = pify(serveStatic(resolve(this.srcDir, 'static')))
this.serveStatic = pify(serveStatic(resolve(this.srcDir, 'static'), this.options.render.static))
// For serving .nuxt/dist/ files (only when build.publicPath is not an URL)
this.serveStaticNuxt = pify(serveStatic(resolve(this.dir, '.nuxt', 'dist'), {
maxAge: (this.dev ? 0 : '1y') // 1 year in production

View File

@ -37,5 +37,10 @@ module.exports = {
},
css: [
{ src: '~/assets/app.css' }
]
],
render: {
static: {
maxAge: '1y'
}
}
}

View File

@ -0,0 +1 @@
A test here :)

View File

@ -1,5 +1,7 @@
import test from 'ava'
import { resolve } from 'path'
import rp from 'request-promise-native'
const port = 4007
const url = (route) => 'http://localhost:' + port + route
@ -91,6 +93,11 @@ test('Check stats.json generated by build.analyze', t => {
t.is(stats.assets.length, 29)
})
test('Check /test.txt with custom serve-static options', async t => {
const { headers } = await rp(url('/test.txt'), { resolveWithFullResponse: true })
t.is(headers['cache-control'], 'public, max-age=31536000')
})
// Close server and ask nuxt to stop listening to file changes
test.after('Closing server and nuxt.js', t => {
server.close()