Giter Club home page Giter Club logo

fiber-fragments's Introduction

Fragments

main Go Report Card License: MIT Taylor Swift

Fragments middleware for Fiber enables building microservices for the frontend.

A <fragment> symbolizes a part of a template that can be served by a singular microservice. Thus, making a fragment the contract between different services and teams within a large engineering organization. The middleware concurrently fetches those parts from the service and replaces it in the template. It supports GET and POST HTTP methods to fetcht the content. Related resources like CSS or JavaScript are injected via the HTTP LINK entity header field. A <fragment> can occure in the body element or the header element. See Example to learn more about using fragments.

Tailor by Zalando is prior art for this middleware.

Fragement(s)

A fragment will be hybrid-polymorphic (if this is a thing). On the server it is parsed and evaluate by the middleware. 🦄 In the browser it will be a web component that received data from the middleware (this is still work in progress ⚠️).

Server

  • src The source to fetch for replacement in the DOM
  • method can be of GET (default) or POST.
  • primary denotes a fragment that sets the response code of the page
  • id is an optional unique identifier (optional)
  • refis an optional forward reference to an id (optional)
  • timeout timeout of a fragement to receive in milliseconds (default is 300)
  • deferred is deferring the fetch to the browser
  • fallback is the fallback source in case of timeout/error on the current fragment

Example

Import the middleware package this is part of the Fiber web framework.

package main

import (
	"github.com/gofiber/fiber/v2"
	"github.com/gofiber/template/html"

	"github.com/github/fiber-fragments"
)

After you initiate your Fiber app, you can plugin in the fragments middleware. The middleware draws the templates for the fragments to load from the template engine. Thus it supports using all template engines supported by the Fiber team.

// Create a new engine
engine := html.New("./views", ".html")

// Pass the engine to the Views
app := fiber.New(fiber.Config{
	Views: engine,
})

// Associates the route with a specific template with fragments to render
app.Get("/index", fragments.Template(fragments.Config{}, "index", fiber.Map{}, "layouts/main"))

// this would listen to port 8080
app.Listen(":8080")
<html>
<head>
    <script type="fragment" src="assets"></script>
</head>
<body>
    <h1>Example</h1>
    <fragment src="fragment1.html"></fragment>
</body>
</html>

The example folder contains many examples. You can learn how to use a forward reference of content for fetching outer and inner content and replace either one.

Benchmark(s)

This is run on a MacBook Pro 16 inch locally. It is the example run.

  • Parsing a local template with extrapolation with the fragments
  • Parsing the fragments
  • Doing fragments
  • Inlining results and adding Link header resources to the output
echo "GET http://127.0.0.1:8080/index" | vegeta attack -duration=5s -rate 2000 | tee results.bin | vegeta report
  vegeta report -type=json results.bin > metrics.json
  cat results.bin | vegeta plot > plot.html
  cat results.bin | vegeta report -type="hist[0,100ms,200ms,300ms]"

Requests      [total, rate, throughput]         10000, 2000.26, 2000.15
Duration      [total, attack, wait]             5s, 4.999s, 285.172µs
Latencies     [min, mean, 50, 90, 95, 99, max]  183.725µs, 251.517µs, 226.993µs, 310.698µs, 394.601µs, 563.022µs, 1.347ms
Bytes In      [total, mean]                     6240000, 624.00
Bytes Out     [total, mean]                     0, 0.00
Success       [ratio]                           100.00%
Status Codes  [code:count]                      200:10000
Error Set:
Bucket           #      %        Histogram
[0s,     100ms]  10000  100.00%  ###########################################################################
[100ms,  200ms]  0      0.00%
[200ms,  300ms]  0      0.00%
[300ms,  +Inf]   0      0.00%

License

MIT

fiber-fragments's People

Contributors

katallaxie avatar mrcull avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

fiber-fragments's Issues

RFC Forward references for fragments

Allow <fragment /> to have reference for future content. <fragment id="12345" /> and <fragment ref="12345" /> allows to preload data and inject in templates. Replacing inner context from outer context by referencing fragments.

template.html

<!-- Layout -->
<fragment src="layout.html">
   <!-- Content -->
   <fragment id="content" src="content.html" />
</fragment>

layout.html

<div>
   <fragment ref="content" />
</div>

content.html

<ul>
  <li>1</li>
</ul>

Fetching parallel and replacement to final output 👇 .

<!-- Layout -->
<div>
  <!-- Content -->
   <ul>
      <li>1</li>
   </ul>
</div>

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.