content icon indicating copy to clipboard operation
content copied to clipboard

useContent() available in normal mode

Open jeannen opened this issue 2 years ago • 5 comments

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>

jeannen avatar Oct 29 '23 03:10 jeannen

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?

Barbapapazes avatar Nov 10 '23 13:11 Barbapapazes

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?

jeannen avatar Nov 12 '23 09:11 jeannen

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.

liamsnowdon avatar Jan 14 '24 18:01 liamsnowdon

Same problem here

mewforest avatar Mar 28 '24 13:03 mewforest