GithubHelp home page GithubHelp logo

isabella232 / hydrate Goto Github PK

View Code? Open in Web Editor NEW

This project forked from riot/hydrate

0.0 0.0 0.0 465 KB

Riot.js hydrate rendering for SSR applications

License: MIT License

JavaScript 94.31% Riot 5.69%

hydrate's Introduction

@riotjs/hydrate

Build Status NPM version NPM downloads MIT License

Installation

npm i -S riotjs @riotjs/hydrate

Usage

If you are using @riotjs/ssr you might prefer hydrating your server side rendered HTML enhancing your application user experience. Your users will get initially the static HTML (generated via @riotjs/ssr) that will be enhanced only when javascript application will be loaded.
@riotjs/hydrate will allow you avoiding any perceivable application flickering or input fields focus loss when the javascript components will replace the static rendered markup.

A good practice is to mount your Riot.js components exactly with the same initial properties on the server as on the client.

import hydrate from '@riotjs/hydrate'
import MyComponent from './my-component.riot'

const hydrateWithMyComponent = hydrate(MyComponent)

// hydrate the SSR DOM contained in the #root element
hydrateWithMyComponent(
  document.getElementById('root'),
  window.__INITIAL_APPLICATION_PROPS__
)

Callbacks

You can use the onBeforeHydrate and onHydrated callback in your components to setup your application internal state. Notice that these callbacks will be called only on the component hydrated and not on all its nested children components.

<my-component>
  <script>
    export default {
      onBeforeHydrate() {
        // do something before the hydration
      },
      onHydrated() {
        // do something after the hydration
      }
    }
  </script>
</my-component>

Caveats

The hydrate method will mount your components on a clone of your target node not yet in the DOM. If your component state relies on DOM computations like get​Bounding​Client​Rect and you don't want to use the onHydrated callback, you will need to use a requestAnimationFrame callback in your onMounted method to wait that your root node has replaced completely the initial static markup for example:

<my-component>
  <script>
    export default {
      onMounted() {
        // your root node is not yet in the DOM
        requestAnimationFrame(() => {
          // your root is in the DOM
        })
      },
    }
  </script>
</my-component>

hydrate's People

Contributors

gianlucaguarini avatar dependabot[bot] avatar nesterow avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.