Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Nuxt Content v3 #382

Merged
merged 14 commits into from
Jan 20, 2025
Merged

feat: Nuxt Content v3 #382

merged 14 commits into from
Jan 20, 2025

Conversation

harlan-zw
Copy link
Owner

@harlan-zw harlan-zw commented Jan 20, 2025

πŸ”— Linked issue

❓ Type of change

  • πŸ“– Documentation (updates to the documentation or readme)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • πŸ‘Œ Enhancement (improving an existing functionality)
  • ✨ New feature (a non-breaking change that adds functionality)
  • 🧹 Chore (updates to the build process or auxiliary tools and libraries)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

πŸ“š Description

Bumps all modules with Nuxt Content v3 support.

Setup Nuxt Content v3

In Nuxt Content v3 we need to use the asSeoCollection(){lang="ts"} function to augment any collections
to be able to use the SEO frontmatter keys: ogImage, schemaOrg, robots and sitemap.

import { defineCollection, defineContentConfig } from '@nuxt/content'
import { asSeoCollection } from '@nuxtjs/seo/content'

export default defineContentConfig({
  collections: {
    content: defineCollection(
      asSeoCollection({
        type: 'page',
        source: '**/*.md',
      }),
    ),
  },
})

To ensure the tags get rendered you need to set up logic to handle the added tags.

<script setup lang="ts">
import { queryCollection, useRoute } from '#imports'

const route = useRoute()
const { data: page } = await useAsyncData(`page-${route.path}`, () => {
  return queryCollection('content').path(route.path).first()
})
if (page.value?.ogImage) {
  defineOgImage(page.value?.ogImage) // <-- Nuxt OG Image
}
// Ensure the schema.org is rendered
useHead(page.value.head || {})  // <-- Nuxt Schema.org
useSeoMeta(page.value.seo || {}) // <-- Nuxt Robots
</script>

Due to current Nuxt Content v3 limitations, you must load the Nuxt SEO module before the content module.

export default defineNuxtConfig({
  modules: [
    '@nuxtjs/seo',
    '@nuxt/content' // <-- Must be after @nuxtjs/seo
  ]
})

Usage

For the full options available for each module, please see the individual module documentation.

---
ogImage:
  component: HelloWorld
  props:
    title: "Hello World"
    description: "This is a description"
    image: "/hello-world.png"
sitemap:
  lastmod: 2025-01-01
robots: index, nofollow
schemaOrg:
  - "@type": "BlogPosting"
    headline: "How to Use Our Product"
    author:
      type: "Person"
      name: "Jane Smith"
    datePublished: "2023-10-01"
---

# Hello World

Copy link

cloudflare-workers-and-pages bot commented Jan 20, 2025

Deploying nuxt-seo with Β Cloudflare Pages Β Cloudflare Pages

Latest commit: e37fc43
Status:🚫  Build failed.

View logs

@harlan-zw harlan-zw merged commit 300481a into main Jan 20, 2025
1 of 2 checks passed
@harlan-zw harlan-zw deleted the feat/nuxt-content-v3 branch January 20, 2025 10:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant