i18n
i18n copied to clipboard
Localized Paths reachable from other languages when using differentDomains
Version
Reproduction link
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 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.
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.
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.
I am having the same problem of paths being accessible from other locales.
Is there any known workaround to this problem?
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
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.