From e17f89b5f6fd8b9acfbb911240f647bc8711335c Mon Sep 17 00:00:00 2001 From: harlan Date: Wed, 22 Jan 2025 17:11:38 +1100 Subject: [PATCH] chore: code rabbit suggestions --- packages/nuxt/src/head/module.ts | 8 ++++---- packages/nuxt/src/head/plugins/unhead-imports.ts | 4 ++-- packages/nuxt/src/head/runtime/composables/v4.ts | 7 ++++++- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/packages/nuxt/src/head/module.ts b/packages/nuxt/src/head/module.ts index 4500f0b859..68f3c6d835 100644 --- a/packages/nuxt/src/head/module.ts +++ b/packages/nuxt/src/head/module.ts @@ -11,7 +11,7 @@ export default defineNuxtModule({ name: 'nuxt:meta', configKey: 'unhead', }, - setup (options, nuxt) { + async setup (options, nuxt) { const runtimeDir = resolve(distDir, 'head/runtime') // Transpile @unhead/vue @@ -45,18 +45,18 @@ export default defineNuxtModule({ rootDir: nuxt.options.rootDir, })) + const unheadPlugins = await tryResolveModule('unhead/plugins', nuxt.options.modulesDir) || 'unhead/plugins' addTemplate({ filename: 'unhead-options.mjs', - async getContents () { + getContents () { if (isNuxtV4) { return `export default {}` } - const unheadPlugins = await tryResolveModule('unhead/plugins', nuxt.options.modulesDir) || 'unhead/plugins' // v1 unhead legacy options const disableCapoSorting = !nuxt.options.experimental.headNext return `import { DeprecationsPlugin, PromisesPlugin } from ${JSON.stringify(unheadPlugins)}; export default { - disableCapoSorting: ${disableCapoSorting} + disableCapoSorting: ${Boolean(disableCapoSorting)}, plugins: [DeprecationsPlugin, PromisesPlugin], }` }, diff --git a/packages/nuxt/src/head/plugins/unhead-imports.ts b/packages/nuxt/src/head/plugins/unhead-imports.ts index 142e2fbdcf..0f5b865a2a 100644 --- a/packages/nuxt/src/head/plugins/unhead-imports.ts +++ b/packages/nuxt/src/head/plugins/unhead-imports.ts @@ -58,10 +58,10 @@ export const UnheadImportsPlugin = (options: UnheadImportsPluginOptions) => crea if (!id.includes('node_modules')) { logger.warn(`You are importing from \`${UnheadVue}\` in \`./${relative(options.rootDir, id)}\`. Please import from \`#app\` instead for full type safety.`) } - s.prepend(`import { ${toImports(importsFromUnhead).join(', ')} } from '#app/composables/head'\n`) + s.prepend(`import { ${toImports(importsFromUnhead).join(', ')} } from "#app/composables/head"\n`) } if (importsFromHead.length) { - s.prepend(`import { ${toImports(importsFromHead).join(', ')} } from ${JSON.stringify(UnheadVue)}'\n`) + s.prepend(`import { ${toImports(importsFromHead).join(', ')} } from "${UnheadVue}"\n`) } if (s.hasChanged()) { diff --git a/packages/nuxt/src/head/runtime/composables/v4.ts b/packages/nuxt/src/head/runtime/composables/v4.ts index 34c20457af..358bab89b0 100644 --- a/packages/nuxt/src/head/runtime/composables/v4.ts +++ b/packages/nuxt/src/head/runtime/composables/v4.ts @@ -19,7 +19,12 @@ export function injectHead (nuxtApp?: NuxtApp): VueHeadClient { const nuxt = nuxtApp || useNuxtApp() return nuxt.ssrContext?.head || nuxt.runWithContext(() => { if (hasInjectionContext()) { - return inject>(headSymbol)! + const head = inject>(headSymbol) + // should not be possible + if (!head) { + throw new Error('[nuxt] [unhead] Missing Unhead instance.') + } + return head } }) as VueHeadClient }