hydration-strat-interaction.html 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <script src="../../dist/vue.global.js"></script>
  2. <div>click to hydrate</div>
  3. <div id="app"><button>0</button></div>
  4. <style>body { margin: 0 }</style>
  5. <script>
  6. const isFragment = location.search.includes('?fragment')
  7. if (isFragment) {
  8. document.getElementById('app').innerHTML =
  9. `<!--[--><!--[--><span>one</span><!--]--><button>0</button><span>two</span><!--]-->`
  10. }
  11. window.isHydrated = false
  12. const { createSSRApp, defineAsyncComponent, h, ref, onMounted, hydrateOnInteraction } = Vue
  13. const Comp = {
  14. setup() {
  15. const count = ref(0)
  16. onMounted(() => {
  17. console.log('hydrated')
  18. window.isHydrated = true
  19. })
  20. return () => {
  21. const button = h('button', { onClick: () => count.value++ }, count.value)
  22. if (isFragment) {
  23. return [[h('span', 'one')], button, h('span', 'two')]
  24. } else {
  25. return button
  26. }
  27. }
  28. },
  29. }
  30. const AsyncComp = defineAsyncComponent({
  31. loader: () => Promise.resolve(Comp),
  32. hydrate: hydrateOnInteraction(['click', 'wheel'])
  33. })
  34. createSSRApp({
  35. setup() {
  36. onMounted(() => {
  37. window.isRootMounted = true
  38. })
  39. return () => h(AsyncComp)
  40. }
  41. }).mount('#app')
  42. </script>