hydration-strat-visible.html 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <script src="../../dist/vue.global.js"></script>
  2. <div style="height: 1000px">scroll to the bottom to hydrate</div>
  3. <div id="app"><button>0</button></div>
  4. <style>
  5. body {
  6. margin: 0;
  7. }
  8. </style>
  9. <script>
  10. const rootMargin = location.search.match(/rootMargin=(\d+)/)?.[1] ?? 0
  11. const isFragment = location.search.includes('?fragment')
  12. if (isFragment) {
  13. document.getElementById('app').innerHTML =
  14. `<!--[--><!--[--><span>one</span><!--]--><button>0</button><span>two</span><!--]-->`
  15. }
  16. window.isHydrated = false
  17. const {
  18. createSSRApp,
  19. defineAsyncComponent,
  20. h,
  21. ref,
  22. onMounted,
  23. hydrateOnVisible,
  24. } = Vue
  25. const Comp = {
  26. setup() {
  27. const count = ref(0)
  28. onMounted(() => {
  29. console.log('hydrated')
  30. window.isHydrated = true
  31. })
  32. return () => {
  33. const button = h(
  34. 'button',
  35. { onClick: () => count.value++ },
  36. count.value,
  37. )
  38. if (isFragment) {
  39. return [[h('span', 'one')], button, h('span', 'two')]
  40. } else {
  41. return button
  42. }
  43. }
  44. },
  45. }
  46. const AsyncComp = defineAsyncComponent({
  47. loader: () => Promise.resolve(Comp),
  48. hydrate: hydrateOnVisible({ rootMargin: rootMargin + 'px' }),
  49. })
  50. createSSRApp({
  51. setup() {
  52. onMounted(() => {
  53. window.isRootMounted = true
  54. })
  55. return () => h(AsyncComp)
  56. },
  57. }).mount('#app')
  58. </script>