Advanced
Raw Content
Access to contents raw data in appliction
There were lots of requests in Content version 2 about accessing contents raw data in production. In Content version 3 it is possible to ship contents raw data to production.
In order to ship raw contents to production you need to define rawbody
field in your collection's schema.
That's it.
Nuxt Content will detect this magical field in your schema and fill it with the raw content.
content.config.ts
import { defineCollection, z } from '@nuxt/content'
export const collections = {
docs: defineCollection({
source: '**',
type: 'page',
schema: z.object({
rawbody: z.string()
})
})
}
And you can use queryCollection()
to fetch the raw content.
pages/index.vue
<script setup lang="ts">
const route = useRoute()
const { data } = useAsyncData('page-' + route.path, () => queryCollection('docs').path(route.path).first())
</script>
<template>
<pre>{{ data.rawbody }}</pre>
</template>
In case you don't want to ship raw content of a specific file you can add rawbody: ''
to frontmatter of that file. The auto filled value of rawbody
is acting like default value and when you define rawbody
in the frontmater it will overwriten.
content.md
---
title: My page
rawbody: ''
---
It is important to fill frontmatter fields with a same type of data that defined in collection schema. In this case
rawbody
is a string, and you should consider passing empry string. Do not use boolean
or other type of values.