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

Fix state not defined issue in #173 #178

Merged
merged 3 commits into from
Jan 14, 2019

Conversation

michaelwnyc
Copy link

@michaelwnyc michaelwnyc commented Jan 7, 2019

This PR fixes #173

The problem occurs when deploying with SSR to a static host, such as AWS S3, then access https://example.com/en. Since there is no matching file, Nuxt loads the fallback file https://example.com/200.html.

Now due to there is no server rendered state value defined(https://nuxtjs.org/guide/vuex-store#modules-mode), client preserves the state without the i18n module state, which raised an exception later when trying to set the locale.

The fix is to set { preserveState: false } by default. If need to pass value from the server, then setpreserveState to true in the options, and create a module vuex file as described here: https://nuxtjs.org/guide/vuex-store#modules-mode

@michaelwnyc michaelwnyc mentioned this pull request Jan 7, 2019
@priezz
Copy link

priezz commented Jan 7, 2019

I would like to note, that #173 occures even when the SSR is not meant at all, i.e. in SPA nuxt mode.

@michaelwnyc
Copy link
Author

@priezz You are correct. This is not unique to SSR. It's how Vuex implements preserveState option. When preserveState is set to true, it skips adding state for the module since it assumes the state value already exists, as it can be passed from SSR.

Here's the explanation about Vuex preserveState; vuejs/vuex#1130 (comment)

@paulgv
Copy link
Collaborator

paulgv commented Jan 14, 2019

Hi and thanks for the PR!
Should I assume that merging this would fix the issue for both SSR and SPA modes?

Sorry for the delay in checking the PR.

@paulgv paulgv merged commit 142dcb0 into nuxt-modules:master Jan 14, 2019
@michaelwnyc
Copy link
Author

@paulgv Sorry for the late response.

TLDR: it works for both cases.

The plugin became broken due to a Nuxt change mentioned in one of the issues you posted here: nuxt/nuxt#3757 (comment). Nuxt now overrides the store.registerModule method for universal apps after this PR https://github.com/nuxt/nuxt.js/pull/3909/files#diff-c1b82c7b8931d52d83e399952282cbc2R52

When preserveState is set to false, it simply initiates with local state. Since this module doesn't have any default server-rendered state, it should work fine in either case.

The reason @priezz is having this issue even when SSR is not being used is very likely due to the mode is set to universal. Pls let me know if this is not correct.

This problem was also identified on Nuxt repo: nuxt/nuxt#4370

@michaelwnyc michaelwnyc deleted the fix-locale-not-defined branch January 23, 2019 03:56
@priezz
Copy link

priezz commented Jan 23, 2019

@michaelize Mode is set to 'spa' in my case.

@michaelwnyc
Copy link
Author

@priezz I couldn't reproduce this issue under spa mode. Can you create a sample repo?

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

Successfully merging this pull request may close these issues.

Cannot set property 'locale' of undefined
3 participants