storyblok / storyblok

Info about Storyblok
https://storyblok.atlassian.net/servicedesk
318 stars 32 forks source link

Link object of a non-default language returns non-translated slug #729

Open ver1nty opened 2 years ago

ver1nty commented 2 years ago

The issue occurs while working with: (check one with "x")

Current behavior: When fetching a Link object of a different language than the default one through the API (both REST and GraphQL), the slug of the returned object is not translated but the default language. It contains the language prefix, but the slug parts are of the default language. Ex.: es/slug-part-1-in-english/slug-part-2-in-english

Meanwhile, if we fetch a Paragraph object that contains a link, it returns the translated slug. Ex.: es/slug-part-1-in-spanish/slug-part-2-in-spanish

Expected behavior: The returned Link object of a different language than the default one should contain the translated slug. Just as the Paragraph object behaves.

Steps to reproduce:

  1. In the UI create a page and add a translated slug to it in a different language than the default.
  2. Create another page, translate it into the different language that was used in step 1, and add a Link component to it that points to the page created in step 1 (internal link). You can also create a Paragraph component, where you add a link in the text, pointing to the same page as in step 1 (internal link) to see the differences.
  3. Run a query for the page created in step 2 and see the Link slugs. Don't forget to specify the language when you're running the query (the same language that was used in step 1).
JonathanSaudhof commented 1 year ago

is there a workaround?

bjoernbg commented 1 year ago

This is also a problem for us. The only workaround I can see is to load the story with the default slug and then extract the translated slug – if it's even there. Seems like too much overhead. Any idea when this can be fixed?

bjoernbg commented 1 year ago

I actually found a usable workaround:

Using resolve_links: 'url' when querying the content will include more information about the story that the link points to, including the full_slug with the correct translated slug!

This is how we are solving it for now:

export function getUrlFromLinkObject(link: MultilinkStoryblok | undefined) {
  if(!link) return '';
  if (link.linktype === 'story') {
    if (link.story?.full_slug) {
      return '/' + link.story.full_slug;
    } else {
      return link.cached_url;
    }
  }
  return link.url;
}