feat: basic support for ssr:false

This commit is contained in:
Pooya Parsa 2017-07-03 04:23:19 +04:30
parent e4e9149b54
commit c348d83120
2 changed files with 30 additions and 3 deletions

View File

@ -66,7 +66,8 @@ export default function webpackClientConfig () {
config.plugins.push( config.plugins.push(
new HTMLPlugin({ new HTMLPlugin({
template: this.options.appTemplatePath, template: this.options.appTemplatePath,
inject: false inject: this.options.render.ssr === false,
chunksSortMode: 'dependency'
}) })
) )

View File

@ -120,7 +120,16 @@ export default class Renderer extends Tapable {
} }
} }
get noSSR () {
return this.options.render.ssr === false
}
createRenderer () { createRenderer () {
// Skip if SSR is disabled
if (this.noSSR) {
return
}
// If resources are not yet provided // If resources are not yet provided
if (!this.resources.serverBundle || !this.resources.clientManifest) { if (!this.resources.serverBundle || !this.resources.clientManifest) {
return return
@ -294,7 +303,7 @@ export default class Renderer extends Tapable {
async renderRoute (url, context = {}) { async renderRoute (url, context = {}) {
/* istanbul ignore if */ /* istanbul ignore if */
if (!this.bundleRenderer || !this.resources.appTemplate) { if (!(this.noSSR || this.bundleRenderer) || !this.resources.appTemplate) {
return new Promise(resolve => { return new Promise(resolve => {
setTimeout(() => resolve(this.renderRoute(url, context)), 1000) setTimeout(() => resolve(this.renderRoute(url, context)), 1000)
}) })
@ -307,6 +316,23 @@ export default class Renderer extends Tapable {
context.url = url context.url = url
context.isServer = true context.isServer = true
// Basic response if SSR is disabled
if (this.noSSR) {
let APP = '<div id="__nuxt"></div>'
let HEAD = ''
let html = this.resources.appTemplate({
HTML_ATTRS: '',
BODY_ATTRS: '',
HEAD,
APP
})
return {
html
}
}
// Call renderToString from the bundleRenderer and generate the HTML (will update the context as well) // Call renderToString from the bundleRenderer and generate the HTML (will update the context as well)
let APP = await this.bundleRenderer.renderToString(context) let APP = await this.bundleRenderer.renderToString(context)
@ -333,7 +359,7 @@ export default class Renderer extends Tapable {
APP += `</script>` APP += `</script>`
APP += context.renderScripts() APP += context.renderScripts()
const html = this.resources.appTemplate({ let html = this.resources.appTemplate({
HTML_ATTRS: 'data-n-head-ssr ' + m.htmlAttrs.text(), HTML_ATTRS: 'data-n-head-ssr ' + m.htmlAttrs.text(),
BODY_ATTRS: m.bodyAttrs.text(), BODY_ATTRS: m.bodyAttrs.text(),
HEAD, HEAD,