content icon indicating copy to clipboard operation
content copied to clipboard

Are some of frontmatter attributes moved to meta key?

Open victor-ponamariov opened this issue 11 months ago • 9 comments

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 :)

victor-ponamariov avatar Feb 01 '25 02:02 victor-ponamariov

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

danielju91 avatar Feb 01 '25 08:02 danielju91

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()
      })
    })
  }
})

mateusznarowski avatar Feb 01 '25 19:02 mateusznarowski

@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)

danielju91 avatar Feb 02 '25 12:02 danielju91

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

Image

mateusznarowski avatar Feb 02 '25 14:02 mateusznarowski

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

victor-ponamariov avatar Feb 02 '25 14:02 victor-ponamariov

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.

farnabaz avatar Feb 03 '25 10:02 farnabaz

@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 ?

danielju91 avatar Feb 03 '25 13:02 danielju91

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 😭 😆

JessicaSachs avatar Feb 03 '25 22:02 JessicaSachs

@mateusznarowski Thanks for this! Started learning Nuxt two days ago and was really confused how to access custom front matter fields without having to go inside the meta object.

larsb-dev avatar Nov 23 '25 10:11 larsb-dev