Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.

Commit b7467e2

Browse files
authored
feat: add composition api helper to interact with head() (#35)
Co-authored-by: Sebastian Krüger <2pi_r2@gmx.de>
1 parent 7b734ac commit b7467e2

File tree

5 files changed

+75
-3
lines changed

5 files changed

+75
-3
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,23 @@ export default defineComponent({
9393

9494
**Note**: `useFetch` must be called synchronously within `setup()`. Any changes made to component data - that is, to properties _returned_ from `setup()` - will be sent to the client and directly loaded. Other side-effects of `useFetch` hook will not be persisted.
9595

96+
### useHead
97+
98+
```ts
99+
import { defineComponent, useHead, computed } from 'nuxt-composition-api'
100+
101+
const { head, useMeta } = useHead()
102+
103+
export default defineComponent({
104+
head, // this line is needed!
105+
setup() {
106+
const { title } = useMeta()
107+
108+
title.value = 'newSetTitle'
109+
},
110+
})
111+
```
112+
96113
### ssrRef
97114

98115
When creating composition utility functions, often there will be server-side state that needs to be conveyed to the client.

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export const meta = require('../package.json')
2828
export { useFetch } from './fetch'
2929
export { withContext } from './context'
3030
export { ssrRef, onServerPrefetch, setSSRContext } from './ssr-ref'
31+
export { useHead } from './meta'
3132

3233
export {
3334
ComponentRenderProxy,

src/meta.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { reactive, toRefs } from '@vue/composition-api'
2+
import { MetaInfo } from 'vue-meta'
3+
4+
function createEmptyMeta(): MetaInfo {
5+
return {
6+
title: undefined,
7+
titleTemplate: undefined,
8+
htmlAttrs: undefined,
9+
headAttrs: undefined,
10+
bodyAttrs: undefined,
11+
12+
base: undefined,
13+
14+
meta: [],
15+
link: [],
16+
style: [],
17+
script: [],
18+
noscript: [],
19+
20+
changed: undefined,
21+
afterNavigation: undefined,
22+
}
23+
}
24+
25+
export function useHead(init: MetaInfo = {}) {
26+
const meta = reactive<MetaInfo>({
27+
...createEmptyMeta(),
28+
...init,
29+
})
30+
31+
return {
32+
head: () => meta,
33+
useMeta: () => toRefs(meta),
34+
}
35+
}

test/fixture/pages/index.vue

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
<template>
22
<div>
33
<div>name-{{ name }}</div>
4-
<div v-if="$fetchState.pending">
5-
loading email
6-
</div>
4+
<div v-if="$fetchState.pending">loading email</div>
75
<div>email-{{ email }}</div>
86
<div>{{ computedProp }}</div>
97
<div>{{ myFunction() }}</div>
108
<nuxt-link to="/other">link forward</nuxt-link>
119
<nuxt-link to="/ssr-ref">ssr refs</nuxt-link>
10+
<nuxt-link to="/meta">meta</nuxt-link>
1211
<button @click="$fetch">Refetch</button>
1312
<child-comp />
1413
</div>

test/fixture/pages/meta.vue

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<template>
2+
<div>
3+
<nuxt-link to="/">link back</nuxt-link>
4+
</div>
5+
</template>
6+
7+
<script>
8+
import { defineComponent, useHead, computed } from 'nuxt-composition-api'
9+
10+
const { head, useMeta } = useHead()
11+
12+
export default defineComponent({
13+
head,
14+
setup() {
15+
const { title } = useMeta()
16+
17+
title.value = 'newSetTitle'
18+
},
19+
})
20+
</script>

0 commit comments

Comments
 (0)