From afa2e6b05ce5918dc1a95511c42aa70f9f7bd083 Mon Sep 17 00:00:00 2001 From: "johan.roxendal@gu.se" Date: Mon, 24 Apr 2017 10:30:05 +0200 Subject: [PATCH] Implemented wait in generate function so generation of dynamic routes with ajax calls are not all sceduled simultaneously, possibly flooding the data backend with queries. Use generate.wait (ms) to stagger route generate calls over time. Fixes #590. --- lib/generate.js | 17 +++++++++++++++++ test/fixtures/basic/nuxt.config.js | 3 ++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/lib/generate.js b/lib/generate.js index b254fee33b..09577631ee 100644 --- a/lib/generate.js +++ b/lib/generate.js @@ -89,11 +89,28 @@ export default function () { this.routes.push(route) } }) + var n = 0 + function timer () { + let promise = new Promise( + (resolve, reject) => { + setTimeout( () => { + resolve() + }, self.options.generate.wait * n) + } + ) + n++ + return promise + + } + let routes = this.routes return co(function * () { while (routes.length) { yield routes.splice(0, 500).map((route) => { return co(function * () { + if(self.options.generate.wait) { + yield timer() + } var { html } = yield self.renderRoute(route, { _generate: true }) html = minify(html, self.options.generate.minify) var path = join(route, sep, 'index.html') // /about -> /about/index.html diff --git a/test/fixtures/basic/nuxt.config.js b/test/fixtures/basic/nuxt.config.js index 19c0433cab..daf0f9f260 100644 --- a/test/fixtures/basic/nuxt.config.js +++ b/test/fixtures/basic/nuxt.config.js @@ -4,6 +4,7 @@ module.exports = { '/users/1', '/users/2', '/users/3' - ] + ], + wait : 1000 } }