1

I'm using Nuxt and Nuxt Content in a project, and i want to display the content of a csv file.

According to the documentation, i use the following code:

const { data }  = await useAsyncData<any | Concert[]>('concerts', () => queryContent('/concerts').findOne())

Up to now, I don't encounter any issue. But once i got the data, i need to use it into two functions before displaying it. I tried many things, like using the transform option on the useAsyncData composable, but i didn't manage to make it work, and finally I came up with this solution:

const sorted = computed(() => data.value?.body ? sortConcerts(data.value.body) : null)
const concerts = computed(() => sorted.value ? formatConcerts(sorted.value, en) : null)

This is only working in development if I wrap my content in a component. But when I deploy the application (with "nuxt generate", on netlify), I only see the data the first time I visit the route. The second time, my list is empty.

I feel that I'm not doing the right thing, but I can't find a solution. Does someone have a solution to achieve what i want ?

Thanks a lot!

2
  • What do you mean by "second time"? The second time to run "npm run dev", to refresh page in borwser, or to click some link to change the route? Anyway, if you haven't set anything up, useAsyncData will only execute once on the server side. Commented Dec 2, 2023 at 6:20
  • I mean that once the application is deployed (and only in that case, using "npm run generate"), the data appears the first time I visit the route, but not the second time. Hope it's more clear now ! Commented Dec 2, 2023 at 11:21

2 Answers 2

0

You could try this

<script setup>
  const sorted = ref()
  const concerts = ref()

  const { data }  = await useAsyncData<any | Concert[]>('concerts', () => queryContent('/concerts').findOne())
    
  if (data.value) {
    sorted.value = data.value?.body
    concerts.value = formatConcerts(sorted.value, en)
  }
</script>
Sign up to request clarification or add additional context in comments.

1 Comment

To clarify, once I’ve got the data, I need to pass the data in two functions. With your example it would mean: if (data.value) { sorted.value = sortConcerts(data.value.body) concerts.value = formatConcerts(sorted.value, en) } This solution is only working on hard reload (and I get the hydration node mismatch error if I don’t wrap my list in the <ClientOnly> component. When I come from an other route, I get a 404 error.
0

You can try this. Manipulate ALL the variables you need on the server-side (prerender).

<script setup>
const sorted = ref()
const concerts = ref()
            
await useAsyncData("concerts", () => queryContent("/concerts").findOne())
  .then((res) => (sorted.value = sortConcerts(res.value.body)))
  .then(() => (concerts.value = formatConcerts(sorted.value, en)));
</script>

1 Comment

Thanks for the solution! It works now. I still have to put my data in the <ClientOnly> component, to avoid the hydratation mismatch error. But at least it's working! Thanks again.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.