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

context.locale on getStaticProps returns wrong locale when rewrites are used #18927

Closed
hk86 opened this issue Nov 7, 2020 · 12 comments · Fixed by #19164
Closed

context.locale on getStaticProps returns wrong locale when rewrites are used #18927

hk86 opened this issue Nov 7, 2020 · 12 comments · Fixed by #19164
Assignees
Milestone

Comments

@hk86
Copy link

hk86 commented Nov 7, 2020

Bug report

Describe the bug

A website with these urls:

/hello
/de/hallo
/es/hola
/ja/hello

All urls are served from the same page and SSG is used. Inspecting the context parameter on getStaticProps, gives us these results by url:

/hello:

{
  params: { ... },
  locales: [
    'en', 'de', 'es', 'ja',
  ],
  **locale: 'en',**
  defaultLocale: 'en'
}

/de/hello and /es/hallo:

{
  params: { ... },
  locales: [
    'en', 'de', 'es', 'ja',
  ],
  **locale: 'en',**
  defaultLocale: 'en'
}

/ja/hello:

{
  params: { ... },
  locales: [
    'en', 'de', 'es', 'ja',
  ],
  **locale: 'ja',**
  defaultLocale: 'en'
}

To Reproduce

Create /pages/[slug]/hello.js:

export async function getStaticProps(context) {
  return {
    props: { ... },
    revalidate: 10,
  };
}

export async function getStaticPaths() {
  return {
    paths: [],
    fallback: 'blocking',
  };
}

Activated the new i18n feature:

  i18n: {
    locales: [
    'en', 'de', 'es', 'ja',
    ],
    defaultLocale: 'en',
  },

Add rewrites:

  async rewrites() {
    return [
      {
        source: "/de/hallo",
        destination: "/hello",
      },
      {
        source: "/es/halo",
        destination: "/hello",
      },
    ];
  },

Expected behavior

context.locale should return the correct locale.

System information

Version of Next.js: 10.0.1

Additional context

This issues arrise when deployed to NOW. If I build it locally and run npm run start the context parameter has the correct locale.

@hk86 hk86 added the bug Issue was opened via the bug report template. label Nov 7, 2020
@hk86
Copy link
Author

hk86 commented Nov 11, 2020

Any news on this one?

@timneutkens
Copy link
Member

Looks like the behavior is correct based on what you're explaining. You're rewriting /de/hallo to /hello which is the /hello route. Not the /de/hello route, so you'll probably want to try changing that.

@hk86
Copy link
Author

hk86 commented Nov 12, 2020

@timneutkens This will result in a 404 - Page Not Found

@ijjk ijjk added kind: bug and removed bug Issue was opened via the bug report template. labels Nov 14, 2020
@ijjk ijjk self-assigned this Nov 14, 2020
@kodiakhq kodiakhq bot closed this as completed in #19164 Nov 14, 2020
kodiakhq bot pushed a commit that referenced this issue Nov 14, 2020
This mirrors the `basePath: false` behavior for custom-routes with `locale: false` to allow users to configure the locales manually (`locale: false`) or have the locales be handled automatically for custom-routes. 

Fixes: #18927
Fixes: #18795
@Timer Timer added this to the iteration 12 milestone Nov 14, 2020
@ijjk
Copy link
Member

ijjk commented Nov 14, 2020

Hi, this has been updated in the latest canary of Next.js v10.0.2-canary.16, docs for the new rewrites + i18n handling can be viewed here until a new patch release is published, please upgrade and give it a try!

@delucca
Copy link

delucca commented Nov 14, 2020

@ijjk what about custom paths for each locale? How can we achieve that?

For example, I have the following page: key-results.tsx and I want these paths to be mapped to it:

  • getbud.co/key-results (when locale = en)
  • br.getbud.co/resultados-chave (when locale = pt-BR)

@ijjk
Copy link
Member

ijjk commented Nov 14, 2020

@delucca with the newly added locale: false option for custom-routes on the latest canary of Next.js v10.0.2-canary.17 you should be able to achieve this, for example:

module.exports = {
  async rewrites() {
    return [
      // /en/key-results already maps to /en/key-results
      {
        source: '/pt-BR/resultados-chave',
        destination: '/pt-BR/key-results',
        locale: false
      },
    ]
  }
}

@delucca
Copy link

delucca commented Nov 15, 2020

@delucca with the newly added locale: false option for custom-routes on the latest canary of Next.js v10.0.2-canary.17 you should be able to achieve this, for example:

module.exports = {
  async rewrites() {
    return [
      // /en/key-results already maps to /en/key-results
      {
        source: '/pt-BR/resultados-chave',
        destination: '/pt-BR/key-results',
        locale: false
      },
    ]
  }
}

Oh, great.

But, that also works with subdomain locales?

Also, I would need to solve the URL links in the application? Or NextJS would map to the given locale route?

@delucca
Copy link

delucca commented Nov 18, 2020

@ijjk quick question, what about client-side routing?

I was able to make your example work while typing the /pt-BR/resultados-chave directly in my browser. But when I try to integrate with next/link it redirects me to /key-results instead.

@hk86
Copy link
Author

hk86 commented Nov 18, 2020

Hi, this has been updated in the latest canary of Next.js v10.0.2-canary.16, docs for the new rewrites + i18n handling can be viewed here until a new patch release is published, please upgrade and give it a try!

I can confirm. The issue is fixed. Thanks!

@ijjk
Copy link
Member

ijjk commented Nov 18, 2020

@delucca can you provide a repo with a minimal reproduction for the client-side routing not working as expected, sounds like this might need updating to handle this case there.

@delucca
Copy link

delucca commented Nov 20, 2020

@ijjk I've created a bug for it, with a proper Codesandbox #19342

Can you help me please? :)

@balazsorban44
Copy link
Member

This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@vercel vercel locked as resolved and limited conversation to collaborators Jan 29, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants