nuxt / content

The file-based CMS for your Nuxt application, powered by Markdown and Vue components.
https://content.nuxt.com
MIT License
3.08k stars 624 forks source link

useContent() available in normal mode #2403

Open jeannen opened 11 months ago

jeannen commented 11 months ago

Hi! I'm new to Github so I hope this is the right place for that

Is your feature request related to a problem? Please describe

I would like to access the content object containing the title, content, etc from my /content/blog.

I have a [...slug].vue file in my /pages/blog folder, where I'd like to access the article info. I saw we could use useContent() to access it but for that I need to enable "document driven mode". It would be nice to be able to access it directly.

Describe the solution you'd like

Instead of having to enable documentDriven mode in the config, have useContent accessible by default.

Describe alternatives you've considered

It kinda works with queryContent() but isn't it overkill? Like, isn't it going to re-fetch the article after it was sent to the template? Could be nice to have access to all document data directly

const route = useRoute()
const slug = '/blog/' + route.params.slug.join('/')
const { data } = await useAsyncData('home', () => queryContent(slug).findOne())
const { body, categories, date, description, title, _path, _slug } = data.value
console.log(description, title)

Additional context

My page:

<template>
    <main>
        <ContentDoc>
            <template #not-found>
                <h1>Oops... 😬</h1>
                <p>It looks like this page doesn't exist.</p>
            </template>

            <template v-slot="{ doc }">
                <p class="article-date">{{ doc.date }}</p>
                <h1 class="article-title">{{ doc.title }}</h1>
                <NuxtImg class="article-img" :src="`/images/blog/${doc.coverImage}`" :alt="doc.title" width="500px" />
                <ContentRenderer :value="doc" class="page-formating" />
            </template>
        </ContentDoc>
    </main>
</template>
Barbapapazes commented 10 months ago

Hello,

I'm not sure to understand the issue. What is the issue of using useAsyncData and what will be improved if you have useContent?

jeannen commented 10 months ago

Hello,

I'm not sure to understand the issue. What is the issue of using useAsyncData and what will be improved if you have useContent?

If I use both < ContentDoc /> and useAsyncData(), isn't it going to fetch the data twice?

liamsnowdon commented 8 months ago

The way I got around data being fetched twice for now was using ContentRenderer directly instead of ContentDoc then passing the data from queryContent through the value prop.

mewforest commented 6 months ago

Same problem here

github-actions[bot] commented 6 days ago

This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 30 days.