feat(vue-app, vue-renderer): support meta headAttrs (#4536)

This commit is contained in:
Ricardo Gobbo de Souza 2018-12-14 12:06:27 -02:00 committed by Pooya Parsa
parent da0a3794ef
commit 99614535b5
6 changed files with 50 additions and 1 deletions

View File

@ -1,6 +1,6 @@
<!DOCTYPE html> <!DOCTYPE html>
<html {{ HTML_ATTRS }}> <html {{ HTML_ATTRS }}>
<head> <head {{ HEAD_ATTRS }}>
{{ HEAD }} {{ HEAD }}
</head> </head>
<body {{ BODY_ATTRS }}> <body {{ BODY_ATTRS }}>

View File

@ -282,6 +282,7 @@ export default class VueRenderer {
renderTemplate(ssr, opts) { renderTemplate(ssr, opts) {
// Fix problem with HTMLPlugin's minify option (#3392) // Fix problem with HTMLPlugin's minify option (#3392)
opts.html_attrs = opts.HTML_ATTRS opts.html_attrs = opts.HTML_ATTRS
opts.head_attrs = opts.HEAD_ATTRS
opts.body_attrs = opts.BODY_ATTRS opts.body_attrs = opts.BODY_ATTRS
const fn = ssr ? this.context.resources.ssrTemplate : this.context.resources.spaTemplate const fn = ssr ? this.context.resources.ssrTemplate : this.context.resources.spaTemplate
@ -315,6 +316,7 @@ export default class VueRenderer {
if (!this.SSR || spa) { if (!this.SSR || spa) {
const { const {
HTML_ATTRS, HTML_ATTRS,
HEAD_ATTRS,
BODY_ATTRS, BODY_ATTRS,
HEAD, HEAD,
BODY_SCRIPTS, BODY_SCRIPTS,
@ -325,6 +327,7 @@ export default class VueRenderer {
const html = this.renderTemplate(false, { const html = this.renderTemplate(false, {
HTML_ATTRS, HTML_ATTRS,
HEAD_ATTRS,
BODY_ATTRS, BODY_ATTRS,
HEAD, HEAD,
APP, APP,
@ -382,6 +385,7 @@ export default class VueRenderer {
const html = this.renderTemplate(true, { const html = this.renderTemplate(true, {
HTML_ATTRS: 'data-n-head-ssr ' + m.htmlAttrs.text(), HTML_ATTRS: 'data-n-head-ssr ' + m.htmlAttrs.text(),
HEAD_ATTRS: m.headAttrs.text(),
BODY_ATTRS: m.bodyAttrs.text(), BODY_ATTRS: m.bodyAttrs.text(),
HEAD, HEAD,
APP, APP,

View File

@ -39,6 +39,7 @@ export default class SPAMetaRenderer {
meta = { meta = {
HTML_ATTRS: '', HTML_ATTRS: '',
HEAD_ATTRS: '',
BODY_ATTRS: '', BODY_ATTRS: '',
HEAD: '', HEAD: '',
BODY_SCRIPTS: '' BODY_SCRIPTS: ''
@ -50,6 +51,9 @@ export default class SPAMetaRenderer {
// HTML_ATTRS // HTML_ATTRS
meta.HTML_ATTRS = m.htmlAttrs.text() meta.HTML_ATTRS = m.htmlAttrs.text()
// HEAD_ATTRS
meta.HEAD_ATTRS = m.headAttrs.text()
// BODY_ATTRS // BODY_ATTRS
meta.BODY_ATTRS = m.bodyAttrs.text() meta.BODY_ATTRS = m.bodyAttrs.text()

View File

@ -0,0 +1,3 @@
import { buildFixture } from '../../utils/build'
buildFixture('meta-attrs')

15
test/fixtures/meta-attrs/nuxt.config.js vendored Normal file
View File

@ -0,0 +1,15 @@
export default {
head: {
htmlAttrs: {
foo: 'baz'
},
headAttrs: {
bar: 'foo'
},
bodyAttrs: {
baz: 'bar'
}
}
}

View File

@ -0,0 +1,23 @@
import { loadFixture, getPort, Nuxt } from '../utils'
let nuxt = null
describe('meta-attrs', () => {
beforeAll(async () => {
const options = await loadFixture('meta-attrs')
nuxt = new Nuxt(options)
await nuxt.server.listen(await getPort(), '0.0.0.0')
})
test('/', async () => {
const { html } = await nuxt.server.renderRoute('/')
expect(html).toContain('<html data-n-head-ssr foo="baz" data-n-head="foo">')
expect(html).toContain('<head bar="foo" data-n-head="bar">')
expect(html).toContain('<body baz="bar" data-n-head="baz">')
})
// Close server and ask nuxt to stop listening to file changes
afterAll(async () => {
await nuxt.close()
})
})