mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-11 08:33:53 +00:00
feat(meta): add default head values (#382)
Co-authored-by: Pooya Parsa <pyapar@gmail.com>
This commit is contained in:
parent
cc212cedc8
commit
2da82ce5cf
@ -10,9 +10,10 @@
|
|||||||
"require": "./dist/module.js"
|
"require": "./dist/module.js"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"types": "./dist/runtime/index.d.ts",
|
"types": "./types.d.ts",
|
||||||
"files": [
|
"files": [
|
||||||
"dist"
|
"dist",
|
||||||
|
"types.d.ts"
|
||||||
],
|
],
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"prepack": "unbuild"
|
"prepack": "unbuild"
|
||||||
|
@ -1,17 +1,38 @@
|
|||||||
import { resolve } from 'upath'
|
import { resolve } from 'upath'
|
||||||
import { defineNuxtModule } from '@nuxt/kit'
|
import { addPlugin, addTemplate, defineNuxtModule } from '@nuxt/kit'
|
||||||
|
import type { MetaObject } from '@nuxt/meta'
|
||||||
|
|
||||||
export default defineNuxtModule({
|
export default defineNuxtModule({
|
||||||
name: 'meta',
|
name: 'meta',
|
||||||
setup (_options, nuxt) {
|
defaults: {
|
||||||
|
charset: 'utf-8',
|
||||||
|
viewport: 'width=device-width, initial-scale=1'
|
||||||
|
},
|
||||||
|
setup (options, nuxt) {
|
||||||
const runtimeDir = resolve(__dirname, 'runtime')
|
const runtimeDir = resolve(__dirname, 'runtime')
|
||||||
|
|
||||||
|
// Transpile @nuxt/meta
|
||||||
nuxt.options.build.transpile.push('@nuxt/meta', runtimeDir)
|
nuxt.options.build.transpile.push('@nuxt/meta', runtimeDir)
|
||||||
nuxt.options.alias['@nuxt/meta'] = resolve(runtimeDir, 'index')
|
nuxt.options.alias['@nuxt/meta'] = resolve(runtimeDir, 'index')
|
||||||
|
|
||||||
nuxt.hook('app:resolve', (app) => {
|
// Global meta
|
||||||
app.plugins.push({ src: resolve(runtimeDir, 'vueuse-head') })
|
const globalMeta: MetaObject = {
|
||||||
app.plugins.push({ src: resolve(runtimeDir, 'meta') })
|
meta: [
|
||||||
|
{ charset: options.charset },
|
||||||
|
{ name: 'viewport', content: options.viewport }
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add global meta configuration
|
||||||
|
addTemplate({
|
||||||
|
filename: 'meta.config.mjs',
|
||||||
|
getContents: () => 'export default ' + JSON.stringify({ globalMeta })
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// Add generic plugin
|
||||||
|
addPlugin({ src: resolve(runtimeDir, 'plugin') })
|
||||||
|
|
||||||
|
// Add library specific plugin
|
||||||
|
addPlugin({ src: resolve(runtimeDir, 'lib/vueuse-head.plugin') })
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import { defineComponent, SetupContext } from 'vue'
|
import { defineComponent, SetupContext } from 'vue'
|
||||||
import { useMeta } from './index'
|
import { useMeta } from './composables'
|
||||||
|
|
||||||
type Props = Readonly<Record<string, any>>
|
type Props = Readonly<Record<string, any>>
|
||||||
|
|
||||||
|
@ -1,7 +1,8 @@
|
|||||||
// import { useMeta as useVueMeta } from 'vue-meta'
|
// import { useMeta as useVueMeta } from 'vue-meta'
|
||||||
import { isFunction } from '@vue/shared'
|
import { isFunction } from '@vue/shared'
|
||||||
import { computed, ComputedGetter } from '@vue/reactivity'
|
import { computed, ComputedGetter } from '@vue/reactivity'
|
||||||
import { useHead } from '@vueuse/head'
|
import { useNuxt } from '@nuxt/app'
|
||||||
|
import type { MetaObject } from '@nuxt/meta'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* You can pass in a meta object, which has keys corresponding to meta tags:
|
* You can pass in a meta object, which has keys corresponding to meta tags:
|
||||||
@ -10,16 +11,7 @@ import { useHead } from '@vueuse/head'
|
|||||||
* Alternatively, for reactive meta state, you can pass in a function
|
* Alternatively, for reactive meta state, you can pass in a function
|
||||||
* that returns a meta object.
|
* that returns a meta object.
|
||||||
*/
|
*/
|
||||||
export function useMeta (meta: Record<string, any> | ComputedGetter<any>) {
|
export function useMeta (meta: MetaObject | ComputedGetter<MetaObject>) {
|
||||||
// TODO: refine @nuxt/meta API
|
const resolvedMeta = isFunction(meta) ? computed(meta) : meta
|
||||||
|
useNuxt()._useMeta(resolvedMeta)
|
||||||
// At the moment we force all interaction to happen through passing in
|
|
||||||
// the meta object or function that returns a meta object.
|
|
||||||
const source = isFunction(meta) ? computed(meta) : meta
|
|
||||||
|
|
||||||
// `vue-meta`
|
|
||||||
// useVueMeta(source)
|
|
||||||
|
|
||||||
// `@vueuse/head`
|
|
||||||
useHead(source)
|
|
||||||
}
|
}
|
||||||
|
@ -1,12 +1,15 @@
|
|||||||
import { defineNuxtPlugin } from '@nuxt/app'
|
import { defineNuxtPlugin } from '@nuxt/app'
|
||||||
import { createApp } from 'vue'
|
import { createApp } from 'vue'
|
||||||
import { createMetaManager } from 'vue-meta'
|
import { createMetaManager } from 'vue-meta'
|
||||||
|
import type { MetaObject } from '@nuxt/meta'
|
||||||
|
|
||||||
export default defineNuxtPlugin((nuxt) => {
|
export default defineNuxtPlugin((nuxt) => {
|
||||||
const manager = createMetaManager(process.server)
|
const manager = createMetaManager(process.server)
|
||||||
|
|
||||||
nuxt.app.use(manager)
|
nuxt.app.use(manager)
|
||||||
|
|
||||||
|
nuxt._useMeta = (meta: MetaObject) => manager.addMeta(meta)
|
||||||
|
|
||||||
if (process.client) {
|
if (process.client) {
|
||||||
const teleportTarget = document.createElement('div')
|
const teleportTarget = document.createElement('div')
|
||||||
teleportTarget.id = 'head-target'
|
teleportTarget.id = 'head-target'
|
@ -1,11 +1,15 @@
|
|||||||
import { createHead, renderHeadToString } from '@vueuse/head'
|
import { createHead, renderHeadToString } from '@vueuse/head'
|
||||||
import { defineNuxtPlugin } from '@nuxt/app'
|
import { defineNuxtPlugin } from '@nuxt/app'
|
||||||
|
import { ref } from 'vue'
|
||||||
|
import type { MetaObject } from '@nuxt/meta'
|
||||||
|
|
||||||
export default defineNuxtPlugin((nuxt) => {
|
export default defineNuxtPlugin((nuxt) => {
|
||||||
const head = createHead()
|
const head = createHead()
|
||||||
|
|
||||||
nuxt.app.use(head)
|
nuxt.app.use(head)
|
||||||
|
|
||||||
|
nuxt._useMeta = (meta: MetaObject) => head.addHeadObjs(ref(meta as any))
|
||||||
|
|
||||||
if (process.server) {
|
if (process.server) {
|
||||||
nuxt.ssrContext.renderMeta = () => renderHeadToString(head)
|
nuxt.ssrContext.renderMeta = () => renderHeadToString(head)
|
||||||
}
|
}
|
@ -1,9 +1,13 @@
|
|||||||
import { getCurrentInstance } from 'vue'
|
import { getCurrentInstance } from 'vue'
|
||||||
import { defineNuxtPlugin } from '@nuxt/app'
|
import { defineNuxtPlugin } from '@nuxt/app'
|
||||||
import * as Components from './components'
|
import * as Components from './components'
|
||||||
import { useMeta } from './index'
|
import { useMeta } from './composables'
|
||||||
|
// @ts-ignore
|
||||||
|
import metaConfig from '#build/meta.config.mjs'
|
||||||
|
|
||||||
export default defineNuxtPlugin((nuxt) => {
|
export default defineNuxtPlugin((nuxt) => {
|
||||||
|
useMeta(metaConfig.globalMeta)
|
||||||
|
|
||||||
nuxt.app.mixin({
|
nuxt.app.mixin({
|
||||||
created () {
|
created () {
|
||||||
const instance = getCurrentInstance()
|
const instance = getCurrentInstance()
|
23
packages/meta/types.d.ts
vendored
Normal file
23
packages/meta/types.d.ts
vendored
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
export * from '@nuxt/meta/dist/runtime/composables'
|
||||||
|
|
||||||
|
export interface MetaObject extends Record<string, any> {
|
||||||
|
/**
|
||||||
|
* The character encoding in which the document is encoded => `<meta charset="<value>" />`
|
||||||
|
*
|
||||||
|
* @default `'utf-8'`
|
||||||
|
*/
|
||||||
|
charset?: string
|
||||||
|
/**
|
||||||
|
* Configuration of the viewport (the area of the window in which web content can be seen),
|
||||||
|
* mapped to => `<meta name="viewport" content="<value>" />`
|
||||||
|
*
|
||||||
|
* @default `'width=device-width, initial-scale=1'`
|
||||||
|
*/
|
||||||
|
viewport?: string
|
||||||
|
}
|
||||||
|
|
||||||
|
declare module '@nuxt/kit' {
|
||||||
|
interface NuxtConfig {
|
||||||
|
meta?: MetaObject,
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user