Lazy Hydration: Possible to disable hydration entirely? #12581
Unanswered
michaelhthomas
asked this question in
Help/Questions
Replies: 1 comment
-
I managed to do so by creating a wrapper component with this util import {defineAsyncComponent, defineComponent, h, resolveComponent} from 'vue';
/**
* A wrapper around async component in order to
* lazily hydrate it, given a strategy. This way
* the JS file of the component is only downloaded
* when the hydration kicks in.
*
* Note: if no strategy is provided, then the
* hydration never happens and the JS file is
* not fetched at all.
*
* @param {Object} options
* @param {string} options.name - The name of the component
* @param {Function} options.loader - The loader function
* @param {Function} [options.strategy] - The hydration strategy to use
* @returns {VueComponent}
*/
const getLazilyHydratedComponent = ({name, loader, strategy = () => {}}) =>
defineAsyncComponent({
loader: () =>
new Promise(resolve => {
resolve(
defineComponent({
components: {
[name]: defineAsyncComponent({
loader
})
},
render() {
return h(resolveComponent(name));
}
})
);
}),
hydrate: strategy
});
export default getLazilyHydratedComponent; You can just use it like this getLazilyHydratedComponent({
name: 'ComponentName',
loader: () => import('path/to/ComponentName.vue'),
strategy: hydrateOnVisible() // If no strategy provided, acts like hydrateNever
}) |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Vue 3.5 added support for lazy component hydration, which is very exciting! I was previously using
vue3-lazy-hydration
for this functionality, which provided a helper namedhydrateNever
which would entirely disable component loading / hydration. My understanding is that something similar could be achieved by passing a do-nothing function as the hydration helper, as shown below.Unfortunately, though I believe this does prevent the component from being hydrated, the component module is still loaded unnecessarily. Is I there any way to prevent the component from being loaded entirely (i.e. prevent the
loader
function from being called), matching the behavior ofhydrateNever
fromvue3-lazy-hydration
?Beta Was this translation helpful? Give feedback.
All reactions