From 5b6483b48607cd0ab9c2c827fe6a81195767669a Mon Sep 17 00:00:00 2001 From: harlan Date: Mon, 20 Jan 2025 20:31:56 +1100 Subject: [PATCH] fix(content): warn when module is loaded after Content v3 --- docs/content/2.guides/5.content.md | 11 +++++++++++ src/module.ts | 8 +++++--- test/fixtures/content-v3/nuxt.config.ts | 2 +- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/docs/content/2.guides/5.content.md b/docs/content/2.guides/5.content.md index 0e3016d..e75f1d3 100644 --- a/docs/content/2.guides/5.content.md +++ b/docs/content/2.guides/5.content.md @@ -29,6 +29,17 @@ export default defineContentConfig({ }) ``` +Due to current Nuxt Content v3 limitations, you must load the sitemap module before the content module. + +```ts +export default defineNuxtConfig({ + modules: [ + '@nuxtjs/sitemap', + '@nuxt/content' // <-- Must be after @nuxtjs/sitemap + ] +}) +``` + ## Setup Nuxt Content v2 diff --git a/src/module.ts b/src/module.ts index 6bf3eec..83acfe1 100644 --- a/src/module.ts +++ b/src/module.ts @@ -360,6 +360,10 @@ declare module 'vue-router' { const nuxtV3Collections = new Set() const isNuxtContentV2 = usingNuxtContent && await hasNuxtModuleCompatibility('@nuxt/content', '^2') if (isNuxtContentV3) { + // check if content was loaded first + if (nuxt.options._installedModules.some(m => m.meta.name === 'Content')) { + logger.warn('You have loaded `@nuxt/content` before `@nuxtjs/sitemap`, this may cause issues with the integration. Please ensure `@nuxtjs/sitemap` is loaded first.') + } // TODO this is a hack until content gives us an alias nuxt.options.alias['#sitemap/content-v3-nitro-path'] = resolve(dirname(resolveModule('@nuxt/content')), 'runtime/nitro') // @ts-expect-error runtime type @@ -388,7 +392,6 @@ declare module 'vue-router' { } // Note: videos only supported through prerendering for simpler logic - const sitemapConfig = typeof content.sitemap === 'object' ? content.sitemap : {} const lastmod = content.seo?.articleModifiedTime || content.updatedAt const defaults: Partial = { loc: content.path, @@ -397,8 +400,7 @@ declare module 'vue-router' { defaults.images = images if (lastmod) defaults.lastmod = lastmod - content.sitemap = defu(sitemapConfig, defaults) as Partial - ctx.content = content + ctx.content.sitemap = defu(typeof content.sitemap === 'object' ? content.sitemap : {}, defaults) as Partial }) addServerHandler({ diff --git a/test/fixtures/content-v3/nuxt.config.ts b/test/fixtures/content-v3/nuxt.config.ts index 6551630..61f8065 100644 --- a/test/fixtures/content-v3/nuxt.config.ts +++ b/test/fixtures/content-v3/nuxt.config.ts @@ -2,8 +2,8 @@ import NuxtRobots from '../../../src/module' export default defineNuxtConfig({ modules: [ - NuxtRobots, '@nuxt/content', + NuxtRobots, ], site: {