hydration-strat-interaction.html 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <script src="../../dist/vue.global.js"></script>
  2. <div>click 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 isFragment = location.search.includes('?fragment')
  11. if (isFragment) {
  12. document.getElementById('app').innerHTML =
  13. `<!--[--><!--[--><span>one</span><!--]--><button>0</button><span>two</span><!--]-->`
  14. }
  15. window.isHydrated = false
  16. const {
  17. createSSRApp,
  18. defineAsyncComponent,
  19. h,
  20. ref,
  21. onMounted,
  22. hydrateOnInteraction,
  23. } = Vue
  24. const Comp = {
  25. setup() {
  26. const count = ref(0)
  27. onMounted(() => {
  28. console.log('hydrated')
  29. window.isHydrated = true
  30. })
  31. return () => {
  32. const button = h(
  33. 'button',
  34. { onClick: () => count.value++ },
  35. count.value,
  36. )
  37. if (isFragment) {
  38. return [[h('span', 'one')], button, h('span', 'two')]
  39. } else {
  40. return button
  41. }
  42. }
  43. },
  44. }
  45. const AsyncComp = defineAsyncComponent({
  46. loader: () => Promise.resolve(Comp),
  47. hydrate: hydrateOnInteraction(['click', 'wheel']),
  48. })
  49. createSSRApp({
  50. setup() {
  51. onMounted(() => {
  52. window.isRootMounted = true
  53. })
  54. return () => h(AsyncComp)
  55. },
  56. }).mount('#app')
  57. </script>