From 14c3c07d578ed2df99f89b567a18eea37182cef0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Chopin?= Date: Sun, 21 May 2017 15:13:19 +0200 Subject: [PATCH 1/5] Remove `cache` option, use `build.ssr.cache` instead --- examples/cached-components/nuxt.config.js | 9 ++++++++- examples/cached-components/package.json | 1 + examples/hello-world/nuxt.config.js | 5 +++++ lib/build.js | 9 --------- lib/nuxt.js | 1 - package.json | 2 -- yarn.lock | 2 +- 7 files changed, 15 insertions(+), 14 deletions(-) create mode 100644 examples/hello-world/nuxt.config.js diff --git a/examples/cached-components/nuxt.config.js b/examples/cached-components/nuxt.config.js index 6277640bf8..9ca68847a0 100644 --- a/examples/cached-components/nuxt.config.js +++ b/examples/cached-components/nuxt.config.js @@ -1,3 +1,10 @@ module.exports = { - cache: true + build: { + ssr: { + cache: require('lru-cache')({ + max: 1000, + maxAge: 1000 * 60 * 15 + }) + } + } } diff --git a/examples/cached-components/package.json b/examples/cached-components/package.json index 05750ddc83..1e7850343b 100644 --- a/examples/cached-components/package.json +++ b/examples/cached-components/package.json @@ -1,6 +1,7 @@ { "name": "nuxt-cached-components", "dependencies": { + "lru-cache": "^4.0.2", "nuxt": "latest" }, "scripts": { diff --git a/examples/hello-world/nuxt.config.js b/examples/hello-world/nuxt.config.js new file mode 100644 index 0000000000..b35102c3b2 --- /dev/null +++ b/examples/hello-world/nuxt.config.js @@ -0,0 +1,5 @@ +module.exports = { + generate: { + minify: false + } +} \ No newline at end of file diff --git a/lib/build.js b/lib/build.js index ab9b2571b4..c98127bee7 100644 --- a/lib/build.js +++ b/lib/build.js @@ -490,16 +490,7 @@ function webpackRunServer () { function createRenderer (bundle, manifest) { // Create bundle renderer to give a fresh context for every request - let cacheConfig = false - if (this.options.cache) { - this.options.cache = (typeof this.options.cache !== 'object' ? {} : this.options.cache) - cacheConfig = require('lru-cache')(_.defaults(this.options.cache, { - max: 1000, - maxAge: 1000 * 60 * 15 - })) - } this.renderer = createBundleRenderer(bundle, Object.assign({ - cache: cacheConfig, clientManifest: manifest, runInNewContext: false, inject: false, diff --git a/lib/nuxt.js b/lib/nuxt.js index 55fcf1919a..0e5e0308f6 100644 --- a/lib/nuxt.js +++ b/lib/nuxt.js @@ -30,7 +30,6 @@ class Nuxt { layouts: {}, serverMiddleware: [], ErrorPage: null, - cache: false, loading: { color: 'black', failedColor: 'red', diff --git a/package.json b/package.json index 6119ebb401..2cdf59a97e 100644 --- a/package.json +++ b/package.json @@ -59,7 +59,6 @@ "babel-preset-es2015": "^6.24.1", "babel-preset-vue-app": "^1.2.0", "chokidar": "^1.7.0", - "co": "^4.6.0", "compression": "^1.6.2", "connect": "^3.6.2", "css-loader": "^0.28.1", @@ -73,7 +72,6 @@ "html-minifier": "^3.5.0", "html-webpack-plugin": "^2.28.0", "lodash": "^4.17.4", - "lru-cache": "^4.0.2", "memory-fs": "^0.4.1", "offline-plugin": "^4.7.0", "opencollective": "^1.0.3", diff --git a/yarn.lock b/yarn.lock index 3898862a9d..2a93aa4892 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3878,7 +3878,7 @@ lru-cache@^3.2.0: dependencies: pseudomap "^1.0.1" -lru-cache@^4.0.0, lru-cache@^4.0.1, lru-cache@^4.0.2: +lru-cache@^4.0.0, lru-cache@^4.0.1: version "4.0.2" resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-4.0.2.tgz#1d17679c069cda5d040991a09dbc2c0db377e55e" dependencies: From ecde37a279616726970b38ddbfc24afd8e010a7e Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Sun, 21 May 2017 17:47:11 +0430 Subject: [PATCH 2/5] [deps] update vue-loader to 12.1.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index fb2aafd4eb..baf3e87806 100644 --- a/package.json +++ b/package.json @@ -88,7 +88,7 @@ "serve-static": "^1.12.2", "url-loader": "^0.5.8", "vue": "~2.3.3", - "vue-loader": "^12.0.4", + "vue-loader": "^12.1.0", "vue-meta": "^1.0.4", "vue-router": "^2.5.3", "vue-server-renderer": "~2.3.3", From 9c964ee0acf3d818254bbb75214cbdaaa1ffdba4 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Sun, 21 May 2017 17:51:10 +0430 Subject: [PATCH 3/5] [deps] update vue-loader to 12.1.0 --- yarn.lock | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/yarn.lock b/yarn.lock index a7256e8d6c..ff40a911db 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6121,9 +6121,9 @@ vue-hot-reload-api@^2.1.0: version "2.1.0" resolved "https://registry.yarnpkg.com/vue-hot-reload-api/-/vue-hot-reload-api-2.1.0.tgz#9ca58a6e0df9078554ce1708688b6578754d86de" -vue-loader@^12.0.4: - version "12.0.4" - resolved "https://registry.yarnpkg.com/vue-loader/-/vue-loader-12.0.4.tgz#bc9385326e3fd6538606454977baa91eacc6e6fd" +vue-loader@^12.1.0: + version "12.1.0" + resolved "https://registry.yarnpkg.com/vue-loader/-/vue-loader-12.1.0.tgz#f9ca958da1fd4e8c0598d90375c5419d10d35546" dependencies: consolidate "^0.14.0" hash-sum "^1.0.2" From 8350dbfdbc54f0c1496eefc2288292c58bfea7ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Chopin?= Date: Sun, 21 May 2017 15:26:39 +0200 Subject: [PATCH 4/5] Disable minify with `generate.minify: false` --- lib/generate.js | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/lib/generate.js b/lib/generate.js index a48241aa46..f8053455ac 100644 --- a/lib/generate.js +++ b/lib/generate.js @@ -45,7 +45,6 @@ export default async function () { ** Set variables */ this.options.generate = _.defaultsDeep(this.options.generate, defaults) - var self = this var srcStaticPath = resolve(this.srcDir, 'static') var srcBuiltPath = resolve(this.dir, '.nuxt', 'dist') var distPath = resolve(this.dir, this.options.generate.dir) @@ -53,7 +52,7 @@ export default async function () { /* ** Launch build process */ - await self.build() + await this.build() /* ** Clean destination folder */ @@ -95,28 +94,32 @@ export default async function () { while (routes.length) { let n = 0 await Promise.all(routes.splice(0, 500).map(async (route) => { - await waitFor(n++ * self.options.generate.interval) + await waitFor(n++ * this.options.generate.interval) + let html try { - var { html, error } = await self.renderRoute(route, { _generate: true }) - if (error) { - errors.push({ type: 'handled', route, error }) + const res = await this.renderRoute(route, { _generate: true }) + html = res.html + if (res.error) { + errors.push({ type: 'handled', route, error: res.error }) } } catch (err) { /* istanbul ignore next */ errors.push({ type: 'unhandled', route, error: err }) } - try { - var minifiedHtml = minify(html, self.options.generate.minify) - } catch (err) /* istanbul ignore next */ { - let minifyErr = new Error(`HTML minification failed. Make sure the route generates valid HTML. Failed HTML:\n ${html}`) - errors.push({ type: 'unhandled', route, error: minifyErr }) + if (this.options.generate.minify) { + try { + html = minify(html, this.options.generate.minify) + } catch (err) /* istanbul ignore next */ { + const minifyErr = new Error(`HTML minification failed. Make sure the route generates valid HTML. Failed HTML:\n ${html}`) + errors.push({ type: 'unhandled', route, error: minifyErr }) + } } - var path = join(route, sep, 'index.html') // /about -> /about/index.html + let path = join(route, sep, 'index.html') // /about -> /about/index.html debug('Generate file: ' + path) path = join(distPath, path) // Make sure the sub folders are created await mkdirp(dirname(path)) - await writeFile(path, minifiedHtml, 'utf8') + await writeFile(path, html, 'utf8') })) } // Add .nojekyll file to let Github Pages add the _nuxt/ folder From c3355e1467f2f690ad92944991ab0a9fca1ac07c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Chopin?= Date: Sun, 21 May 2017 15:45:21 +0200 Subject: [PATCH 5/5] Store state should always be a method now --- examples/hello-world/nuxt.config.js | 5 ----- examples/vuex-store-modules/store/articles.js | 4 ++-- .../vuex-store-modules/store/articles/comments.js | 4 ++-- examples/vuex-store-modules/store/index.js | 4 +++- examples/vuex-store-modules/store/todos.js | 4 ++-- lib/app/store.js | 14 +++++++------- test/fixtures/basic/store/foo/bar.js | 8 +++----- test/fixtures/basic/store/index.js | 8 +++----- 8 files changed, 22 insertions(+), 29 deletions(-) delete mode 100644 examples/hello-world/nuxt.config.js diff --git a/examples/hello-world/nuxt.config.js b/examples/hello-world/nuxt.config.js deleted file mode 100644 index b35102c3b2..0000000000 --- a/examples/hello-world/nuxt.config.js +++ /dev/null @@ -1,5 +0,0 @@ -module.exports = { - generate: { - minify: false - } -} \ No newline at end of file diff --git a/examples/vuex-store-modules/store/articles.js b/examples/vuex-store-modules/store/articles.js index a0698ff850..e2fb76f674 100644 --- a/examples/vuex-store-modules/store/articles.js +++ b/examples/vuex-store-modules/store/articles.js @@ -1,10 +1,10 @@ -export const state = { +export const state = () => ({ list: [ 'Lorem ipsum', 'dolor sit amet', 'consetetur sadipscing elitr' ] -} +}) export const mutations = { add (state, title) { diff --git a/examples/vuex-store-modules/store/articles/comments.js b/examples/vuex-store-modules/store/articles/comments.js index 1cb5606bae..dd7f3c47c0 100644 --- a/examples/vuex-store-modules/store/articles/comments.js +++ b/examples/vuex-store-modules/store/articles/comments.js @@ -1,10 +1,10 @@ -export const state = { +export const state = () => ({ list: [ 'Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.', 'Lorem ipsum dolor sit amet, consetetur sadipscing elit.', 'Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.' ] -} +}) export const mutations = { add (state, title) { diff --git a/examples/vuex-store-modules/store/index.js b/examples/vuex-store-modules/store/index.js index a4521c2fc2..63f1975bf2 100644 --- a/examples/vuex-store-modules/store/index.js +++ b/examples/vuex-store-modules/store/index.js @@ -1,4 +1,6 @@ -export const state = { counter: 0 } +export const state = () => ({ + counter: 0 +}) export const mutations = { increment (state) { diff --git a/examples/vuex-store-modules/store/todos.js b/examples/vuex-store-modules/store/todos.js index befde00b95..5063cbd1b8 100644 --- a/examples/vuex-store-modules/store/todos.js +++ b/examples/vuex-store-modules/store/todos.js @@ -1,6 +1,6 @@ -export const state = { +export const state = () => ({ list: [] -} +}) export const mutations = { add (state, { text }) { diff --git a/lib/app/store.js b/lib/app/store.js index da563d00cf..4d12f9ecef 100644 --- a/lib/app/store.js +++ b/lib/app/store.js @@ -13,7 +13,7 @@ let storeData = {} // Check if store/index.js exists const indexFilename = filenames.find((filename) => filename.includes('./index.')) if (indexFilename) { - storeData = getModule(indexFilename, true) + storeData = getModule(indexFilename) } // If store is not an exported method = modules store @@ -41,19 +41,19 @@ if (typeof storeData !== 'function') { // createStore export const createStore = storeData instanceof Function ? storeData : () => { return new Vuex.Store(Object.assign({}, storeData, { - // state: storeData.state instanceof Function ? storeData.state() : {} + state: storeData.state instanceof Function ? storeData.state() : {} })) } // Dynamically require module -function getModule (filename, root) { +function getModule (filename) { const file = files(filename) const module = file.default || file - // if (root && module.state && typeof module.state !== 'function') { - // throw new Error('[nuxt] root store state should be a function.') - // } if (module.commit) { - throw new Error('[nuxt] store should export raw store options instead of an instance.') + throw new Error('[nuxt] store/' + filename.replace('./', '') + ' should export a method which returns a Vuex instance.') + } + if (module.state && typeof module.state !== 'function') { + throw new Error('[nuxt] state should be a function in store/' + filename.replace('./', '')) } return module } diff --git a/test/fixtures/basic/store/foo/bar.js b/test/fixtures/basic/store/foo/bar.js index c8163e596a..9a709f66e7 100644 --- a/test/fixtures/basic/store/foo/bar.js +++ b/test/fixtures/basic/store/foo/bar.js @@ -1,8 +1,6 @@ -export const state = () => { - return { - baz: 'Vuex Nested Modules' - } -} +export const state = () => ({ + baz: 'Vuex Nested Modules' +}) export const getters = { baz (state) { diff --git a/test/fixtures/basic/store/index.js b/test/fixtures/basic/store/index.js index 4edd0cdc3f..1cd4087a00 100755 --- a/test/fixtures/basic/store/index.js +++ b/test/fixtures/basic/store/index.js @@ -3,11 +3,9 @@ import Vuex from 'vuex' Vue.use(Vuex) -export const state = () => { - return { - counter: 1 - } -} +export const state = () => ({ + counter: 1 +}) export const mutations = { increment (state) {