hydration-strat-media.html 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <script src="../../dist/vue.global.js"></script>
  2. <div>resize the window width to < 500px to hydrate</div>
  3. <div id="app"><button>0</button></div>
  4. <script>
  5. window.isHydrated = false
  6. const {
  7. createSSRApp,
  8. defineAsyncComponent,
  9. h,
  10. ref,
  11. onMounted,
  12. hydrateOnMediaQuery,
  13. } = Vue
  14. const Comp = {
  15. props: {
  16. value: Boolean,
  17. },
  18. setup(props) {
  19. const count = ref(0)
  20. onMounted(() => {
  21. console.log('hydrated')
  22. window.isHydrated = true
  23. })
  24. return () => {
  25. props.value
  26. return h('button', { onClick: () => count.value++ }, count.value)
  27. }
  28. },
  29. }
  30. const AsyncComp = defineAsyncComponent({
  31. loader: () => Promise.resolve(Comp),
  32. hydrate: hydrateOnMediaQuery('(max-width:500px)'),
  33. })
  34. createSSRApp({
  35. setup() {
  36. onMounted(() => {
  37. window.isRootMounted = true
  38. })
  39. const show = (window.show = ref(true))
  40. return () => h(AsyncComp, { value: show.value })
  41. },
  42. }).mount('#app')
  43. </script>