Can't read property 'locale' of undefined

HapLifeMan
0
HapLifeMan
commented 8 months ago

Version

v5.3.0

Reproduction link

https://github.com/HapLifeMan/nuxt-i18n-issue

Steps to reproduce

1) Clone git repository
2) Generate files with npm run generate
3) Open local server on dist/ folder
4) Try to access /, see error

What is expected ?

As the route generation strategy is configured on prefix, and there is no forced rootRedirect, it is expected that we are redirected to /fr or /en depending on the browser language.

What is actually happening?

Actually, no index.html file that redirect us to localized routes is generated in the dist folder.
We're stuck on the / with no redirect (personally, I have an error Can't read property 'locale' of undefined.).

Additional comments?

Everything is working fine with npm run build or npm run start, we are redirected correctly.
But I want to have a fully static website with a working root redirection…
Actually to fix temporarily the problem, I defined fr as my default locale (strategy prefix_except_default).

bug
0
michaelize
0
michaelize
commented 7 months ago

@HapLifeMan Can you check if this fix in https://github.com/nuxt-community/nuxt-i18n/pull/178 resolves your issue?

0
HapLifeMan
0
HapLifeMan
commented 7 months ago

@michaelize Unfortunately this doesn't fix the issue, no index.html file is generated on root folder…

0
michaelize
0
michaelize
commented 7 months ago

I see. This is a different issue than in https://github.com/nuxt-community/nuxt-i18n/issues/173. The strategy is prefix and there isn't an index page to redirect to the default locale folder.

@paulgv Do you think we should have an index.html in the root directory to do the redirection?

0
paulgv
4.6k
paulgv
commented 7 months ago

@michaelize hmm yeah maybe we should add a / when in generate mode with prefix strategy. But if we do this, users might be able to access an empty page at / after being redirected once (if useCookie is true).
@HapLifeMan how about using prefix_and_default strategy instead, would that make sense in your case?

0
michaelize
0
michaelize
commented 7 months ago

On second thought, creating an index.html seems a bad idea. The module only generates the pages that exist. Creating an index.html will have a higher chance of causing conflicts, for example, if the user has a common index.html in place, we cannot overwrite that index.html.

I think it makes more sense to have the user creates a common entry point for all locales. It can be a page where the user can pick their language preference, something like https://www.apple.com/choose-country-region/

0
HapLifeMan
0
HapLifeMan
commented 7 months ago

@paulgv This is what I am doing to temporarily "fix" the problem.

But it's not a good solution in my opinion because you can access same pages with different URLs (for the default language).

This is a big problem I think because the generate doesn't generate and work properly if you get the / and don't want to have a locale without prefix 😕

0
paulgv
4.6k
paulgv
commented 7 months ago

I agree @HapLifeMan, I'm not sure there's a legit way of doing this for nuxt-i18n because the middleware will never be called unless there's an actual page to show.
In this specific case, when using Nuxt's generate, you might have to configure a server-side redirection yourself, say with an .htaccess like this: https://gist.github.com/markhowellsmead/0c414cea619727a3618b
What do you think?

0
HapLifeMan
0
HapLifeMan
commented 7 months ago

Yes it can be a good solution and resolve a part of the problem @paulgv.
But I don't know what can be the impact on SEO with that kind of redirect…
What will be displayed when you type myapp in your browser, what URL will be showed and in which language etc…

0
paulgv
4.6k
paulgv
commented 7 months ago

Having this kind of redirection will prevent the root path / from being indexed at all because all requests will be redirected to one of the prefixed paths. More info on 301 redirects and SEO: https://moz.com/learn/seo/redirection
As for which URL will show up first in the results, it will probably be the one corresponding to the default redirect most of the time since googlebot does not set the Accept-Language header: https://support.google.com/webmasters/answer/6144055?hl=en

0
Informations
Bug ReportOpen
#c179 - Created 8 months ago