Are some of frontmatter attributes moved to meta key?
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
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 :)
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:
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 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)
It is already described in the documentation https://content.nuxt.com/docs/collections/types
But it's not described how to add them in frontmatter https://content.nuxt.com/docs/files/markdown#frontmatter ?
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.
@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 ?
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:
- Example Zod Types for augmenting a simple blog post with
tagsorauthorand thecontent.config.tsrequired - Example frontmatter setting
authorvs.some-custom-data - Syntax for how to load with
queryCollectionand 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 😭 😆
@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.