Skip to content

feat: DOM update resumability API - #99

Closed
harlan-zw wants to merge 11 commits into
vueuse:mainfrom
harlan-zw:fix/avoid-ssr-hydration-flash
Closed

harlan-zw wants to merge 11 commits into
vueuse:mainfrom
harlan-zw:fix/avoid-ssr-hydration-flash

Conversation

@harlan-zw

@harlan-zw harlan-zw commented Sep 21, 2022 •

Copy link
Copy Markdown
Collaborator

Problem

See nuxt/framework#98, nuxt/framework#75, nuxt/nuxt#14438

Solution

Simple boolean ref for toggling the DOM updates.

The updateDOM calls will automatically be re-done when unpaused as they are wrapped in a watchEffects.

In Nuxt this allows you to wait for <Suspense> to resolve with the below code. I don't think it's possible to add a way for @vueuse/head to handle this generically without introducing a router dependency.

nuxtApp.hooks.hookOnce("page:finish", () => {
  // start pausing DOM updates when route changes (trigger immediately)
  useRouter().beforeEach(() => {
    head._pauseDOMUpdates.value = true
  })

  // watch for new route before unpausing dom updates (triggered after suspense resolved)
  watch(useRoute(), () => {
    head._pauseDOMUpdates.value = false
  })
})

Other notes

The Nuxt example module now completely removes the native meta module in favour of our custom implementation, which makes testing changes like this simpler.

@harlan-zw

Copy link
Copy Markdown
Collaborator Author

@danielroe Would you mind reviewing if you have a chance?

@harlan-zw harlan-zw changed the title fix: hydration intermediary state flash when SSR with <suspense> feat: improved SSR with <suspense> and DOM update pause API Sep 21, 2022
@harlan-zw harlan-zw changed the title feat: improved SSR with <suspense> and DOM update pause API fix: DOM flicker for SSR with <suspense>, introduce generic pause API Sep 21, 2022
@danielroe

Copy link
Copy Markdown
Contributor

The API for pausing updates looks good to me and I think this would resolve the nuxt-linked issue. (cc: @pi0)

I'm not 100% sure either about tracking node id from ssr -> client. In case there are issues, what do you think about exposing a fallback API for 'forcing' hydrating of the head? Nuxt, for example, could call this when we believe the page is ready (when all suspenses have resolved).

@harlan-zw

Copy link
Copy Markdown
Collaborator Author

Thanks for the review!

The code may have changed when I pinged you, but the tracking node id simply toggles the pausing. So if you wanted to force the hydration you could set this value to true.

nuxtApp.hooks.hookOnce("page:finish", () => {
  head._pauseDOMUpdates.value = false
})

I agree the tracking node seems a bit too risky though. For Nuxt you wouldn't need it if you can do the pause/unpause yourself. I'll move it to a separate PR

@harlan-zw harlan-zw changed the title fix: DOM flicker for SSR with <suspense>, introduce generic pause API feat: DOM update resumability API Sep 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants