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

Navigating to a non-existent URL with no defaultLocale defined throws a TypeError #9

Closed
joakimglaving opened this issue Jan 23, 2018 · 10 comments

Comments

@joakimglaving
Copy link

joakimglaving commented Jan 23, 2018

I'm trying to create a website with all content shown at example.com/en (or /fr, /es, /it etc.) So basically when you go to example.com, I want to redirect to /en /fr /es.. based on certain parameters, such as cookies and the browser locale.

While trying to figure out a way to do this, the first step was skipping defining a defaultLocale. That way, all my languages are under their respective /locale/ url (it is also necessary to include the trailing slash, otherwise you get the error shown below).

However, when navigating to a non-existent URL without a defaultLocale, you get:

{ TypeError: Cannot set property 'error' of undefined
    at Object.error (.nuxt/index.js:91:0)
    at module.exports.__webpack_exports__.a (.nuxt/i18n.plugin.js:20:0)
    at createApp (.nuxt/index.js:159:0)
    at <anonymous> statusCode: 500, name: 'TypeError' }

Instead of the usual 404 not found that you get with defaultLocale defined.

So what I'm asking is more or less; is this expected behaviour? Are you supposed to always use a defaultLocale? And do you have any suggestion as to how I can achieve what I am trying to achieve?

Ps. Thank you for a truly superb module, the localePath() feature has helped me loads!

This question is available on Nuxt.js community (#c11)
@paulgv
Copy link
Collaborator

paulgv commented Jan 24, 2018

Hi @bobcat1
I did some tests and I think the issue might be caused by some changes in Nuxt v1 stable release, I'll need more time to test this better.
The defaultLocale is not required but I always assumed it would be set, I wonder if the module should have more options/features to handle automatic locale selection or if it should be up to the user to create a middleware to do this.

@joakimglaving
Copy link
Author

From my point of view (biased because of my use case) it would be great if it had an option that let you redirect to a specific /prefix, if defaultLocale is undefined. Or perhaps add an option to defaultLocale that if set to true, will also make the default language get a prefix, which it will then redirect to if someone tries to access the site without a prefix. Something like

defaultLocale: {
   locale: 'en',
   prefix: true
}

Just an idea, I don't know how much work that would take and I'd be happy with any solution as long as it's working :)

@joakimglaving
Copy link
Author

Upon further consideration of my specific use case outlined above, I might be shooting myself in the foot with the redirect suggestions I gave you, since I don't necessarily want to redirect to the same prefix every single time, but rather base the redirect on certain parameters (cookies, browser locale..).

@paulgv
Copy link
Collaborator

paulgv commented Jan 25, 2018

The way I see it, there should be a simple redirect option that would simply redirect / to the default locale's homepage, but it also should be possible to pass a custom handler to make further checks and redirect to a specific language.

@joakimglaving
Copy link
Author

That sounds like a great idea :)

@ghost
Copy link

ghost commented Jan 25, 2018

@bobcat1 thank you for asking the question. I am too looking to be able to set the language based on a cookie and or locale based on a geolocation service I'm using.

@paulgv
I'm wondering if there was a way to run switchLocalePath from within a plugin therefore the language is set before any of the component/pages are rendered. Or something equivalent to that

@paulgv
Copy link
Collaborator

paulgv commented Jan 26, 2018

@bobcat1 The TypeError issue has been fixed in Nuxt 1.2.0 https://github.com/nuxt/nuxt.js/releases/tag/v1.2.0

@andrade1379 That's currently not possible since switchLocalePath is declared in a Vue mixin but it would be nice to have this option! I'll think about it.

@paulgv
Copy link
Collaborator

paulgv commented Jan 27, 2018

@bobcat1 just release v2.1.0 which comes with 2 new options noPrefixDefaultLocale and redirectRootToLocale : https://github.com/paulgv/nuxt-i18n#options
You can use these 2 options to force URL prefixes for languages as well as achieving simple redirections from / to a given locale. Hope it helps a little, we still need to have an option for more complex redirections but it's a start :)

@joakimglaving
Copy link
Author

@paulgv Just had the chance to try it now, it works great! Thanks for the superquick updates :)

@paulgv paulgv closed this as completed Mar 20, 2018
@ghost
Copy link

ghost commented Aug 11, 2018

This question has been resolved by @paulgv, see answer.

@ghost ghost added the cmty:status:resolved label Aug 11, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants