diff --git a/lib/common/nuxt.config.js b/lib/common/nuxt.config.js index 3b88916f47..e2d58206a7 100644 --- a/lib/common/nuxt.config.js +++ b/lib/common/nuxt.config.js @@ -190,7 +190,13 @@ export default { etag: { weak: false }, - csp: false + csp: false, + dist: { + // Don't serve index.html template + index: false, + // 1 year in production + maxAge: '1y' + } }, // User-defined changes watch: [], diff --git a/lib/core/renderer.js b/lib/core/renderer.js index 34c66f514b..54b8f1e7a5 100644 --- a/lib/core/renderer.js +++ b/lib/core/renderer.js @@ -251,10 +251,10 @@ export default class Renderer { const distDir = path.resolve(this.options.buildDir, 'dist') this.useMiddleware({ path: this.publicPath, - handler: serveStatic(distDir, { - index: false, // Don't serve index.html template - maxAge: '1y' // 1 year in production - }) + handler: serveStatic( + distDir, + this.options.render.dist + ) }) } diff --git a/test/fixtures/basic/nuxt.config.js b/test/fixtures/basic/nuxt.config.js index 5f64d6d92d..f4eb543109 100644 --- a/test/fixtures/basic/nuxt.config.js +++ b/test/fixtures/basic/nuxt.config.js @@ -1,6 +1,11 @@ import path from 'path' export default { + render: { + dist: { + maxAge: ((60 * 60 * 24 * 365) * 2) + } + }, generate: { routes: [ // TODO: generate with {build: false} does not scans pages! diff --git a/test/unit/dist-options.test.js b/test/unit/dist-options.test.js new file mode 100644 index 0000000000..305c31191b --- /dev/null +++ b/test/unit/dist-options.test.js @@ -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() + }) +})