From cd148a24b4000841f5cd5acc73b1f17d1e1556fb Mon Sep 17 00:00:00 2001 From: Mehdi Lahlou Date: Fri, 29 Sep 2017 03:37:53 +0200 Subject: [PATCH] fix(module): properly detect static mode (`nuxt generate`) Properly set `axios` baseUrl accordingly Properly trigger static routes generation in content builder accordingly fixes #83, #88, #89 --- lib/content/build.js | 4 ++-- lib/module.js | 39 +++++++++++++++++++++------------------ 2 files changed, 23 insertions(+), 20 deletions(-) diff --git a/lib/content/build.js b/lib/content/build.js index 7c66dd2..49ac6ea 100644 --- a/lib/content/build.js +++ b/lib/content/build.js @@ -82,7 +82,7 @@ const addAssets = (nuxtOpts, assetMap) => { * 1) Add content data to assets so that it can be statically requested. * 2) Adds dynamic content routes to nuxt generate so that pages can get built. */ -const buildContent = (nuxt, buildDir, options) => { +const buildContent = (nuxt, buildDir, isStatic, options) => { const { contentDir, content, parsers, isDev } = options const routePages = [] // dynamic pages to create @@ -98,7 +98,7 @@ const buildContent = (nuxt, buildDir, options) => { routePaths.set(name, permalink.replace(/^\//, '')) } - if (generate && process.env.STATIC) { + if (generate && isStatic) { const dirOpts = { ...content[dirName], parsers } const db = createDatabase(contentDir, dirName, dirOpts, isDev) diff --git a/lib/module.js b/lib/module.js index 667756e..00cb6ac 100644 --- a/lib/module.js +++ b/lib/module.js @@ -64,6 +64,7 @@ export default function ContentModule(moduleOpts) { const contentDirWebpackAlias = '~/' + CONTENT_DIR const port = process.env.PORT || process.env.npm_package_config_nuxt_port || 3000 + const isDev = this.nuxt.options.dev const loaderComponentExtensions = ['.vue', '.js'] @@ -115,8 +116,25 @@ export default function ContentModule(moduleOpts) { }) }) - // 2. Build dynamic content pages without components (*.md) - buildContent(this, BUILD_DIR, routesOptions) + this.nuxt.plugin('build', builder => { + // 1. Initialize axios module + this.requireModule([ + '@nuxtjs/axios', + { + baseURL: api.baseURL + api.serverPrefix, + browserBaseURL: + api.baseURL + + (builder.isStatic ? api.browserPrefix : api.serverPrefix) + } + ]) + + // 2. Build dynamic content pages without components (*.md) + buildContent(this, BUILD_DIR, builder.isStatic, routesOptions) + }) + + this.addPlugin({ + src: resolve(__dirname, 'plugins/requestContent.js') + }) // 3. Add content API const router = createRouter(api, routesOptions) @@ -138,22 +156,7 @@ export default function ContentModule(moduleOpts) { }) }) - // 4. Add request helpers - this.requireModule([ - '@nuxtjs/axios', - { - baseURL: api.baseURL + api.serverPrefix, - browserBaseURL: - api.baseURL + - (process.env.STATIC ? api.browserPrefix : api.serverPrefix) - } - ]) - - this.addPlugin({ - src: resolve(__dirname, 'plugins/requestContent.js') - }) - - // 5. Add Vue templates generated from markdown to output build + // 4. Add Vue templates generated from markdown to output build this.addPlugin({ src: resolve(__dirname, 'plugins/markdownComponents.template.js'), options: {