vuejs / vue

This is the repo for Vue 2. For Vue 3, go to https://github.com/vuejs/core
http://v2.vuejs.org
MIT License
207.83k stars 33.68k forks source link

Did Vue hydrate successfully? #5907

Closed bjunc closed 7 years ago

bjunc commented 7 years ago

What problem does this feature solve?

According to the SSR hydration docs:

In development mode, Vue will assert the client-side generated virtual DOM tree matches the DOM structure rendered from the server. If there is a mismatch, it will bail hydration, discard existing DOM and render from scratch. In production mode, this assertion is disabled for maximum performance.

How would one know if hydration succeeded/failed? If "this assertion is disabled" in production, does that mean Vue defaults to discarding the DOM, or does Vue assume the server sent the proper markup? I would imagine a failed hydration would be something that would be nice to capture (eg. via Raven.captureException()) so it can be tracked / fixed.

What does the proposed API look like?

If there is indeed a "hydration mode", can that exposed as a global Vue.mode === 'hydration' (or Vue.hydrationMode === true if there are multiple "modes" that Vue can run in simultaneously)? Can a boolean/exception be exposed to let the developer know hydration succeeded?

yyx990803 commented 7 years ago
bjunc commented 7 years ago

Sorry to reopen, but your answer doesn't address the primary concern (thanks for responding though!).

What happens if (in prod mode) Vue cannot hydrate? Does the app become static at that point?

yyx990803 commented 7 years ago

@bjunc it can't happen because prod mode only strips away warnings, all behavior logic remains exactly the same. If it works in dev, it will work in prod.

bjunc commented 7 years ago

I believe we have a good business case for SSR of WYSIWYG content. The big reason I am pursuing SSR, is for the SEO; which we're currently not getting with valuable user-generated content. So, I can't just trust that it works in dev. I want to make sure that a content admin doesn't brick the app by saving improper HTML in production (despite checks we might put in place).

I don't want to go off-topic, but if there is a better way to address the SEO aspect of dynamic content, maybe this a moot topic.

yyx990803 commented 7 years ago

So you are allowing users to save arbitrary HTML, or just that you worry there could be potential XSS?

If you use v-html to display user-generated content, Vue won't attempt to diff/hydrate anything inside that node, so it can't lead to hydration failures.

Otherwise, pure VDOM-based hydration can only possibly fail when there is a bug in Vue.

Finally, hydration failures in prod will also cause Vue to throw away existing DOM and render afresh, so it won't "brick" the app.

bjunc commented 7 years ago

Well, we take proactive measures, but I also want to have reactive measures. So no, not "arbitrary" HTML, but I'd feel better if I had redundancy checks on both the server and the client.

That's really great to hear about v-html though. I think that ultimately addresses the concern.

Also good to hear about the refresh, although it would defeat the point of the SEO; which is why I figured catching an exception could help us pinpoint the pages with the issues.

I think I'm good knowing v-html isn't part of the diff/hydration process. Thanks for the back-and-forth.

iloginow commented 6 years ago

@yyx990803

it can't happen because prod mode only strips away warnings, all behavior logic remains exactly the same. If it works in dev, it will work in prod.

Well, it did actually happen! Here it is https://github.com/iloginow/vue-base-template and it actually seems very hard to debug)

bjunc commented 6 years ago

This actually reared its head again for me. The user entered bad markup (no closing tag) into a WYSIWYG. This not only caused the page not to render server-side, but client-side render failed as well. Having a way of catching hydration failure would at least give me a way of knowing that certain pages are failing due to user generated content.

curtisbelt commented 6 years ago

@bjunc

I also need SSR for SEO, and will be displaying HTML from WordPress content -- so this is a huge concern for me as well.

For your latest issue, was the content inside v-html? Can you share your