From b17f331fe51bb85776213988eb13559342955fcd Mon Sep 17 00:00:00 2001 From: Pim Date: Wed, 18 Sep 2019 15:51:44 +0200 Subject: [PATCH] feat: support setting (non-essential) vue-meta options by user (#6432) --- packages/config/src/config/_app.js | 2 ++ .../test/__snapshots__/options.test.js.snap | 1 + .../config/__snapshots__/index.test.js.snap | 2 ++ packages/vue-app/template/index.js | 9 ++++--- packages/vue-renderer/src/renderers/spa.js | 3 ++- test/fixtures/basic/pages/head.vue | 9 +++++++ test/fixtures/meta-attrs/meta-attrs.test.js | 3 --- test/fixtures/meta-attrs/nuxt.config.js | 15 ----------- test/fixtures/with-config/nuxt.config.js | 3 +++ test/fixtures/with-config/pages/head.vue | 13 ++++++++++ test/unit/basic.ssr.test.js | 6 ++++- test/unit/meta-attrs.test.js | 25 ------------------- test/unit/with-config.test.js | 6 +++++ 13 files changed, 49 insertions(+), 48 deletions(-) delete mode 100644 test/fixtures/meta-attrs/meta-attrs.test.js delete mode 100644 test/fixtures/meta-attrs/nuxt.config.js create mode 100644 test/fixtures/with-config/pages/head.vue delete mode 100644 test/unit/meta-attrs.test.js diff --git a/packages/config/src/config/_app.js b/packages/config/src/config/_app.js index 5d285c875d..8db8e1f9ad 100644 --- a/packages/config/src/config/_app.js +++ b/packages/config/src/config/_app.js @@ -6,6 +6,8 @@ export default () => ({ } }, + vueMeta: null, + head: { meta: [], link: [], diff --git a/packages/config/test/__snapshots__/options.test.js.snap b/packages/config/test/__snapshots__/options.test.js.snap index 5fe1eb9c02..af5e2cbf23 100644 --- a/packages/config/test/__snapshots__/options.test.js.snap +++ b/packages/config/test/__snapshots__/options.test.js.snap @@ -367,6 +367,7 @@ Object { "silent": true, }, }, + "vueMeta": null, "watch": Array [ "/var/nuxt/test/nuxt.config.js", ], diff --git a/packages/config/test/config/__snapshots__/index.test.js.snap b/packages/config/test/config/__snapshots__/index.test.js.snap index a0eacacdb0..29562c6098 100644 --- a/packages/config/test/config/__snapshots__/index.test.js.snap +++ b/packages/config/test/config/__snapshots__/index.test.js.snap @@ -334,6 +334,7 @@ Object { "silent": undefined, }, }, + "vueMeta": null, "watch": Array [], "watchers": Object { "chokidar": Object { @@ -681,6 +682,7 @@ Object { "silent": undefined, }, }, + "vueMeta": null, "watch": Array [], "watchers": Object { "chokidar": Object { diff --git a/packages/vue-app/template/index.js b/packages/vue-app/template/index.js index a26a154e37..92b9ef55ce 100644 --- a/packages/vue-app/template/index.js +++ b/packages/vue-app/template/index.js @@ -43,14 +43,17 @@ Vue.component(NuxtChild.name, NuxtChild) // Component: ` Vue.component(Nuxt.name, Nuxt) -<% if (features.meta) { %> +<% if (features.meta) { // vue-meta configuration -Vue.use(Meta, { +const vueMetaOptions = { + ...nuxtOptions.vueMeta, keyName: 'head', // the component option name that vue-meta looks for meta info on. attribute: 'data-n-head', // the attribute name vue-meta adds to the tags it observes ssrAttribute: 'data-n-head-ssr', // the attribute name that lets vue-meta know that meta info has already been server-rendered tagIDKeyName: 'hid' // the property name that vue-meta uses to determine whether to overwrite or append a tag -}) +} +%> +Vue.use(Meta, <%= JSON.stringify(vueMetaOptions) %>)<%= isTest ? '// eslint-disable-line' : '' %> <% } %> <% if (features.transitions) { %> diff --git a/packages/vue-renderer/src/renderers/spa.js b/packages/vue-renderer/src/renderers/spa.js index 09958eccc5..692ec6b1b1 100644 --- a/packages/vue-renderer/src/renderers/spa.js +++ b/packages/vue-renderer/src/renderers/spa.js @@ -13,10 +13,11 @@ export default class SPARenderer extends BaseRenderer { this.cache = new LRU() this.vueMetaConfig = { + ssrAppId: '1', + ...this.options.vueMeta, keyName: 'head', attribute: 'data-n-head', ssrAttribute: 'data-n-head-ssr', - ssrAppId: '1', tagIDKeyName: 'hid' } } diff --git a/test/fixtures/basic/pages/head.vue b/test/fixtures/basic/pages/head.vue index 1be13ec8e8..550794d849 100644 --- a/test/fixtures/basic/pages/head.vue +++ b/test/fixtures/basic/pages/head.vue @@ -8,6 +8,15 @@ export default { head: { title: 'My title', + htmlAttrs: { + foo: 'baz' + }, + headAttrs: { + bar: 'foo' + }, + bodyAttrs: { + baz: 'bar' + }, meta: [ { content: 'my meta' } ], diff --git a/test/fixtures/meta-attrs/meta-attrs.test.js b/test/fixtures/meta-attrs/meta-attrs.test.js deleted file mode 100644 index 8f43c520c8..0000000000 --- a/test/fixtures/meta-attrs/meta-attrs.test.js +++ /dev/null @@ -1,3 +0,0 @@ -import { buildFixture } from '../../utils/build' - -buildFixture('meta-attrs') diff --git a/test/fixtures/meta-attrs/nuxt.config.js b/test/fixtures/meta-attrs/nuxt.config.js deleted file mode 100644 index 8acbef9f8d..0000000000 --- a/test/fixtures/meta-attrs/nuxt.config.js +++ /dev/null @@ -1,15 +0,0 @@ -export default { - head: { - htmlAttrs: { - foo: 'baz' - }, - - headAttrs: { - bar: 'foo' - }, - - bodyAttrs: { - baz: 'bar' - } - } -} diff --git a/test/fixtures/with-config/nuxt.config.js b/test/fixtures/with-config/nuxt.config.js index 4cf8ce3806..9f5d2904cb 100644 --- a/test/fixtures/with-config/nuxt.config.js +++ b/test/fixtures/with-config/nuxt.config.js @@ -11,6 +11,9 @@ export default { total: true } }, + vueMeta: { + ssrAppId: 'test-ssr-app-id' + }, router: { base: '/test/', middleware: 'noop', diff --git a/test/fixtures/with-config/pages/head.vue b/test/fixtures/with-config/pages/head.vue new file mode 100644 index 0000000000..5dff57d8fa --- /dev/null +++ b/test/fixtures/with-config/pages/head.vue @@ -0,0 +1,13 @@ + + + diff --git a/test/unit/basic.ssr.test.js b/test/unit/basic.ssr.test.js index 25e48f03c2..c0fa38202a 100644 --- a/test/unit/basic.ssr.test.js +++ b/test/unit/basic.ssr.test.js @@ -82,13 +82,17 @@ describe('basic ssr', () => { const window = await nuxt.server.renderAndGetWindow(url('/head')) expect(window.document.title).toBe('My title - Nuxt.js') - const html = window.document.body.innerHTML + const html = window.document.querySelector('html').outerHTML expect(html).toContain('

I can haz meta tags

') expect(html).toContain('