vue: Uncaught TypeError: n.setAttribute is not a function when ssr client-side hydration
Version
2.6.10
Reproduction link
https://github.com/yinxin630/vue-issue-example
Steps to reproduce
- clone and npm i
- run
npm run buildandnpm run build:ssr - run
node src/ssr.js - render app.js with ssr get result
<div id="app" data-server-rendered="true"><!----><h2>2222222222</h2></div>
- open in browser. client-side hydration will throw error
Uncaught TypeError: n.setAttribute is not a function
What is expected?
client-side hydration success
What is actually happening?
throw error Uncaught TypeError: n.setAttribute is not a function
I found this when debug. vue call n.setAttribute('class', 'm-image-padding') but n is a comment node.
In app.js I use v-if. It will be true when ssr but be false when csr.
I don’t know if there is something wrong with my usage or the logic of vue client-side hydration.
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 8
- Comments: 15 (2 by maintainers)
Yes i have same issue any suggestion
In my case, the main issue is that when this error happens, everything else is prevented to execute, so instead of just breaking one part of the page, the whole rest of the page doesn’t work.
Edit: Not a single component gets its
mountedhook executed. Edit 2: I’m using Nuxt 2.15.8I have the exact same problem, a value is false on ssr, true on csr, I have a v-if to display or not a NuxtLink. Upon hydration, console displays
TypeError: el.setAttribute is not a function.Note :
yarn devv-showinstead ofv-ifv-ifhas a correspondingv-elsev-if was the issue. In SSR mode, i was using v-if in the layout. I removed the v-if with v-show and it worked like a charm.
I know why you do not have the problem if the
v-ifhas a correspondingv-else. It’s because withoutv-else, at ssr vue will render the node to a comment node, however comment node don’t have asetAttributemethod; but withv-else, vue will render the ‘else’ node, which is a vue node and has thesetAttributemethod。Also getting this error but not when running with
npm run devonly once I went to deploy I found there was problems related to said error.