contentful / contentful.js

JavaScript library for Contentful's Delivery API (node & browser)
https://contentful.github.io/contentful.js
MIT License
1.18k stars 197 forks source link

`withAllLocales` doesn't query on all locales. #2196

Open votemike opened 5 months ago

votemike commented 5 months ago

Expected Behavior

Querying with withAllLocales should query on all translations. E.G.

await client.withAllLocales.getEntries<
  TypePageSkeleton | TypePostSkeleton
>({
  content_type: 'page',
  'fields.url': url,
  include: 10,
  limit: 1,
});

Should return my results for any localised url.

Actual Behavior

The query mentioned above only works if querying url specified in the default locale language.

Possible Solution

Steps to Reproduce

Set up Contentful with a default language and a language that falls back to the default language. Create a model with a translatable URL field. Add an entry with with only a url in the default language Add an entry with with a url in both languages Query based on non-default url.

Context

I have several pages that can be in several languages. Sometimes I want all languages to use the same URL. Sometimes I want each language to have a unique URL. I want to retrieve the page by URL. I cannot pass the locale in to the query, as locales with no URL translation will not fallback to the default language url for querying purposes.

Environment

rfearing commented 5 months ago

I'm having the same problem: V 10.7.0 (and in latest)

rfearing commented 5 months ago

@votemike I figured out my issue. I had to continue digging down into the nested fields. Our Page content model has a content field. The localized content is nested in that content field. That means we had to dig deeper to get our localized fields e.g.

entry.fields.content['en-US'].fields.title['pt-BR']

I hope maybe that helps but not sure if it's the same for you.

votemike commented 5 months ago

@votemike I figured out my issue. I had to continue digging down into the nested fields. Our Page content model has a content field. The localized content is nested in that content field. That means we had to dig deeper to get our localized fields e.g.

entry.fields.content['en-US'].fields.title['pt-BR']

I hope maybe that helps but not sure if it's the same for you.

Is that while you are querying the API?

rfearing commented 5 months ago

Yeah, so entry is a result of mapping items from:

const {items} = await this.client.withAllLocales.getEntries<PageEntrySkeleton>(query)

votemike commented 5 months ago

So what query are you using to query the pt-BR title?

rfearing commented 5 months ago
const query = {
    content_type: 'page',
    'fields.path': path,
    include: 5,
    limit: 1,
}

This get's our Page and child models, then entry is items[0]

votemike commented 5 months ago

Ahh, so you query by path (which I guess is the same in all locale's) and then choose the pt-BR localisation of the title.