chore: numerous type issues

This commit is contained in:
harlan 2025-01-21 17:04:53 +11:00
parent 470e1f1c02
commit 9807edb356
16 changed files with 151 additions and 211 deletions

View File

@ -44,9 +44,9 @@
"@nuxt/vite-builder": "workspace:*", "@nuxt/vite-builder": "workspace:*",
"@nuxt/webpack-builder": "workspace:*", "@nuxt/webpack-builder": "workspace:*",
"@types/node": "22.10.7", "@types/node": "22.10.7",
"@unhead/schema": "2.0.0-alpha.3", "@unhead/schema": "2.0.0-alpha.4",
"@unhead/vue": "2.0.0-alpha.3", "@unhead/vue": "2.0.0-alpha.4",
"@unhead/shared": "2.0.0-alpha.3", "@unhead/shared": "2.0.0-alpha.4",
"@vue/compiler-core": "3.5.13", "@vue/compiler-core": "3.5.13",
"@vue/compiler-dom": "3.5.13", "@vue/compiler-dom": "3.5.13",
"@vue/shared": "3.5.13", "@vue/shared": "3.5.13",
@ -63,7 +63,7 @@
"typescript": "5.7.3", "typescript": "5.7.3",
"ufo": "1.5.4", "ufo": "1.5.4",
"unbuild": "3.3.1", "unbuild": "3.3.1",
"unhead": "2.0.0-alpha.3", "unhead": "2.0.0-alpha.4",
"unimport": "3.14.6", "unimport": "3.14.6",
"vite": "6.0.9", "vite": "6.0.9",
"vue": "3.5.13" "vue": "3.5.13"
@ -83,8 +83,8 @@
"@types/babel__helper-plugin-utils": "7.10.3", "@types/babel__helper-plugin-utils": "7.10.3",
"@types/node": "22.10.7", "@types/node": "22.10.7",
"@types/semver": "7.5.8", "@types/semver": "7.5.8",
"@unhead/schema": "2.0.0-alpha.3", "@unhead/schema": "2.0.0-alpha.4",
"@unhead/vue": "2.0.0-alpha.3", "@unhead/vue": "2.0.0-alpha.4",
"@vitest/coverage-v8": "3.0.2", "@vitest/coverage-v8": "3.0.2",
"@vue/test-utils": "2.4.6", "@vue/test-utils": "2.4.6",
"acorn": "8.14.0", "acorn": "8.14.0",

View File

@ -71,7 +71,7 @@
"@nuxt/schema": "workspace:*", "@nuxt/schema": "workspace:*",
"@nuxt/telemetry": "^2.6.4", "@nuxt/telemetry": "^2.6.4",
"@nuxt/vite-builder": "workspace:*", "@nuxt/vite-builder": "workspace:*",
"@unhead/vue": "^2.0.0-alpha.3", "@unhead/vue": "^2.0.0-alpha.4",
"@vue/shared": "^3.5.13", "@vue/shared": "^3.5.13",
"acorn": "8.14.0", "acorn": "8.14.0",
"c12": "^2.0.1", "c12": "^2.0.1",

View File

@ -3,14 +3,15 @@ import { Fragment, Teleport, computed, createStaticVNode, createVNode, defineCom
import { debounce } from 'perfect-debounce' import { debounce } from 'perfect-debounce'
import { hash } from 'ohash' import { hash } from 'ohash'
import { appendResponseHeader } from 'h3' import { appendResponseHeader } from 'h3'
import { type ActiveHeadEntry, type Head, injectHead } from '@unhead/vue'
import { randomUUID } from 'uncrypto' import { randomUUID } from 'uncrypto'
import { joinURL, withQuery } from 'ufo' import { joinURL, withQuery } from 'ufo'
import type { FetchResponse } from 'ofetch' import type { FetchResponse } from 'ofetch'
import type { ActiveHeadEntry, ResolvedHead } from '@unhead/vue/types'
import type { NuxtIslandResponse } from '../types' import type { NuxtIslandResponse } from '../types'
import { useNuxtApp, useRuntimeConfig } from '../nuxt' import { useNuxtApp, useRuntimeConfig } from '../nuxt'
import { prerenderRoutes, useRequestEvent } from '../composables/ssr' import { prerenderRoutes, useRequestEvent } from '../composables/ssr'
import { injectHead } from '../composables/head'
import { getFragmentHTML } from './utils' import { getFragmentHTML } from './utils'
// @ts-expect-error virtual file // @ts-expect-error virtual file
@ -90,7 +91,7 @@ export default defineComponent({
const instance = getCurrentInstance()! const instance = getCurrentInstance()!
const event = useRequestEvent() const event = useRequestEvent()
let activeHead: ActiveHeadEntry<Head> let activeHead: ActiveHeadEntry<Partial<ResolvedHead>>
// TODO: remove use of `$fetch.raw` when nitro 503 issues on windows dev server are resolved // TODO: remove use of `$fetch.raw` when nitro 503 issues on windows dev server are resolved
const eventFetch = import.meta.server ? event!.fetch : import.meta.dev ? $fetch.raw : globalThis.fetch const eventFetch = import.meta.server ? event!.fetch : import.meta.dev ? $fetch.raw : globalThis.fetch

View File

@ -0,0 +1,78 @@
import type { UseHeadInput, UseHeadOptions, UseHeadSafeInput, UseSeoMetaInput, VueHeadClient } from '@unhead/vue'
import type { ActiveHeadEntry, MergeHead } from '@unhead/schema'
import { hasInjectionContext, inject } from 'vue'
import {
useHead as headCore,
useHeadSafe as headSafe,
headSymbol,
useSeoMeta as seoMeta, useServerHead as serverHead, useServerHeadSafe as serverHeadSafe,
useServerSeoMeta as serverSeoMeta,
} from '@unhead/vue'
import { tryUseNuxtApp, useNuxtApp } from '#app'
import type { NuxtApp } from '#app'
// @ts-expect-error build-time
import { isNuxt4 } from '#build/nuxt.config.mjs'
function resolveUnheadInject (): VueHeadClient<MergeHead> | undefined {
// try use Vue inject
if (hasInjectionContext()) {
return inject<VueHeadClient<MergeHead>>(headSymbol)!
}
}
/**
* Injects the head client from the Nuxt context or Vue inject.
*
* In Nuxt v3 this function will not throw an error if the context is missing.
*/
export function injectHead (nuxtApp?: NuxtApp): VueHeadClient<MergeHead> {
// Nuxt 4 will throw an error if the context is missing
const nuxt = nuxtApp || (isNuxt4 ? useNuxtApp() : tryUseNuxtApp())
return nuxt?.ssrContext?.head || nuxt?.runWithContext(resolveUnheadInject) as VueHeadClient<MergeHead> || resolveUnheadInject()
}
interface NuxtUseHeadOptions extends UseHeadOptions {
nuxt?: NuxtApp
}
export function useHead<T extends MergeHead> (input: UseHeadInput<T>, options: NuxtUseHeadOptions = {}): ActiveHeadEntry<UseHeadInput<T>> | void {
const head = injectHead(options.nuxt)
if (isNuxt4 || head) {
return headCore(input, { head, ...options }) as ActiveHeadEntry<UseHeadInput<T>>
}
}
export function useHeadSafe<T extends MergeHead> (input: UseHeadSafeInput, options: NuxtUseHeadOptions = {}): ActiveHeadEntry<UseHeadInput<T>> | void {
const head = injectHead(options.nuxt)
if (isNuxt4 || head) {
return headSafe(input, { head, ...options }) as ActiveHeadEntry<UseHeadInput>
}
}
export function useSeoMeta (input: UseSeoMetaInput, options: NuxtUseHeadOptions = {}): ActiveHeadEntry<UseSeoMetaInput> | void {
const head = injectHead(options.nuxt)
if (isNuxt4 || head) {
return seoMeta(input, { head, ...options }) as ActiveHeadEntry<UseHeadInput>
}
}
export function useServerHead<T extends MergeHead> (input: UseHeadInput<T>, options: NuxtUseHeadOptions = {}): ActiveHeadEntry<UseHeadInput<T>> | void {
const head = injectHead(options.nuxt)
if (isNuxt4 || head) {
return serverHead(input, { head, ...options }) as ActiveHeadEntry<UseHeadInput<T>>
}
}
export function useServerHeadSafe (input: UseHeadSafeInput, options: NuxtUseHeadOptions = {}): ActiveHeadEntry<UseSeoMetaInput> | void {
const head = injectHead(options.nuxt)
if (isNuxt4 || head) {
return serverHeadSafe(input, { head, ...options }) as ActiveHeadEntry<UseHeadInput>
}
}
export function useServerSeoMeta (input: UseSeoMetaInput, options: NuxtUseHeadOptions = {}): ActiveHeadEntry<UseHeadInput> | void {
const head = injectHead(options.nuxt)
if (isNuxt4 || head) {
return serverSeoMeta(input, { head, ...options }) as ActiveHeadEntry<UseHeadInput>
}
}

View File

@ -26,3 +26,4 @@ export { useId } from './id'
export { useRouteAnnouncer } from './route-announcer' export { useRouteAnnouncer } from './route-announcer'
export type { Politeness } from './route-announcer' export type { Politeness } from './route-announcer'
export { useRuntimeHook } from './runtime-hook' export { useRuntimeHook } from './runtime-hook'
export { injectHead, useHead, useHeadSafe, useSeoMeta, useServerHead, useServerHeadSafe, useServerSeoMeta } from './head'

View File

@ -1,4 +1,4 @@
import type { UseScriptInput } from '@unhead/vue' import type { UseScriptInput, UseScriptOptions } from '@unhead/vue/legacy'
import { createError } from './error' import { createError } from './error'
function renderStubMessage (name: string) { function renderStubMessage (name: string) {
@ -13,7 +13,7 @@ function renderStubMessage (name: string) {
} }
// eslint-disable-next-line @typescript-eslint/no-unused-vars // eslint-disable-next-line @typescript-eslint/no-unused-vars
export function useScript<T extends Record<string | symbol, any>> (input: UseScriptInput, options?: Record<string, unknown>) { export function useScript<T extends Record<string | symbol, any>> (input: UseScriptInput, options?: UseScriptOptions) {
renderStubMessage('useScript') renderStubMessage('useScript')
} }

View File

@ -1,7 +1,7 @@
export { applyPlugin, applyPlugins, callWithNuxt, createNuxtApp, defineAppConfig, defineNuxtPlugin, definePayloadPlugin, isNuxtPlugin, registerPluginHooks, tryUseNuxtApp, useNuxtApp, useRuntimeConfig } from './nuxt' export { applyPlugin, applyPlugins, callWithNuxt, createNuxtApp, defineAppConfig, defineNuxtPlugin, definePayloadPlugin, isNuxtPlugin, registerPluginHooks, tryUseNuxtApp, useNuxtApp, useRuntimeConfig } from './nuxt'
export type { CreateOptions, NuxtApp, NuxtPayload, NuxtPluginIndicator, NuxtSSRContext, ObjectPlugin, Plugin, PluginEnvContext, PluginMeta, ResolvedPluginMeta, RuntimeNuxtHooks } from './nuxt' export type { CreateOptions, NuxtApp, NuxtPayload, NuxtPluginIndicator, NuxtSSRContext, ObjectPlugin, Plugin, PluginEnvContext, PluginMeta, ResolvedPluginMeta, RuntimeNuxtHooks } from './nuxt'
export { defineNuxtComponent, useAsyncData, useLazyAsyncData, useNuxtData, refreshNuxtData, clearNuxtData, useHydration, callOnce, useState, clearNuxtState, clearError, createError, isNuxtError, showError, useError, useFetch, useLazyFetch, useCookie, refreshCookie, onPrehydrate, prerenderRoutes, useRequestHeaders, useRequestEvent, useRequestFetch, setResponseStatus, useResponseHeader, onNuxtReady, abortNavigation, addRouteMiddleware, defineNuxtRouteMiddleware, onBeforeRouteLeave, onBeforeRouteUpdate, setPageLayout, navigateTo, useRoute, useRouter, preloadComponents, prefetchComponents, preloadRouteComponents, isPrerendered, loadPayload, preloadPayload, definePayloadReducer, definePayloadReviver, getAppManifest, getRouteRules, reloadNuxtApp, useRequestURL, usePreviewMode, useId, useRouteAnnouncer, useHead, useSeoMeta, useServerSeoMeta, useRuntimeHook } from './composables/index' export { useHead, useHeadSafe, useServerSeoMeta, useServerHeadSafe, useServerHead, useSeoMeta, defineNuxtComponent, useAsyncData, useLazyAsyncData, useNuxtData, refreshNuxtData, clearNuxtData, useHydration, callOnce, useState, clearNuxtState, clearError, createError, isNuxtError, showError, useError, useFetch, useLazyFetch, useCookie, refreshCookie, onPrehydrate, prerenderRoutes, useRequestHeaders, useRequestEvent, useRequestFetch, setResponseStatus, useResponseHeader, onNuxtReady, abortNavigation, addRouteMiddleware, defineNuxtRouteMiddleware, onBeforeRouteLeave, onBeforeRouteUpdate, setPageLayout, navigateTo, useRoute, useRouter, preloadComponents, prefetchComponents, preloadRouteComponents, isPrerendered, loadPayload, preloadPayload, definePayloadReducer, definePayloadReviver, getAppManifest, getRouteRules, reloadNuxtApp, useRequestURL, usePreviewMode, useId, useRouteAnnouncer, injectHead, useRuntimeHook } from './composables/index'
export type { AddRouteMiddlewareOptions, AsyncData, AsyncDataOptions, AsyncDataRequestStatus, CookieOptions, CookieRef, FetchResult, NuxtAppManifest, NuxtAppManifestMeta, NuxtError, Politeness, ReloadNuxtAppOptions, RouteMiddleware, UseFetchOptions } from './composables/index' export type { AddRouteMiddlewareOptions, AsyncData, AsyncDataOptions, AsyncDataRequestStatus, CookieOptions, CookieRef, FetchResult, NuxtAppManifest, NuxtAppManifestMeta, NuxtError, Politeness, ReloadNuxtAppOptions, RouteMiddleware, UseFetchOptions } from './composables/index'
export { defineNuxtLink } from './components/index' export { defineNuxtLink } from './components/index'

View File

@ -17,8 +17,7 @@ import destr from 'destr'
import { getQuery as getURLQuery, joinURL, withoutTrailingSlash } from 'ufo' import { getQuery as getURLQuery, joinURL, withoutTrailingSlash } from 'ufo'
import { renderToString as _renderToString } from 'vue/server-renderer' import { renderToString as _renderToString } from 'vue/server-renderer'
import { createHead as createServerHead, propsToString, renderSSRHead } from '@unhead/vue/server' import { createHead as createServerHead, propsToString, renderSSRHead } from '@unhead/vue/server'
import type { Head, HeadEntryOptions } from '@unhead/schema' import type { Head, HeadEntryOptions, Link, ResolvedHead, Script, Style } from '@unhead/vue/types'
import type { Link, Script, Style } from '@unhead/vue'
import { resolveUnrefHeadInput } from '@unhead/vue' import { resolveUnrefHeadInput } from '@unhead/vue'
import { defineRenderHandler, getRouteRules, useNitroApp, useRuntimeConfig, useStorage } from 'nitro/runtime' import { defineRenderHandler, getRouteRules, useNitroApp, useRuntimeConfig, useStorage } from 'nitro/runtime'
@ -78,7 +77,7 @@ export interface NuxtIslandContext {
export interface NuxtIslandResponse { export interface NuxtIslandResponse {
id?: string id?: string
html: string html: string
head: Head head: Partial<ResolvedHead>
props?: Record<string, Record<string, any>> props?: Record<string, Record<string, any>>
components?: Record<string, NuxtIslandClientResponse> components?: Record<string, NuxtIslandClientResponse>
slots?: Record<string, NuxtIslandSlotResponse> slots?: Record<string, NuxtIslandSlotResponse>
@ -474,7 +473,7 @@ export default defineRenderHandler(async (event): Promise<Partial<RenderResponse
// Response for component islands // Response for component islands
if (isRenderingIsland && islandContext) { if (isRenderingIsland && islandContext) {
const islandHead: Head = {} const islandHead: Partial<ResolvedHead> = {}
for (const entry of head.headEntries()) { for (const entry of head.headEntries()) {
for (const [key, value] of Object.entries(resolveUnrefHeadInput(entry.input) as Head)) { for (const [key, value] of Object.entries(resolveUnrefHeadInput(entry.input) as Head)) {
const currentValue = islandHead[key as keyof Head] const currentValue = islandHead[key as keyof Head]

View File

@ -525,6 +525,7 @@ export const nuxtConfigTemplate: NuxtTemplate = {
`export const chunkErrorEvent = ${ctx.nuxt.options.experimental.emitRouteChunkError ? ctx.nuxt.options.builder === '@nuxt/vite-builder' ? '"vite:preloadError"' : '"nuxt:preloadError"' : 'false'}`, `export const chunkErrorEvent = ${ctx.nuxt.options.experimental.emitRouteChunkError ? ctx.nuxt.options.builder === '@nuxt/vite-builder' ? '"vite:preloadError"' : '"nuxt:preloadError"' : 'false'}`,
`export const crawlLinks = ${!!((ctx.nuxt as any)._nitro as Nitro).options.prerender.crawlLinks}`, `export const crawlLinks = ${!!((ctx.nuxt as any)._nitro as Nitro).options.prerender.crawlLinks}`,
`export const spaLoadingTemplateOutside = ${ctx.nuxt.options.experimental.spaLoadingTemplateLocation === 'body'}`, `export const spaLoadingTemplateOutside = ${ctx.nuxt.options.experimental.spaLoadingTemplateLocation === 'body'}`,
`export const isNuxt4 = ${ctx.nuxt.options.future.compatibilityVersion === 4}`,
].join('\n\n') ].join('\n\n')
}, },
} }

View File

@ -42,7 +42,7 @@ export default defineNuxtModule<NuxtOptions['unhead']>({
const exportPath = resolve(runtimeDir, 'exports', isNuxtV4 ? 'v4' : 'v3') const exportPath = resolve(runtimeDir, 'exports', isNuxtV4 ? 'v4' : 'v3')
nuxt.options.alias['#unhead/exports'] = exportPath nuxt.options.alias['#unhead/exports'] = exportPath
addImportsSources({ addImportsSources({
from: exportPath, from: '#app',
imports: unheadVueComposablesImports['@unhead/vue'], imports: unheadVueComposablesImports['@unhead/vue'],
}) })

View File

@ -1,6 +1,5 @@
import { defineComponent } from 'vue' import { computed, defineComponent } from 'vue'
import type { PropType, SetupContext } from 'vue' import type { PropType, SetupContext } from 'vue'
import { useHead } from '@unhead/vue'
import type { import type {
CrossOrigin, CrossOrigin,
FetchPriority, FetchPriority,
@ -10,6 +9,7 @@ import type {
ReferrerPolicy, ReferrerPolicy,
Target, Target,
} from './types' } from './types'
import { useHead } from '#app/composables/head'
const removeUndefinedProps = (props: Props) => { const removeUndefinedProps = (props: Props) => {
const filteredProps = Object.create(null) const filteredProps = Object.create(null)
@ -23,7 +23,7 @@ const removeUndefinedProps = (props: Props) => {
} }
const setupForUseMeta = (metaFactory: (props: Props, ctx: SetupContext) => Record<string, any>, renderChild?: boolean) => (props: Props, ctx: SetupContext) => { const setupForUseMeta = (metaFactory: (props: Props, ctx: SetupContext) => Record<string, any>, renderChild?: boolean) => (props: Props, ctx: SetupContext) => {
useHead(() => metaFactory({ ...removeUndefinedProps(props), ...ctx.attrs }, ctx)) useHead(computed(() => metaFactory({ ...removeUndefinedProps(props), ...ctx.attrs }, ctx)))
return () => renderChild ? ctx.slots.default?.() : null return () => renderChild ? ctx.slots.default?.() : null
} }

View File

@ -0,0 +1,17 @@
export {
injectHead,
useHead,
useHeadSafe,
useSeoMeta,
useServerHead,
useServerHeadSafe,
useServerSeoMeta,
} from '#app'
export {
createHeadCore,
resolveUnrefHeadInput,
unheadVueComposablesImports,
} from '@unhead/vue'
export * from '@unhead/vue/types'

View File

@ -1,92 +0,0 @@
import type { ActiveHeadEntry, MergeHead } from '@unhead/schema'
import type { UseHeadInput, UseHeadOptions, UseHeadSafeInput, UseSeoMetaInput,
VueHeadClient } from '@unhead/vue'
import {
useHead as head,
useHeadSafe as headSafe,
useSeoMeta as seoMeta,
useServerHead as serverHead,
useServerHeadSafe as serverHeadSafe,
useServerSeoMeta as serverSeoMeta,
} from '@unhead/vue'
import type { UseScriptInput, UseScriptOptions, UseScriptReturn } from '@unhead/vue/legacy'
import {
injectHead as inject,
useScript as script,
} from '@unhead/vue/legacy'
import { tryUseNuxtApp } from '#app'
import type { NuxtApp } from '#app'
export * from '@unhead/vue/legacy'
interface NuxtUseHeadOptions extends UseHeadOptions {
nuxt?: NuxtApp
}
/**
* Injects the head client from the Nuxt context or Vue inject.
*
* In Nuxt v3 this function will not throw an error if the context is missing.
*/
export function injectHead (nuxtApp?: NuxtApp): VueHeadClient<MergeHead> | undefined {
const nuxt = nuxtApp || tryUseNuxtApp()
if (nuxt?.ssrContext?.head) {
return nuxt?.ssrContext?.head
}
if (nuxt) {
return nuxt.runWithContext(inject) as VueHeadClient<MergeHead>
}
// try use Vue inject
return inject()
}
export function useHead<T extends MergeHead> (input: UseHeadInput<T>, options: NuxtUseHeadOptions = {}): ActiveHeadEntry<UseHeadInput<T>> | void {
const unhead = injectHead(options.nuxt)
if (unhead) {
return head(input, { head: unhead, ...options }) as ActiveHeadEntry<UseHeadInput<T>>
}
}
export function useHeadSafe<T extends MergeHead> (input: UseHeadSafeInput, options: NuxtUseHeadOptions = {}): ActiveHeadEntry<UseHeadInput<T>> | void {
const unhead = injectHead(options.nuxt)
if (unhead) {
return headSafe(input, { head: unhead, ...options }) as ActiveHeadEntry<UseHeadInput>
}
}
export function useSeoMeta (input: UseSeoMetaInput, options: NuxtUseHeadOptions = {}): ActiveHeadEntry<UseSeoMetaInput> | void {
const unhead = injectHead(options.nuxt)
if (unhead) {
return seoMeta(input, { head: unhead, ...options }) as ActiveHeadEntry<UseHeadInput>
}
}
export function useServerHead<T extends MergeHead> (input: UseHeadInput<T>, options: NuxtUseHeadOptions = {}): ActiveHeadEntry<UseHeadInput<T>> | void {
const unhead = injectHead(options.nuxt)
if (unhead) {
return serverHead(input, { head: unhead, ...options }) as ActiveHeadEntry<UseHeadInput<T>>
}
}
export function useServerHeadSafe (input: UseHeadSafeInput, options: NuxtUseHeadOptions = {}): ActiveHeadEntry<UseSeoMetaInput> | void {
const unhead = injectHead(options.nuxt)
if (unhead) {
return serverHeadSafe(input, { head: unhead, ...options }) as ActiveHeadEntry<UseHeadInput>
}
}
export function useServerSeoMeta<T extends MergeHead> (input: UseSeoMetaInput, options: NuxtUseHeadOptions = {}): ActiveHeadEntry<UseHeadInput<T>> | void {
const unhead = injectHead(options.nuxt)
if (unhead) {
return serverSeoMeta(input, { head: unhead, ...options }) as ActiveHeadEntry<UseHeadInput>
}
}
/**
* Aliased for users doing `import { useScript } from '@unhead/vue'`
* @deprecated This will be removed in Nuxt v4. Use `useScript` exported from `@unhead/scripts/vue`
*/
export function useScript<T extends Record<symbol | string, any> = Record<symbol | string, any>> (input: UseScriptInput, options?: UseScriptOptions<T> & { nuxt?: NuxtApp }): UseScriptReturn<T> | void {
const unhead = injectHead(options?.nuxt)
return script(input, { head: unhead, ...options })
}

View File

@ -1,58 +0,0 @@
import type { ActiveHeadEntry, MergeHead } from '@unhead/schema'
import type { UseHeadInput, UseHeadOptions, UseHeadSafeInput, UseSeoMetaInput, VueHeadClient } from '@unhead/vue'
import { useHead as head, useHeadSafe as headSafe, injectHead as inject, useSeoMeta as seoMeta, useServerHead as serverHead, useServerHeadSafe as serverHeadSafe, useServerSeoMeta as serverSeoMeta } from '@unhead/vue'
import type { NuxtApp } from 'nuxt/app'
import { useNuxtApp } from 'nuxt/app'
export * from '@unhead/vue'
interface NuxtUseHeadOptions extends UseHeadOptions {
nuxt?: NuxtApp
}
/**
* Injects the head client from the Nuxt context or Vue inject.
*
* In Nuxt v3 this function will not throw an error if the context is missing.
*/
export function injectHead (nuxtApp?: NuxtApp): VueHeadClient<MergeHead> {
const nuxt = nuxtApp || useNuxtApp()
if (nuxt?.ssrContext?.head) {
return nuxt?.ssrContext?.head
}
if (nuxt) {
return nuxt.runWithContext(inject) as VueHeadClient<MergeHead>
}
// try use Vue inject
return inject()
}
export function useHead<T extends MergeHead> (input: UseHeadInput<T>, options: NuxtUseHeadOptions = {}): ActiveHeadEntry<UseHeadInput<T>> | void {
const unhead = injectHead(options.nuxt)
return head(input, { head: unhead, ...options }) as ActiveHeadEntry<UseHeadInput<T>>
}
export function useHeadSafe<T extends MergeHead> (input: UseHeadSafeInput, options: NuxtUseHeadOptions = {}): ActiveHeadEntry<UseHeadInput<T>> | void {
const unhead = injectHead(options.nuxt)
return headSafe(input, { head: unhead, ...options }) as ActiveHeadEntry<UseHeadInput>
}
export function useSeoMeta (input: UseSeoMetaInput, options: NuxtUseHeadOptions = {}): ActiveHeadEntry<UseSeoMetaInput> | void {
const unhead = injectHead(options.nuxt)
return seoMeta(input, { head: unhead, ...options }) as ActiveHeadEntry<UseHeadInput>
}
export function useServerHead<T extends MergeHead> (input: UseHeadInput<T>, options: NuxtUseHeadOptions = {}): ActiveHeadEntry<UseHeadInput<T>> | void {
const unhead = injectHead(options.nuxt)
return serverHead(input, { head: unhead, ...options }) as ActiveHeadEntry<UseHeadInput<T>>
}
export function useServerHeadSafe (input: UseHeadSafeInput, options: NuxtUseHeadOptions = {}): ActiveHeadEntry<UseSeoMetaInput> | void {
const unhead = injectHead(options.nuxt)
return serverHeadSafe(input, { head: unhead, ...options }) as ActiveHeadEntry<UseHeadInput>
}
export function useServerSeoMeta<T extends MergeHead> (input: UseSeoMetaInput, options: NuxtUseHeadOptions = {}): ActiveHeadEntry<UseHeadInput<T>> | void {
const unhead = injectHead(options.nuxt)
return serverSeoMeta(input, { head: unhead, ...options }) as ActiveHeadEntry<UseHeadInput>
}

View File

@ -37,7 +37,7 @@
}, },
"devDependencies": { "devDependencies": {
"@types/pug": "2.0.10", "@types/pug": "2.0.10",
"@unhead/schema": "2.0.0-alpha.3", "@unhead/schema": "2.0.0-alpha.4",
"@vitejs/plugin-vue": "5.2.1", "@vitejs/plugin-vue": "5.2.1",
"@vitejs/plugin-vue-jsx": "4.1.1", "@vitejs/plugin-vue-jsx": "4.1.1",
"@vue/compiler-core": "3.5.13", "@vue/compiler-core": "3.5.13",

View File

@ -12,9 +12,9 @@ overrides:
'@nuxt/vite-builder': workspace:* '@nuxt/vite-builder': workspace:*
'@nuxt/webpack-builder': workspace:* '@nuxt/webpack-builder': workspace:*
'@types/node': 22.10.7 '@types/node': 22.10.7
'@unhead/schema': 2.0.0-alpha.3 '@unhead/schema': 2.0.0-alpha.4
'@unhead/vue': 2.0.0-alpha.3 '@unhead/vue': 2.0.0-alpha.4
'@unhead/shared': 2.0.0-alpha.3 '@unhead/shared': 2.0.0-alpha.4
'@vue/compiler-core': 3.5.13 '@vue/compiler-core': 3.5.13
'@vue/compiler-dom': 3.5.13 '@vue/compiler-dom': 3.5.13
'@vue/shared': 3.5.13 '@vue/shared': 3.5.13
@ -31,7 +31,7 @@ overrides:
typescript: 5.7.3 typescript: 5.7.3
ufo: 1.5.4 ufo: 1.5.4
unbuild: 3.3.1 unbuild: 3.3.1
unhead: 2.0.0-alpha.3 unhead: 2.0.0-alpha.4
unimport: 3.14.6 unimport: 3.14.6
vite: 6.0.9 vite: 6.0.9
vue: 3.5.13 vue: 3.5.13
@ -83,11 +83,11 @@ importers:
specifier: 7.5.8 specifier: 7.5.8
version: 7.5.8 version: 7.5.8
'@unhead/schema': '@unhead/schema':
specifier: 2.0.0-alpha.3 specifier: 2.0.0-alpha.4
version: 2.0.0-alpha.3 version: 2.0.0-alpha.4
'@unhead/vue': '@unhead/vue':
specifier: 2.0.0-alpha.3 specifier: 2.0.0-alpha.4
version: 2.0.0-alpha.3(vue@3.5.13(typescript@5.7.3)) version: 2.0.0-alpha.4(vue@3.5.13(typescript@5.7.3))
'@vitest/coverage-v8': '@vitest/coverage-v8':
specifier: 3.0.2 specifier: 3.0.2
version: 3.0.2(vitest@3.0.2(@types/node@22.10.7)(happy-dom@16.6.0)(jiti@2.4.2)(terser@5.32.0)(tsx@4.19.2)(yaml@2.6.1)) version: 3.0.2(vitest@3.0.2(@types/node@22.10.7)(happy-dom@16.6.0)(jiti@2.4.2)(terser@5.32.0)(tsx@4.19.2)(yaml@2.6.1))
@ -339,8 +339,8 @@ importers:
specifier: 22.10.7 specifier: 22.10.7
version: 22.10.7 version: 22.10.7
'@unhead/vue': '@unhead/vue':
specifier: 2.0.0-alpha.3 specifier: 2.0.0-alpha.4
version: 2.0.0-alpha.3(vue@3.5.13(typescript@5.7.3)) version: 2.0.0-alpha.4(vue@3.5.13(typescript@5.7.3))
'@vue/shared': '@vue/shared':
specifier: 3.5.13 specifier: 3.5.13
version: 3.5.13 version: 3.5.13
@ -684,8 +684,8 @@ importers:
specifier: 2.0.10 specifier: 2.0.10
version: 2.0.10 version: 2.0.10
'@unhead/schema': '@unhead/schema':
specifier: 2.0.0-alpha.3 specifier: 2.0.0-alpha.4
version: 2.0.0-alpha.3 version: 2.0.0-alpha.4
'@vitejs/plugin-vue': '@vitejs/plugin-vue':
specifier: 5.2.1 specifier: 5.2.1
version: 5.2.1(vite@6.0.9(@types/node@22.10.7)(jiti@2.4.2)(terser@5.32.0)(tsx@4.19.2)(yaml@2.6.1))(vue@3.5.13(typescript@5.7.3)) version: 5.2.1(vite@6.0.9(@types/node@22.10.7)(jiti@2.4.2)(terser@5.32.0)(tsx@4.19.2)(yaml@2.6.1))(vue@3.5.13(typescript@5.7.3))
@ -2842,14 +2842,14 @@ packages:
'@ungap/structured-clone@1.2.0': '@ungap/structured-clone@1.2.0':
resolution: {integrity: sha512-zuVdFrMJiuCDQUMCzQaD6KL28MjnqqN8XnAqiEq9PNm/hCPTSGfrXCOfwj1ow4LFb/tNymJPwsNbVePc1xFqrQ==} resolution: {integrity: sha512-zuVdFrMJiuCDQUMCzQaD6KL28MjnqqN8XnAqiEq9PNm/hCPTSGfrXCOfwj1ow4LFb/tNymJPwsNbVePc1xFqrQ==}
'@unhead/schema@2.0.0-alpha.3': '@unhead/schema@2.0.0-alpha.4':
resolution: {integrity: sha512-Q9xkdjtBNH0gBp0IzX2FIjRFencMlhQwZgm6L9HtPDtM1UEsOsHZq4f/EsaUyJj82py3aJrbVe6rQfTs1gPTjw==} resolution: {integrity: sha512-iy2bqEDLcqoiShxb2iPpU6DBeBPuTugEgcg7Crixc2QZMRSPKYfjN2q0T5nRDJxQhOfuI7pEAFWUZ2prI6B4gw==}
'@unhead/shared@2.0.0-alpha.3': '@unhead/shared@2.0.0-alpha.4':
resolution: {integrity: sha512-fiqKptv3VrjVASoMGMsLyKAFiVPyArPPr6VXDkhAPVlu1twvs58AhuWF6GJaJr2XJCnamPBj88rUVFO0x4E/9g==} resolution: {integrity: sha512-1qXRbFGncCon7HBBX0wSVjZ8Y43kT6OnfXFcLNdNO5lpOZqrY9VCj+jqCpGPXGGtw+FAqzAk5EVw5fVVD/WzUw==}
'@unhead/vue@2.0.0-alpha.3': '@unhead/vue@2.0.0-alpha.4':
resolution: {integrity: sha512-EpjNAykZ9XffZEPXxWp7iMAKrIordKjDfliaY8Wlz2fbsqbHnucTfTEvmJE+niFpA3pvjxJOd/atAME6MuYzbw==} resolution: {integrity: sha512-RyzNur7Ui2XHCV07aw2r4g4Tf7wpL18XxYiOB6tqli3ryf5K1W728N53x0TCMrjfQksByCaIogHUsvI+cKx+ZQ==}
peerDependencies: peerDependencies:
vue: 3.5.13 vue: 3.5.13
@ -7278,10 +7278,6 @@ packages:
resolution: {integrity: sha512-i11VH5gS6IFeLY3gMBQ00/MmLncVP7JLXOw1vlgkytLmJK7QnEr7NXf0LBdxfmNPAeyetukOk0bOYrJrFGjYJQ==} resolution: {integrity: sha512-i11VH5gS6IFeLY3gMBQ00/MmLncVP7JLXOw1vlgkytLmJK7QnEr7NXf0LBdxfmNPAeyetukOk0bOYrJrFGjYJQ==}
engines: {node: '>=14.0.0'} engines: {node: '>=14.0.0'}
tinypool@1.0.1:
resolution: {integrity: sha512-URZYihUbRPcGv95En+sz6MfghfIc2OJ1sv/RmhWZLouPY0/8Vo80viwPvg3dlaS9fuq7fQMEfgRRK7BBZThBEA==}
engines: {node: ^18.0.0 || >=20.0.0}
tinypool@1.0.2: tinypool@1.0.2:
resolution: {integrity: sha512-al6n+QEANGFOMf/dmUMsuS5/r9B06uwlyNjZZql/zv8J7ybHCgoihBNORZCY2mzUuAnomQa2JdhyHKzZxPCrFA==} resolution: {integrity: sha512-al6n+QEANGFOMf/dmUMsuS5/r9B06uwlyNjZZql/zv8J7ybHCgoihBNORZCY2mzUuAnomQa2JdhyHKzZxPCrFA==}
engines: {node: ^18.0.0 || >=20.0.0} engines: {node: ^18.0.0 || >=20.0.0}
@ -7436,8 +7432,8 @@ packages:
unenv@1.10.0: unenv@1.10.0:
resolution: {integrity: sha512-wY5bskBQFL9n3Eca5XnhH6KbUo/tfvkwm9OpcdCvLaeA7piBNbavbOKJySEwQ1V0RH6HvNlSAFRTpvTqgKRQXQ==} resolution: {integrity: sha512-wY5bskBQFL9n3Eca5XnhH6KbUo/tfvkwm9OpcdCvLaeA7piBNbavbOKJySEwQ1V0RH6HvNlSAFRTpvTqgKRQXQ==}
unhead@2.0.0-alpha.3: unhead@2.0.0-alpha.4:
resolution: {integrity: sha512-L9KBaikZL/aftapkfKW9jN4PEGv0czhodZvxHFv7JGD85WdWvVW9KJ2c74qvGhz2Y53JMjP83//O6ZwW/LhGoA==} resolution: {integrity: sha512-BEcPo1xyQMpJjLuh3fh68zQA26rOsfNi/HjheCUBsn59Y+EnqYC/I8NH1QjPNIeXH8snCI3/D0IzKj16qkhsBA==}
unicode-emoji-modifier-base@1.0.0: unicode-emoji-modifier-base@1.0.0:
resolution: {integrity: sha512-yLSH4py7oFH3oG/9K+XWrz1pSi3dfUrWEnInbxMfArOfc1+33BlGPQtLsOYwvdMy11AwUBetYuaRxSPqgkq+8g==} resolution: {integrity: sha512-yLSH4py7oFH3oG/9K+XWrz1pSi3dfUrWEnInbxMfArOfc1+33BlGPQtLsOYwvdMy11AwUBetYuaRxSPqgkq+8g==}
@ -9114,7 +9110,7 @@ snapshots:
'@types/google.maps': 3.58.1 '@types/google.maps': 3.58.1
'@types/vimeo__player': 2.18.3 '@types/vimeo__player': 2.18.3
'@types/youtube': 0.1.0 '@types/youtube': 0.1.0
'@unhead/vue': 2.0.0-alpha.3(vue@3.5.13(typescript@5.7.3)) '@unhead/vue': 2.0.0-alpha.4(vue@3.5.13(typescript@5.7.3))
'@vueuse/core': 11.1.0(vue@3.5.13(typescript@5.7.3)) '@vueuse/core': 11.1.0(vue@3.5.13(typescript@5.7.3))
consola: 3.4.0 consola: 3.4.0
defu: 6.1.4 defu: 6.1.4
@ -10070,22 +10066,22 @@ snapshots:
'@ungap/structured-clone@1.2.0': {} '@ungap/structured-clone@1.2.0': {}
'@unhead/schema@2.0.0-alpha.3': '@unhead/schema@2.0.0-alpha.4':
dependencies: dependencies:
hookable: 5.5.3 hookable: 5.5.3
zhead: 2.2.4 zhead: 2.2.4
'@unhead/shared@2.0.0-alpha.3': '@unhead/shared@2.0.0-alpha.4':
dependencies: dependencies:
'@unhead/schema': 2.0.0-alpha.3 '@unhead/schema': 2.0.0-alpha.4
packrup: 0.1.2 packrup: 0.1.2
'@unhead/vue@2.0.0-alpha.3(vue@3.5.13(typescript@5.7.3))': '@unhead/vue@2.0.0-alpha.4(vue@3.5.13(typescript@5.7.3))':
dependencies: dependencies:
'@unhead/schema': 2.0.0-alpha.3 '@unhead/schema': 2.0.0-alpha.4
'@unhead/shared': 2.0.0-alpha.3 '@unhead/shared': 2.0.0-alpha.4
hookable: 5.5.3 hookable: 5.5.3
unhead: 2.0.0-alpha.3 unhead: 2.0.0-alpha.4
vue: 3.5.13(typescript@5.7.3) vue: 3.5.13(typescript@5.7.3)
'@unocss/astro@0.62.4(rollup@4.31.0)(vite@6.0.9(@types/node@22.10.7)(jiti@2.4.2)(terser@5.32.0)(tsx@4.19.2)(yaml@2.6.1))': '@unocss/astro@0.62.4(rollup@4.31.0)(vite@6.0.9(@types/node@22.10.7)(jiti@2.4.2)(terser@5.32.0)(tsx@4.19.2)(yaml@2.6.1))':
@ -15440,9 +15436,6 @@ snapshots:
tinypool@0.8.4: {} tinypool@0.8.4: {}
tinypool@1.0.1:
optional: true
tinypool@1.0.2: {} tinypool@1.0.2: {}
tinyrainbow@1.2.0: tinyrainbow@1.2.0:
@ -15637,10 +15630,10 @@ snapshots:
node-fetch-native: 1.6.4 node-fetch-native: 1.6.4
pathe: 1.1.2 pathe: 1.1.2
unhead@2.0.0-alpha.3: unhead@2.0.0-alpha.4:
dependencies: dependencies:
'@unhead/schema': 2.0.0-alpha.3 '@unhead/schema': 2.0.0-alpha.4
'@unhead/shared': 2.0.0-alpha.3 '@unhead/shared': 2.0.0-alpha.4
hookable: 5.5.3 hookable: 5.5.3
unicode-emoji-modifier-base@1.0.0: {} unicode-emoji-modifier-base@1.0.0: {}
@ -15930,7 +15923,7 @@ snapshots:
dependencies: dependencies:
cac: 6.7.14 cac: 6.7.14
debug: 4.4.0(supports-color@9.4.0) debug: 4.4.0(supports-color@9.4.0)
es-module-lexer: 1.5.4 es-module-lexer: 1.6.0
pathe: 1.1.2 pathe: 1.1.2
vite: 6.0.9(@types/node@22.10.7)(jiti@2.4.2)(sass@1.78.0)(terser@5.32.0)(tsx@4.19.2)(yaml@2.6.1) vite: 6.0.9(@types/node@22.10.7)(jiti@2.4.2)(sass@1.78.0)(terser@5.32.0)(tsx@4.19.2)(yaml@2.6.1)
transitivePeerDependencies: transitivePeerDependencies:
@ -16122,7 +16115,7 @@ snapshots:
std-env: 3.8.0 std-env: 3.8.0
tinybench: 2.9.0 tinybench: 2.9.0
tinyexec: 0.3.2 tinyexec: 0.3.2
tinypool: 1.0.1 tinypool: 1.0.2
tinyrainbow: 1.2.0 tinyrainbow: 1.2.0
vite: 6.0.9(@types/node@22.10.7)(jiti@2.4.2)(sass@1.78.0)(terser@5.32.0)(tsx@4.19.2)(yaml@2.6.1) vite: 6.0.9(@types/node@22.10.7)(jiti@2.4.2)(sass@1.78.0)(terser@5.32.0)(tsx@4.19.2)(yaml@2.6.1)
vite-node: 2.1.8(@types/node@22.10.7)(jiti@2.4.2)(sass@1.78.0)(terser@5.32.0)(tsx@4.19.2)(yaml@2.6.1) vite-node: 2.1.8(@types/node@22.10.7)(jiti@2.4.2)(sass@1.78.0)(terser@5.32.0)(tsx@4.19.2)(yaml@2.6.1)