-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
fix(vue-query): skip hydration when Nuxt state is null #11429
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
Merged
+2
β2
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
π©Ί Stability & Availability | π Major | β‘ Quick win
π Supported by static analysis
π€ get_repo_knowledge executed:
get_repo_knowledge TanStack/query /tmp/coderabbit-repo-knowledge/tanstack-query-2191d1d3/learnings /tmp/coderabbit-repo-knowledge/tanstack-query-2191d1d3/conventionsLength of output: 3472
π Script executed:
Repository: TanStack/query
Length of output: 24466
π Script executed:
Repository: TanStack/query
Length of output: 33999
π Script executed:
Repository: TanStack/query
Length of output: 4088
π Web query:
site:nuxt.com/docs useState Nuxt 3 initializer optional undefinedπ‘ Result:
In Nuxt 3 and 4, the initializer function provided to
useStateis optional [1]. If you do not provide an initializer, the state will be initialized asundefinedif the key has not been set previously [1]. When usinguseState, it is important to understand the following: 1. Initializer Requirements: The initializer must be a function, not a raw value [2]. Passing a non-function value as the initializer will trigger a runtime error (e.g., NUXT_E7007) [2]. 2. Purpose of the Initializer: The initializer function runs only once (on the server) to set the initial value, which is then serialized into the payload for hydration [2]. 3. Optionality: You can defineuseStatewith only a key: const state = useState('my-key') // state.value will be undefined if not previously set [1] 4. TypeScript: If you omit the initializer, you may need to explicitly define the type of the state (e.g., useState<string | undefined>('key')) to avoid type errors in your application [1]. If you find that your state is unexpectedly undefined, ensure that the component or composable using it is actually triggering the assignment, or provide a default value via the factory function: const state = useState('my-key', => 'default value') [1]Citations:
π Script executed:
Repository: TanStack/query
Length of output: 40067
Guard the Nuxt state before calling
hydrate.In both
examples/vue/nuxt3/plugins/vue-query.tsanddocs/framework/vue/guides/ssr.md,useStatehas no initializer, sovueQueryState.valuecan beundefinedwhen the payload key is absent.!== nullstill enters the client path. Thehydratere-export then dereferencesdehydratedState.mutations, causing aTypeError. Use a nullish guard or initializeuseStatewith() => nullin both copies.π Affects 2 files
examples/vue/nuxt3/plugins/vue-query.ts#L31-L31(this comment)docs/framework/vue/guides/ssr.md#L45-L45π€ Prompt for AI Agents