Skip to content

Hydration detection #1723

Open
Open
@CyberAP

Description

@CyberAP

What problem does this feature solve?

When an app is server rendered there is a case when server side state does not correspond to client side state on an init lifecycle. Take for example a window.matchMedia and conditional rendering depending on matching media queries, the actual media query matching happens after the created stage either on beforeMount or mounted.

If you take the mounted approach the downside is that client side rendered applications would also be forced into this mode of re-rendering after the initial render because there's no reliable way (at least that I am aware of) to detect that this component is being hydrated. With a beforeMount approach you'll certainly get hydration errors in production, which is a no go.

As a workaround your component can accept a prop like ssr or check for common SSR solutions presence like nuxt. Doing this for every isomorphic Vue library feels like a barrier for developers who want to support both client and server side rendering.

What does the proposed API look like?

Would it be possible to add a property on a component instance to indicate that in fact this component has hydration undergoing to make an informed decision what state update strategy to take?

{
  beforeMount() {
    if (this._hydrating) {
      bailout()
    } else {
      updateState()
    }
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    ✨ feature requestNew feature or requesthas PRA pull request has already been submitted to solve the issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions