Skip to content

Commit

Permalink
feat: allow accessing meta properties in onGlobalSetup
Browse files Browse the repository at this point in the history
closes #305
  • Loading branch information
danielroe committed Nov 24, 2020
1 parent 739b04f commit 15cf20e
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 5 deletions.
2 changes: 1 addition & 1 deletion docs/content/en/helpers/useMeta.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ fullscreen: True
position: 18
---

You can interact directly with [head properties](https://nuxtjs.org/api/pages-head/) in `setup` by means of the `useMeta()` helper.
You can interact directly with [head properties](https://nuxtjs.org/api/pages-head/) in `setup` (and within the `onGlobalSetup` method) by means of the `useMeta()` helper.

```ts
import { defineComponent, useMeta, computed, ref } from '@nuxtjs/composition-api'
Expand Down
5 changes: 5 additions & 0 deletions src/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Plugin } from '@nuxt/types'
import type { SetupContext } from '@vue/composition-api'
import { getHeadOptions } from './meta'

import { reqRefs } from './req-ref'
import { setSSRContext } from './ssr-ref'
Expand Down Expand Up @@ -39,6 +40,10 @@ export const globalPlugin: Plugin = context => {
if (process.server) {
reqRefs.forEach(reset => reset())
}

const { head } = context.app
Object.assign(context.app, getHeadOptions({ head: head as any }))

context.app.setup = function (...args) {
let result = {}
if (setup instanceof Function) {
Expand Down
5 changes: 1 addition & 4 deletions test/fixture/layouts/default.vue
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@ export default defineComponent({
setup() {
const { route } = useContext()
const { title } = useMeta()
title.value = 'My fixture'
return { route, title }
return { route }
},
})
</script>
4 changes: 4 additions & 0 deletions test/fixture/plugins/global.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
onGlobalSetup,
useMeta,
provide,
ref,
ssrRef,
Expand All @@ -16,6 +17,9 @@ export default () => {
onGlobalSetup(() => {
const { $config } = useContext()

const { title } = useMeta()
title.value = 'My fixture'

ran.value++
if (process.server) ranSsr.value++

Expand Down

0 comments on commit 15cf20e

Please sign in to comment.