I want to create a custom Vue
directive that lets me select components on my page which I want to hydrate. In other words, this is what I want to archive
- I render my
Vue
app on the server (ssr
) I attach a directive to some components, like this:
<template> <div v-hydrate @click="do-something"> I will be hydrated</div> </template>
I send my code to the client and only those components that have the
v-hydrate
property will be hydrated (asroot
elements) on the client.
I want to achieve this roughly this way:
I will create a directives that marks and remembers components:
import Vue from "vue";
Vue.directive("hydrate", {
inserted: function(el, binding, vnode) {
el.setAttribute("data-hydration-component", vnode.component.name);
}
});
My idea is that in my inserted
method write a data-attribute to the server-rendered element that I can read out in the client and then hydrate my component with.
Now I have 2 questions:
- Is that a feasible approach
- How do I get the component name in
el.setAttribute
?vnode.component.name
is just dummy code and does not exist this way.
PS: If you want to know why I only want to hydrate parts of my website: It's ads. They mess with the DOM which breaks Vue.