Giter Club home page Giter Club logo

render-hydratable's Introduction

Render Hydratable

This is a work in progress and it's practially verbatim extracted from a project I was doing, not really production-ready, and the API is very confusing, use at your own risk. It's also completely untested.

My motivation to make this library was to be able to use any kind of component from any framework on an Eleventy site with static rendering and progressive partial hydration (hydrate only the components that need hydration as they become visible in the page).

The lib/render folder files are more a wish-list than real files, right now only Preact and Nunjucks are actually written. I'll separate each renderer to a different repository in the near future.

Basic usage

Install

$ yarn add render-hydratable

Usage

On /src/components/Person.js

const { h } = require('preact')
const { html } = require(`htm/preact`)

const Person = ({name, age}) => html`
  <button onClick=${() => console.log(name, age)}>
    ${name} is ${age} years old
  </button>
`

Person.hydratable = true

module.exports = Person

Then directly on JavaScript

const renderHydratable = require('render-hydratable/lib/render-hydratable')
const components = renderHydratable([`${__dirname}/src/components`])

components('Person', {name: "Bob", age: 27})

Or on .eleventy.js as a shortcode

config.addShortcode(
    'renderHydratable',
    renderHydratable([`${__dirname}/src/components`])
  )

If the component is marked as hydratable, then it will additionally render the following hydration marker before the component:

<script
  data-component="Person"
  data-props="{\"name\":\"Bob\",\"age\":27}"
  data-strategy="preact"
></script>

Technically it support hydrating with multiple frameworks on the same page.

Rehydration

On main JS file

const rehydration = require('render-hydratable/lib/rehydration')

// In the future my intention is to generate this automatically from the
// components used on the site
const componentsMap = {
  Person: require('./components/Person'),
}

// Also this from the frameworks used on the site
const strategies = {
  preact: require('render-hydratable/lib/render/with-preact').rehydrate,
}

render-hydratable's People

Contributors

zequez avatar

Stargazers

Mark Pinero avatar

Watchers

 avatar James Cloos 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.