Skip to content

Commit

Permalink
feat(bridge): add support for useNuxt2Meta in plugins (nuxt#3187)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe authored Feb 15, 2022
1 parent 42373e0 commit d046c96
Showing 1 changed file with 45 additions and 26 deletions.
71 changes: 45 additions & 26 deletions packages/bridge/src/runtime/composables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { MetaInfo } from 'vue-meta'
import type VueRouter from 'vue-router'
import type { Route } from 'vue-router'
import type { RuntimeConfig } from '@nuxt/schema'
import defu from 'defu'
import { useNuxtApp } from './app'

export { useLazyAsyncData } from './asyncData'
Expand Down Expand Up @@ -90,35 +91,53 @@ type AugmentedComponent = CombinedVueInstance<Vue, object, object, object, Recor
$metaInfo?: MetaInfo
}

export const useNuxt2Meta = (metaOptions: Reffed<MetaInfo> | (() => Reffed<MetaInfo>)) => {
const vm = getCurrentInstance()!.proxy as AugmentedComponent
const meta = vm.$meta()
const $root = vm.$root
/** internal */
function metaInfoFromOptions (metaOptions: Reffed<MetaInfo> | (() => Reffed<MetaInfo>)) {
return metaOptions instanceof Function ? metaOptions : () => metaOptions
}

if (!vm._vueMeta) {
vm._vueMeta = true
export const useNuxt2Meta = (metaOptions: Reffed<MetaInfo> | (() => Reffed<MetaInfo>)) => {
let vm: AugmentedComponent | null = null
try {
vm = getCurrentInstance()!.proxy as AugmentedComponent
const meta = vm.$meta()
const $root = vm.$root

if (!vm._vueMeta) {
vm._vueMeta = true

let parent = vm.$parent as AugmentedComponent
while (parent && parent !== $root) {
if (parent._vueMeta === undefined) {
parent._vueMeta = false
}
parent = parent.$parent
}
}
// @ts-ignore
vm.$options.head = vm.$options.head || {}

let parent = vm.$parent as AugmentedComponent
while (parent && parent !== $root) {
if (parent._vueMeta === undefined) {
parent._vueMeta = false
const unwatch = watch(metaInfoFromOptions(metaOptions), (metaInfo: MetaInfo) => {
vm.$metaInfo = {
...vm.$metaInfo || {},
...unwrap(metaInfo)
}
parent = parent.$parent
if (process.client) {
meta.refresh()
}
}, { immediate: true, deep: true })

onBeforeUnmount(unwatch)
} catch {
const app = (useNuxtApp().nuxt2Context as any).app
if (typeof app.head === 'function') {
const originalHead = app.head
app.head = function () {
const head = originalHead.call(this) || {}
return defu(unwrap(metaInfoFromOptions(metaOptions)), head)
}
} else {
app.head = defu(unwrap(metaInfoFromOptions(metaOptions)), app.head)
}
}
// @ts-ignore
vm.$options.head = vm.$options.head || {}

const metaSource = metaOptions instanceof Function ? metaOptions : () => metaOptions
const unwatch = watch(metaSource, (metaInfo: MetaInfo) => {
vm.$metaInfo = {
...vm.$metaInfo || {},
...unwrap(metaInfo)
}
if (process.client) {
meta.refresh()
}
}, { immediate: true, deep: true })

onBeforeUnmount(unwatch)
}

0 comments on commit d046c96

Please sign in to comment.