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

Are some of frontmatter attributes moved to meta key? #3073

Open
victor-ponamariov opened this issue Feb 1, 2025 · 8 comments
Open

Are some of frontmatter attributes moved to meta key? #3073

victor-ponamariov opened this issue Feb 1, 2025 · 8 comments

Comments

@victor-ponamariov
Copy link

When moving to Nuxt Content 3 I found out that when I queried collection, my previous custom frontmatter attributes were empty. Then I checked the collection via dev tools and found out that they are under meta now

Image

But I couldn't find anything about this in docs, only "native" parameters https://content.nuxt.com/docs/files/markdown#native-parameters.

If I miss it, could you please guide me where it is. If I don't miss it, then I can try to make the first PR ever to update the docs, but I'm not a native English speaker and will be able to only check grammar :)

@danielju91
Copy link

danielju91 commented Feb 1, 2025

I am also wondering about this. Not yet versed in Nuxt, learning at the moment, but tried to add "publishedAt" field and was wondering why I could not add it to the queryCollection.select() method.(was returning "ssr:error [nuxt] [request error] [unhandled] [500] no such column: "publishedAt" - should this be a string literal in single-quotes?" error) Turns out the field was under meta key.

I am working through a tutorial that uses V2 of nuxt/content, which instructs me to just add the publishedAt field in the frontmatter in yaml format. However, I could not find info in the docs about rules that automatically nests non-native keys inside meta key, or how to select or order by such nested fields, so I am stuck at the moment.

My Code:

<script setup>
const route = useRoute()
const posts = ref([])

const { data } = await useAsyncData(route.path, () => {
  return queryCollection('blog').where('path', '<>', '/blog').all()
})

posts.value = data.value
console.log(posts.value/*  */)
</script>

The frontmatter of 'first.md' file.

title: 'Title of the page'
description: 'meta description of the page'
publishedAt: '2023-03-12 17:15:00'
head:
  meta:
    - name: 'keywords'
    - content: 'nuxt, vue, content'

Console output:
Image

@mateusznarowski
Copy link

mateusznarowski commented Feb 1, 2025

If you want the link and external to appear in the main object, you must define a schema, all fields that are not defined in the schema will appear in meta. (meta: Custom fields not defined in the collection schema)

It seems to me that such a schema is sufficient for your example

import { defineCollection, defineContentConfig, z } from '@nuxt/content'

export default defineContentConfig({
  collections: {
    content: defineCollection({
      type: 'page',
      source: '**',
      schema: z.object({
        external: z.boolean(),
        link: z.string()
      })
    })
  }
})

@danielju91
Copy link

@mateusznarowski Thank you for your clarification! Are there plans for this detail to be added to documentation? (That unspecified frontmatter keys will be automatically nested inside meta key)

@mateusznarowski
Copy link

It is already described in the documentation https://content.nuxt.com/docs/collections/types

Image

@victor-ponamariov
Copy link
Author

But it's not described how to add them in frontmatter https://content.nuxt.com/docs/files/markdown#frontmatter ?

@farnabaz
Copy link
Member

farnabaz commented Feb 3, 2025

I guess keeping non-schema fields, created a bit of confusing. Lets put it this way, Schemas are the source of truth for collections and contents. If you want to use a field/data, should be defined in the collection's schema.

By design, meta field only exists to preserve content as whole and not lose any data.

@danielju91
Copy link

@victor-ponamariov , technically speaking, @mateusznarowski did show that, as far as the Nuxt Content doc as a whole is concerned, it is described.

I do agree with you that a brief heads-up in the frontmatter section could be helpful for Nuxt newbies like me.

But it's not described how to add them in frontmatter https://content.nuxt.com/docs/files/markdown#frontmatter ?

@JessicaSachs
Copy link
Contributor

JessicaSachs commented Feb 3, 2025

FWIW I also didn't understand how to solve this after reading the Alpha docs many, many times. I feel like a great addition to the Collection Docs would be:

  1. Example Zod Types for augmenting a simple blog post with tags or author and the content.config.ts required
  2. Example frontmatter setting author vs. some-custom-data
  3. Syntax for how to load with queryCollection and proper typings to see the zod types flow through.

I personally thought I'd done my zod-ing properly, but was like "WTF why are my types broken and why is everything under meta?" and then just was like "Whatever" and strictly typed meta as the Zod schema I passed in 😭 😆

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

No branches or pull requests

5 participants