next-drupal icon indicating copy to clipboard operation
next-drupal copied to clipboard

defaultLocale missing language prefix

Open pinoniq opened this issue 3 years ago • 12 comments

The defaultLocale of next.js is not always the same as the Drupal one. So removing the language prefix when retreiving paths will give a 404.

What is the reason for removing the prefix from the slug for defaultLocale? https://github.com/chapter-three/next-drupal/blob/main/packages/next-drupal/src/utils.ts#L117-L120

Drupal can also add the prefix for the defaultLocale, so shouldn't this be a configuration?

pinoniq avatar Aug 30 '22 12:08 pinoniq

I see. We assume that the defaultLocale on the Next.js site is always the same as the Drupal one.

The defaultLocale of next.js is not always the same as the Drupal one.

Hmm, do you mean your Next.js site is pulling non-default content and using it for for defaultLocale?

Untitled-2022-03-23-1048

shadcn avatar Sep 01 '22 10:09 shadcn

@shadcn I think that what @pinoniq try to explain is that on Drupal URL language detection configuration (/admin/config/regional/language/detection/url) the default language CAN have a langcode prefix too:

Screen Shot 2022-11-09 at 12 22 09

And in that case, then all default language nodes return a 404 error page with Next Drupal.

What @pinoniq suggest is to make that langcode prefix configurable in Next Drupal. Maybe there is already a way to do that, but it is not obvious and I could not find a fix neither.

Any help on that issue would be welcome, as I can't really remove the English (en) path prefix (Default language) on existing project.

sinyayadynya avatar Nov 09 '22 06:11 sinyayadynya

@sinyayadynya Any chance you can help me reproduce this? What should I configure on the Drupal site?

shadcn avatar Nov 09 '22 07:11 shadcn

@shadcn sure I would be glad if I can help. Here is what is needed on the Drupal side config:

  • multilingual, with the following core modules enabled: language, config_translation
  • adding at least 1 language other than default English
  • at /admin/config/regional/language/detection, enabling the 'URL' option and save, Screen Shot 2022-11-09 at 14 36 57
  • clicking the 'Configure' button for 'URL' and go to /admin/config/regional/language/detection/url: adding the 'en' as below Screen Shot 2022-11-09 at 14 32 55

Then with some content in default English language, you should be able to reproduce the 404 error. Screen Shot 2022-11-09 at 14 40 17

After trying the marketing and umami examples and watching the Bay Area Drupal Camp demo, I came back to the basic-starter app and noticed that if I used it with my Umami Drupal demo (without 'en' prefix for the default language) it was working as expected. But as soon as I'm connecting it to another project with 'en' prefix for the default language, then it fails.

@pinoniq probably pointed the right piece of code where that happen. Now I'm too fresh with NextJS to say if there is a simple way to fix that either in config or in components.

sinyayadynya avatar Nov 09 '22 08:11 sinyayadynya

I have tried also to add this to the next config:

module.exports = {
    nextConfig,
    i18n: {
        locales: ['default', 'en', 'es', 'fr'],
        defaultLocale: 'default',
        localeDetection: false,
    },
    trailingSlash: true,
};

But then I will get this error, with double /en/en/ :

Server Error
Error: Failed to fetch JSON:API index at http://dev.site.io/en/en/jsonapi - Unexpected token < in JSON at position 0

sinyayadynya avatar Nov 09 '22 11:11 sinyayadynya

What if you use the /en/jsonapi as apiPrefix? Could this work as a temp solution?

export const drupal = new DrupalClient(process.env.NEXT_PUBLIC_DRUPAL_BASE_URL, {
  apiPrefix: "/en/jsonapi",
})

https://next-drupal.org/docs/configuration#apiprefix

shadcn avatar Nov 09 '22 11:11 shadcn

Unfortunately also getting:

Error: Failed to fetch JSON:API index at http://dev.drupalsite.io/en/en/jsonapi - Unexpected token < in JSON at position 0

with double /en/en/ in the path, when adding apiPrefix: "/en/jsonapi", into drupal.ts

(while reverting the previous next config change mentioned in https://github.com/chapter-three/next-drupal/issues/286#issuecomment-1308611254)

It is either too many or too few prefix.

sinyayadynya avatar Nov 09 '22 17:11 sinyayadynya

~~Just came across this as webforms requires Interface text language detection of url to serve the translated form: https://www.drupal.org/project/webform/issues/3175374~~

~~Enabling that of course raises this issue where next-drupal is trying to fetch ${NEXT_PUBLIC_DRUPAL_BASE_URL}/jsonapi instead of ${NEXT_PUBLIC_DRUPAL_BASE_URL}/en-ca/jsonapi for the default locale (en-ca is our default). The former 404s.~~

~~Anyway, I think my issue is exactly this ticket, fingers crossed on the pull request you opened. Thanks!~~

I'm not even confident in what I wrote above - will update if I find a pattern of behaviour.

martdavidson avatar Nov 28 '22 16:11 martdavidson

Actually, @shadcn started exploring a fix where it is possible to set the defaultLocale on the client: https://github.com/chapter-three/next-drupal/pull/362 (https://github.com/chapter-three/next-drupal/tree/feat/defaultLocale-for-client)

Unfortunately, I'm not sure where and how to set that defaultLocale. Would be glad if you could suggest me a way @martdavidson

sinyayadynya avatar Nov 28 '22 18:11 sinyayadynya

You can now install experimental releases from PRs. See https://github.com/chapter-three/next-drupal/pull/362#issuecomment-1332019345

d315kZ2T

@martdavidson Can you check out one of those releases and see if this fixes your issue?

You can now set a defaultLocale on the client.

// Will fetch JSON:API from https://drupal-site.com/es/jsonapi by default.
export const drupal = new DrupalClient(
  process.env.NEXT_PUBLIC_DRUPAL_BASE_URL,
  {
    defaultLocale: "es",
  }
)

shadcn avatar Nov 30 '22 13:11 shadcn

@shadcn I think I just added to confusion in this thread, my issues might have been unrelated. If I run into issues again I'll check this out to see if it lines up - thanks 🙏

martdavidson avatar Nov 30 '22 13:11 martdavidson

I just reviewed #362 and I think a better way to think about this issue is with a "default locale prefix".

For non-default locales, the locale prefix will be strings looking like "/es" or "/jp".

The default locale prefix for the default locale is an empty string "" no matter what the default locale is. But you can configure Drupal to always use a locale prefix even for the default locale.

It makes sense to me to add a defaultLocalePrefix: string config that defaults to "".

How does that sound to everyone?

JohnAlbin avatar Feb 22 '24 10:02 JohnAlbin