Skip to content

Commit

Permalink
fix(content): warn when module is loaded after Content v3
Browse files Browse the repository at this point in the history
  • Loading branch information
harlan-zw committed Jan 20, 2025
1 parent 632cb64 commit 5b6483b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
11 changes: 11 additions & 0 deletions docs/content/2.guides/5.content.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 5 additions & 3 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,10 @@ declare module 'vue-router' {
const nuxtV3Collections = new Set<string>()
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
Expand Down Expand Up @@ -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<SitemapUrl> = {
loc: content.path,
Expand All @@ -397,8 +400,7 @@ declare module 'vue-router' {
defaults.images = images
if (lastmod)
defaults.lastmod = lastmod
content.sitemap = defu(sitemapConfig, defaults) as Partial<SitemapUrl>
ctx.content = content
ctx.content.sitemap = defu(typeof content.sitemap === 'object' ? content.sitemap : {}, defaults) as Partial<SitemapUrl>
})

addServerHandler({
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/content-v3/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import NuxtRobots from '../../../src/module'

export default defineNuxtConfig({
modules: [
NuxtRobots,
'@nuxt/content',
NuxtRobots,
],

site: {
Expand Down

0 comments on commit 5b6483b

Please sign in to comment.