• This repository has been archived on 05/Dec/2022
  • Stars
    star
    1,728
  • Rank 26,975 (Top 0.6 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created almost 9 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A streaming layout service for front-end microservices

Tailor

NPM Build Status Test Coverage OpenTracing Badge

npm status

downloads version

Tailor is a layout service that uses streams to compose a web page from fragment services. O'Reilly describes it in the title of this blog post as "a library that provides a middleware which you can integrate into any Node.js server." It's partially inspired by Facebook’s BigPipe, but developed in an ecommerce context.

Some of Tailor's features and benefits:

  • Composes pre-rendered markup on the backend. This is important for SEO and fastens the initial render.
  • Ensures a fast Time to First Byte. Tailor requests fragments in parallel and streams them as soon as possible, without blocking the rest of the page.
  • Enforces performance budget. This is quite challenging otherwise, because there is no single point where you can control performance.
  • Fault Tolerance. Render the meaningful output, even if a page fragment has failed or timed out.

Tailor is part of Project Mosaic, which aims to help developers create microservices for the frontend. The Mosaic also includes an extendable HTTP router for service composition (Skipper) with related RESTful API that stores routes (Innkeeper); more components are in the pipeline for public release. If your front-end team is making the monolith-to-microservices transition, you might find Tailor and its available siblings beneficial.

Why a Layout Service?

Microservices get a lot of traction these days. They allow multiple teams to work independently from each other, choose their own technology stacks and establish their own release cycles. Unfortunately, frontend development hasn’t fully capitalized yet on the benefits that microservices offer. The common practice for building websites remains “the monolith”: a single frontend codebase that consumes multiple APIs.

What if we could have microservices on the frontend? This would allow frontend developers to work together with their backend counterparts on the same feature and independently deploy parts of the website — “fragments” such as Header, Product, and Footer. Bringing microservices to the frontend requires a layout service that composes a website out of fragments. Tailor was developed to solve this need.

Installation

Begin using Tailor with:

yarn add node-tailor
const http = require('http');
const Tailor = require('node-tailor');
const tailor = new Tailor({/* Options */});
const server = http.createServer(tailor.requestHandler);
server.listen(process.env.PORT || 8080);

Options

  • fetchContext(request) - Function that returns a promise of the context, that is an object that maps fragment id to fragment url, to be able to override urls of the fragments on the page, defaults to Promise.resolve({})
  • fetchTemplate(request, parseTemplate) - Function that should fetch the template, call parseTemplate and return a promise of the result. Useful to implement your own way to retrieve and cache the templates, e.g. from s3. Default implementation lib/fetch-template.js fetches the template from the file system
  • templatesPath - To specify the path where the templates are stored locally, Defaults to /templates/
  • fragmentTag - Name of the fragment tag, defaults to fragment
  • handledTags - An array of custom tags, check tests/handle-tag for more info
  • handleTag(request, tag, options, context) - Receives a tag or closing tag and serializes it to a string or returns a stream
  • filterRequestHeaders(attributes, request) - Function that filters the request headers that are passed to fragment request, check default implementation in lib/filter-headers
  • filterResponseHeaders(attributes, headers) - Function that maps the given response headers from the primary fragment request to the final response
  • maxAssetLinks - Number of Link Header directives for CSS and JS respected per fragment - defaults to 1
  • requestFragment(filterHeaders)(url, attributes, request) - Function that returns a promise of request to a fragment server, check the default implementation in lib/request-fragment
  • amdLoaderUrl - URL to AMD loader. We use RequireJS from cdnjs as default
  • pipeInstanceName - Pipe instance name that is available in the browser window for consuming frontend hooks.
  • pipeAttributes(attributes) - Function that returns the minimal set of fragment attributes available on the frontend hooks.
  • tracer - Opentracing compliant Tracer implementation.

Template

Tailor uses parse5 to parse the template, where it replaces each fragmentTag with a stream from the fragment server and handledTags with the result of handleTag function.

<html>
<head>
    <script type="fragment" src="http://assets.domain.com"></script>
</head>
<body>
    <fragment src="http://header.domain.com"></fragment>
    <fragment src="http://content.domain.com" primary></fragment>
    <fragment src="http://footer.domain.com" async></fragment>
</body>
</html>

Fragment attributes

  • id - optional unique identifier (autogenerated)
  • src - URL of the fragment
  • primary - denotes a fragment that sets the response code of the page
  • timeout - optional timeout of fragment in milliseconds (default is 3000)
  • async - postpones the fragment until the end of body tag
  • public - to prevent tailor from forwarding filtered request headers from upstream to the fragments.
  • fallback-src - URL of the fallback fragment in case of timeout/error on the current fragment

Other attributes are allowed and will be passed as well to relevant functions (eg. filterRequestHeaders, filterResponseHeaders, etc.)

Fragment server

A fragment is an http(s) server that renders only the part of the page and sets Link header to provide urls to CSS and JavaScript resources. Check examples/basic-css-and-js/index.js for a draft implementation.

A JavaScript of the fragment is an AMD module, that exports an init function, that will be called with DOM element of the fragment as an argument.

Tailor will not follow redirects even if fragment response contains 'Location' Header, that is on purpose as redirects can introduce unwanted latency. Fragments with the attribute primary can do a redirect since it controls the status code of the page.

Note: For compatability with AWS the Link header can also be passed as x-amz-meta-link

Passing information to fragments

By default, the incoming request will be used to selecting the template.

So to get the index.html template you go to /index.

If you want to listen to /product/my-product-123 to go to product.html template, you can change the req.url to /product.

Every header is filtered by default to avoid leaking information, but you can give the original URI and host by adding it to the headers, x-request-host and x-request-uri, then reading in the fragment the headers to know what product to fetch and display.

http
    .createServer((req, res) => {
        req.headers['x-request-uri'] = req.url
        req.url = '/index'

        tailor.requestHandler(req, res);
    })
    .listen(8080, function() {
        console.log('Tailor server listening on port 8080');
    });

Concepts

Some of the concepts in Tailor are described in detail on the specific docs.

OpenTracing

Tailor has out of the box distributed tracing instrumentation with OpenTracing. It will pick up any span context on the ingress HTTP request and propagate it to the existing Remote Procedure Calls (RPCs).

Currently, only the fetching of fragments is instrumented providing some additional details like the fragment tag, attributes and some logging payload like the stack trace for errors.

Examples

# Get a copy of the repository
git clone https://github.com/zalando/tailor.git

# Change to the folder
cd tailor

# Install dependencies
yarn
  • Basic - node examples/basic
  • CSS and JS - node examples/basic-css-and-js
  • Multiple Fragments and AMD - node examples/multiple-fragments-with-custom-amd
  • Fragment Performance - node examples/fragment-performance

Go to http://localhost:8080/index after running the specific example.

Note: Please run the examples with node versions > 6.0.0


Single-page application examples are also available:

Benchmark

To start running benchmark execute npm run benchmark and wait for couple of seconds to see the results.

Contributing

Please check the Contributing guidelines here.

More Repositories

1

patroni

A template for PostgreSQL High Availability with Etcd, Consul, ZooKeeper, or Kubernetes
Python
6,267
star
2

postgres-operator

Postgres operator creates and manages PostgreSQL clusters running in Kubernetes
Go
3,686
star
3

skipper

An HTTP router and reverse proxy for service composition, including use cases like Kubernetes Ingress
Go
3,088
star
4

restful-api-guidelines

A model set of guidelines for RESTful APIs and Events, created by Zalando
CSS
2,605
star
5

zalenium

A flexible and scalable container based Selenium Grid with video recording, live preview, basic auth & dashboard.
Java
2,385
star
6

SwiftMonkey

A framework for doing randomised UI testing of iOS apps
Swift
1,947
star
7

logbook

An extensible Java library for HTTP request and response logging
Java
1,788
star
8

tech-radar

Visualizing our technology choices
1,581
star
9

spilo

Highly available elephant herd: HA PostgreSQL cluster using Docker
Python
1,225
star
10

intellij-swagger

A plugin to help you easily edit Swagger and OpenAPI specification files inside IntelliJ IDEA
Java
1,172
star
11

problem-spring-web

A library for handling Problems in Spring Web MVC
Java
1,031
star
12

nakadi

A distributed event bus that implements a RESTful API abstraction on top of Kafka-like queues
Java
928
star
13

zally

A minimalistic, simple-to-use API linter
Kotlin
903
star
14

problem

A Java library that implements application/problem+json
Java
869
star
15

zalando-howto-open-source

Open Source guidance from Zalando, Europe's largest online fashion platform
799
star
16

go-keyring

Cross-platform keyring interface for Go
Go
689
star
17

gin-oauth2

Middleware for Gin Framework users who also want to use OAuth2
Go
580
star
18

zappr

An agent that enforces guidelines for your GitHub repositories
JavaScript
542
star
19

pg_view

Get a detailed, real-time view of your PostgreSQL database and system metrics
Python
494
star
20

engineering-principles

Our guidelines for building new applications and managing legacy systems
376
star
21

gulp-check-unused-css

A build tool for checking your HTML templates for unused CSS classes
CSS
359
star
22

zmon

Real-time monitoring of critical metrics & KPIs via elegant dashboards, Grafana3 visualizations & more
Shell
355
star
23

expan

Open-source Python library for statistical analysis of randomised control trials (A/B tests)
Python
325
star
24

PGObserver

A battle-tested, flexible & comprehensive monitoring solution for your PostgreSQL databases
Python
316
star
25

riptide

Client-side response routing for Spring
Java
292
star
26

jackson-datatype-money

Extension module to properly support datatypes of javax.money
Java
240
star
27

grafter

Grafter is a library to configure and wire Scala applications
Scala
240
star
28

opentracing-toolbox

Best-of-breed OpenTracing utilities, instrumentations and extensions
Java
180
star
29

elm-street-404

A fun WebGL game built with Elm
Elm
176
star
30

tokens

Java library for conveniently verifying and storing OAuth 2.0 service access tokens
Java
169
star
31

innkeeper

Simple route management API for Skipper
Scala
166
star
32

public-presentations

List of public talks by Zalando Tech: meetup presentations, recorded conference talks, slides
165
star
33

python-nsenter

Enter kernel namespaces from Python
Python
139
star
34

faux-pas

A library that simplifies error handling for Functional Programming in Java
Java
132
star
35

dress-code

The official style guide and framework for all Zalando Brand Solutions products
CSS
129
star
36

beard

A lightweight, logicless templating engine, written in Scala and inspired by Mustache
Scala
121
star
37

friboo

Utility library for writing microservices in Clojure, with support for Swagger and OAuth
Clojure
117
star
38

spring-cloud-config-aws-kms

Spring Cloud Config add-on that provides encryption via AWS KMS
Java
99
star
39

zalando.github.io

Open Source Documentation and guidelines for Zalando developers
HTML
86
star
40

failsafe-actuator

Endpoint library for the failsafe framework
Java
52
star
41

package-build

A toolset for building system packages using Docker and fpm-cookery
Ruby
35
star
42

ghe-backup

Github Enterprise backup at ZalandoTech (Kubernetes, AWS, Docker)
Shell
30
star
43

rds-health

discover anomalies, performance issues and optimization within AWS RDS
Go
26
star
44

backstage-plugin-api-linter

API Linter is a quality assurance tool that checks the compliance of API's specifications to Zalando's API rules.
TypeScript
12
star
45

.github

Standard github health files
1
star