Giter VIP home page Giter VIP logo

milla's Introduction

Build Status codecov npm version Known Vulnerabilities


Milla - Streaming Layout Service

Milla is a layout service that generates web pages from asynchronous fragments. It is a friendly and fast library that helps you optimizing your web pages with progressive rendering. Milla has built-in integration with ExpressJs .

Milla Progressive Rendering

Contributions

Pull requests are welcome!

Fragments

What is fragment and what makes it different from components?

We create component because of their reusabilities. We can use them anywhere we want, in a search bar, in a login popup or anything else. But fragments are encapsulated modules with specific business. Like an header, product container, footer. They are all independent from each other.

How Milla approaches to fragments?

Milla is designed to stream fragments asynchronously to the clients. Without any single XHR from browser, all fragments are delivered to clients when their data is ready. Like single page application but fully server side rendered. So it makes it SEO friendly and great for e-commerce websites. Milla shows placeholders on the clients browser while it fetches your data then replaces placeholder with real content. So it is really improving your time to first byte

You can easily improve your Speed Index!

How it works?

This is not a valid Milla usage, please see usage details below

Whenever user provides a html to milla, milla converts it to stream friendly format and flushes to user asynchronously.

Lets assume that our header service responds in 300 ms and product service in 140ms.

<html>
    <head>
        <milla-head></milla-head>
    </head>
    <body>
        <fragment name="header" css="pathtocss" js="pathtojs"></fragment>
        <div>A static content</div>
        <fragment name="product" css="pathtocss" js="pathtojs"></fragment>
    </body>
</html>

Converted:

<html>
    <head>
        <style>/* Fragment styles */</style>
        <script>/*Really small milla js. 118bytes*/</script>
    </head>
    <body>
        <div id="p_0">Header placeholder</div>
        <div>A static content</div>
        <div id="p_1">Product placeholder</div>
        <!-- User started load html dependencies, css files etc and your website is already rendered -->
        ... 140ms
        <!-- Milla recieved product data and flushed product content and js -->
        <div id="c_1" hidden></div>
        <script>/*use head script to change c_1 p_1; run header script */</script>
        <!-- Product is ready to be used -->
        ... 160ms
        <!-- Milla recieved header data and flushed product content and js -->
        <div id="c_1" hidden></div>
        <script>/*use head script to change c_1 p_1; run header script */</script>
        <!-- header is ready to be used -->
    </body>
</html>

Milla closes the stream.

Installation

Using npm

npm install milla --save

Using yarn

yarn add milla

Usage

You can check out demo folder

Page File

First of all, you should create a page html file.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Milla Title</title>
    <milla-head></milla-head>
</head>
<body>
    <fragment name="header" js="../test/dependencies/8.js" css="../test/dependencies/8.css"></fragment>
    <div>Middle Content</div>
    <fragment name="product" js="../test/dependencies/8_1.js"></fragment>
</body>
</html>

Then use your page file in your express route.

Express Route

    const Milla = require('../src');

    /* Optional, you can find default configs under src/configuration */
    Milla.config.set({
      minifyJs: false,
      minifyCss: false,
      minifyHtml: false
    });

    /* Create express route */
    app.get('/', Milla.express({
      htmlFile: path.join(__dirname, '../test/html/test8.html'), //Html with fragments
      data: { //fragment datas. You can use () => {}, {}, () => new Promise. Milla will handle them in most efficient way.
        header: (req) => {
          return {
            header: {
              menu: ['Item 1', 'Item 2', 'Item 3']
            }
          }
        },
        product: (req) => {
          return new Promise((resolve, reject) => {
            setTimeout(() => {
              resolve({product:{name:'Test'}});
            }, 4000 * Math.random())
          });
        }
      },
      fragments: { //Fragment rendering functions. (input) is coming from data. You can use your favorite template engine here (anything that returns string).
        header: {
          content: (input) => JSON.stringify(input)
        },
        product: {
          placeholder: () => '<div>Product placeholder</div>',
          content: (input) => {
            return JSON.stringify(input);
          }
        }
      }
    }));

Thats all! You have a great progressive rendering now.

milla's People

Contributors

acanguven avatar

Watchers

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