i18n icon indicating copy to clipboard operation
i18n copied to clipboard

Localized Paths reachable from other languages when using differentDomains

Open seltsam23 opened this issue 5 years ago • 6 comments

Version

v5.3.0

Reproduction link

http://jsfiddle.com

Steps to reproduce

  • Set 'differentDomains' to true in i18n config.
  • Specify 'domain' for each item in locales
  • Add page-translations to 'pages'
// nuxt.config.js
{
  i18n: {
    differentDomains: true,
    locales: [
      {
        code: 'de',
        domain: 'de.website.com'
      },
      {
        code: 'en',
        domain: 'en.website.com'
      }
    ],
    parsePages: false, 
    pages: {
      login:{
        de:'/anmelden',
        en:'/login',
      },
}

What is expected ?

de subdomain http://de.website.com/anmelden -> 200 http://de.website.com/login -> 404

en subdomain http://en.website.com/login -> 200 http://en.website.com/anmelden -> 404

What is actually happening?

de subdomain http://de.website.com/anmelden -> 200 http://de.website.com/login -> 200 // should be 404

en subdomain http://en.website.com/login -> 200 http://en.website.com/anmelden -> 200 // should be 404

Additional comments?

It is working as expected when not using custom domains:

de subdomain http://website.com/de/anmelden -> 200 http://website.com/de/login -> 404 // works

en subdomain http://website.com/en/login -> 200 http://website.com/en/anmelden -> 404 // works

This bug report is available on Nuxt community (#c281)

seltsam23 avatar Aug 11 '19 19:08 seltsam23

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Oct 10 '19 20:10 stale[bot]

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Dec 17 '19 08:12 stale[bot]

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Oct 04 '20 20:10 stale[bot]

I am having the same problem of paths being accessible from other locales.

Is there any known workaround to this problem?

nikolashaag avatar Feb 07 '22 12:02 nikolashaag

One workaroud I can think of is to create a global middleware that checks the current path matches the locale, and if not, redirects to the correct one:

const localePathsRedirectsOnLocalizedDomains: Middleware = ({ route, redirect, app }) => {
  /* get the localized path from the current route path
     if route.path === '/anmelden' but current locale is 'en' -> it will return '/login'
   */
  const expectedPath = app.localePath(route.path)

  // If the current path doesn't match the localized one, then we're not on the right path.
  if (route.path !== expectedPath) {
    // redirect to correct one
    redirect(301, expectedPath)
    // or send 404 page
    error({ statusCode: 404 })
  }
}

export default localePathsRedirectsOnLocalizedDomains

Kapcash avatar Feb 07 '22 13:02 Kapcash

Thanks for the quick response @Kapcash . I came up with a similar solution but was manually checking the routes against the router. I didn't know about app.localePath 🙌 .

Can confirm that above workaround works.

nikolashaag avatar Feb 07 '22 16:02 nikolashaag