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

fix: overwrite titleTemplate with undefined #220

Merged
merged 5 commits into from
Aug 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 29 additions & 3 deletions src/meta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
reactive,
watch,
UnwrapRef,
customRef,
} from '@vue/composition-api'

import type { MetaInfo } from 'vue-meta'
Expand All @@ -32,13 +33,15 @@ function assign<T extends Record<string, any>>(target: T, source: Partial<T>) {
return target
}

export function createEmptyMeta(): MetaInfoMapper<Required<MetaInfo>> {
export function createEmptyMeta(): Omit<
MetaInfoMapper<Required<MetaInfo>>,
'titleTemplate'
> {
return {
__dangerouslyDisableSanitizers: [],
__dangerouslyDisableSanitizersByTagID: {},

title: undefined,
titleTemplate: undefined,
htmlAttrs: {},
headAttrs: {},
bodyAttrs: {},
Expand Down Expand Up @@ -104,7 +107,30 @@ export const useMeta = <T extends MetaInfo>(init?: T) => {
assign(_head, createEmptyMeta())
assign<MetaInfo>(_head, init || {})

const refs = toRefs(_head) as ToRefs<ReturnType<typeof createEmptyMeta> & T>
const refs = toRefs(_head) as ToRefs<
ReturnType<typeof createEmptyMeta> & {
titleTemplate: ReactiveHead['titleTemplate']
} & T
>

refs.titleTemplate = customRef<ReactiveHead['titleTemplate']>(
(track, trigger) => {
return {
get() {
track()
return _head.titleTemplate
},
set(newValue) {
if (!_head.titleTemplate) {
Vue.set(_head, 'titleTemplate', newValue)
} else {
_head.titleTemplate = newValue
}
trigger()
},
}
}
)

if (process.client)
watch(Object.values(refs), vm.$meta().refresh, { immediate: true })
Expand Down
19 changes: 13 additions & 6 deletions test/e2e/meta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,28 @@ fixture`useMeta`

test('Shows correct title on server-loaded page', async t => {
await navigateTo('/meta')
await t.expect(Selector('title').innerText).eql('newSetTitle')
await t.expect(Selector('title').innerText).eql('newSetTitle - fixture')
await expectOnPage('title-newSetTitle')
await t.expect(Selector('title').innerText).eql('mounted title')
await t.expect(Selector('title').innerText).eql('mounted title - fixture')
await expectOnPage('title-mounted title')
await t.expect(Selector('body').getAttribute('class')).eql('dark-mode mobile')

await t.click(Selector('button').withText('Change'))
await t.expect(Selector('title').innerText).eql('mounted title - Changed')

await t.click(Selector('a').withText('back'))
await t.expect(Selector('title').innerText).eql('My fixture')
await t.expect(Selector('title').innerText).eql('My fixture - fixture')
})

test('Shows correct title on client-loaded page', async t => {
await navigateTo('/')
await t.expect(Selector('title').innerText).eql('My fixture')
await t.expect(Selector('title').innerText).eql('My fixture - fixture')

await t.click(Selector('a').withText('meta'))
await t.expect(Selector('title').innerText).eql('newSetTitle')
await t.expect(Selector('title').innerText).eql('mounted title')
await t.expect(Selector('title').innerText).eql('newSetTitle - fixture')

await t.expect(Selector('title').innerText).eql('mounted title - fixture')

await t.click(Selector('button').withText('Change'))
await t.expect(Selector('title').innerText).eql('mounted title - Changed')
})
1 change: 1 addition & 0 deletions test/fixture/nuxt.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ module.exports = {
},
],
head: {
titleTemplate: title => `${title} - fixture`,
link: isTesting
? []
: [
Expand Down
11 changes: 9 additions & 2 deletions test/fixture/pages/meta.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
<blockquote>
<p>
<code>title-{{ title }}</code>
<button @click="changeTitleTemplate">
Change title template
</button>
</p>
</blockquote>
</template>
Expand All @@ -21,7 +24,7 @@ export default defineComponent({

title.value = 'oldSetTitle'

const { title: newImport, bodyAttrs } = useMeta()
const { title: newImport, bodyAttrs, titleTemplate } = useMeta()
newImport.value = 'newSetTitle'

onMounted(() => {
Expand All @@ -32,7 +35,11 @@ export default defineComponent({

bodyAttrs.value.class = ['dark-mode', 'mobile']

return { title }
function changeTitleTemplate() {
titleTemplate.value = `%s - Changed`
}

return { title, changeTitleTemplate, titleTemplate }
},
})
</script>
3 changes: 3 additions & 0 deletions test/tsd/meta.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@ import { expectType } from 'tsd'
import { useMeta, Ref } from '../..'

expectType<Ref<string>>(useMeta({ title: 'provided' }).title)
expectType<Ref<undefined | string | ((title: string) => string)>>(
useMeta().titleTemplate
)
expectType<Ref<string | undefined>>(useMeta().title)