Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to query localized content in Nuxt Content with experimental clientDB and no SSR #2846

Open
dxlliv opened this issue Nov 15, 2024 · 2 comments
Labels
v2 Relevant for @nuxt/content v2

Comments

@dxlliv
Copy link

dxlliv commented Nov 15, 2024

Environment

Nuxt version: 3.14.159
Nuxt Content version: 2.13.4
SSR: false

Error happens during development and after server side content generation.

Reproduction

https://stackblitz.com/edit/sos-nuxt-content-npcvlj?file=nuxt.config.ts

It works with default language
https://sosnuxtcontent-ewna--3000--f565b097.local-credentialless.webcontainer.io/omg/error

but not with secondary languages
https://sosnuxtcontent-ewna--3000--f565b097.local-credentialless.webcontainer.io/it/omg/errore

Describe the bug

Hi, I'm experiencing an issue while querying localized content with Nuxt Content in a setup with SSR: false and experimental.clientDB enabled.

Following the suggestion in issue #2062, I enabled clientDB to make Nuxt Content work with static generation and ssr: false. Everything happily worked as expected until I tried filtering content by locale.

I'm on latest versions of Nuxt and Nuxt Content.
Here's a code example of how I'm filtering content by locale:

const myItems = ref([]);

await useAsyncData(queryContentKey, async () => {
  const document = await queryContent(queryContentKey).locale(locale.value).findOne()

  myItems.value = document.list
})

With .locale(locale.value) it doesn't retrieve anything, but if I disable the experimental DB client it works again.

Additional context

I'm using clientDB as suggested in issue #2062, because without it, querying content with SSR: false results in a 404 error on the generated content. I'm building an app with Capacitor and need to keep SSR: false; otherwise, the app size increases significantly (200MB with SSR vs. 24MB without).

@dxlliv dxlliv added the v2 Relevant for @nuxt/content v2 label Nov 15, 2024
@thorge
Copy link

thorge commented Dec 4, 2024

I am encountering the same issue. In my previous project, which used "@nuxt/content": "^2.11.0", "nuxt": "^3.9.3" and "@nuxtjs/i18n": "^8.0.0", I did not need to specify the locale explicitly. The following code worked seamlessly across all locales:

const route = useRoute();

const { data, status, error, refresh } = await useAsyncData("page", () =>
  queryContent(route.path).findOne()
);

However, in my current setup using "@nuxt/content": "^2.13.4", "nuxt": "^3.14.1592" and "@nuxtjs/i18n": "^9.1.0",, I’m running into issues. Now this is my workaround:

const route = useRoute();
const localePath = useLocalePath();

const { data, status, error, refresh } = await useAsyncData("page", () =>
  queryContent(localePath(route.path)).findOne()
);

My files are located in content/{locale}/...

@hywax
Copy link

hywax commented Dec 4, 2024

My solutions

// app/composables/usePageContent.ts
export function usePageContent() {
  const route = useRoute()
  const { locale } = useI18n()

  const normalizedRoute = route.path.replace(`/${locale.value}`, '')

  return useAsyncData(normalizedRoute, () => queryContent(normalizedRoute).locale(locale.value).findOne())
}

// page/*
<script setup lang="ts">
const { data } = await usePageContent()
</script>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
v2 Relevant for @nuxt/content v2
Projects
None yet
Development

No branches or pull requests

3 participants