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

View File

@ -37,5 +37,10 @@ module.exports = {
}, },
css: [ css: [
{ src: '~/assets/app.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 test from 'ava'
import { resolve } from 'path' import { resolve } from 'path'
import rp from 'request-promise-native'
const port = 4007 const port = 4007
const url = (route) => 'http://localhost:' + port + route 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) 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 // Close server and ask nuxt to stop listening to file changes
test.after('Closing server and nuxt.js', t => { test.after('Closing server and nuxt.js', t => {
server.close() server.close()