-
-
Notifications
You must be signed in to change notification settings - Fork 33.8k
Node attribute value is not reactive after client side hydration #11398
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
Comments
I don't think it's possible to use random values with hydration like that because the html rendered by the server should be the same as the one generated by the client. By using random values, you are pretty much forcing an hydration mismatch because they will (almost) always differ. But I don't understand why there is no error |
The value of val in data is a certain value after initialization, so when run $forceUpdate() will not run Math.floor(Math.random() * 10000000), the value of val is not changed. |
@posva Perhaps I shouldn't have used |
@cinsanity I'm not sure I understand. I attached a screenshot to give more details. I expected |
It has to really be after hydration. In that case it might be related to one of these linked issues #10260 (see linked issues as well) |
I am very curious about why |
We ran over the exact same issue with Nuxt when creating a uid in the created hook, our workaround was creating a new ID in the mounted hook. |
Hi, I encountered the same issue If we set those ID's in mounted, we will loose SEO gain from accessibility attributes Edit: putting a ref on the bugging node solve the issue |
For Vue 3, there's a built-in getSSRProps hook available via directives: <script setup lang="ts">
import type { Directive } from 'vue'
import { nanoid } from 'nanoid';
const vUniqueIds: Directive = {
created(el, binding) {
el.setAttribute('id', binding.value.id)
el.setAttribute('data-whatever', binding.value['data-whatever'])
},
getSSRProps(binding) {
return {
id: binding.value.id,
'data-whatever': binding.value['data-whatever']
}
},
}
</script>
<template>
<div>
<div v-unique-ids="{ id: nanoid(), 'data-whatever': nanoid() }">
hello
</div>
</div>
</template> |
Version
2.6.11
Reproduction link
https://codesandbox.io/s/ssr-reactivity-up6nh?file=/pages/index.vue
Steps to reproduce
div
does not match the myid value from the component (requires page refresh sometimes on first load).$forceUpdate()
(first button) does not update the id attribute of thediv
val
data (second button) property triggers the reactivityWhat is expected?
Every reference to the
myid
computed property should matchWhat is actually happening?
The id attribute of the
div
does not match themyid
valueThe same component used in client-side rendering does not exhibit this issue.
The text was updated successfully, but these errors were encountered: