UNPKG

8.2 kBMarkdownView Raw
1<h1><img width="500" height="200" alt="Tailor" src="https://rawgithub.com/zalando/tailor/master/logo/tailor-logo.svg"></h1>
2
3[![NPM](https://nodei.co/npm/node-tailor.png)](https://npmjs.org/package/node-tailor)
4
5[![Build Status](https://travis-ci.org/zalando/tailor.svg?branch=master)](https://travis-ci.org/zalando/tailor)
6[![Test Coverage](https://codecov.io/github/zalando/tailor/coverage.svg?precision=0)](https://codecov.io/github/zalando/tailor)
7
8## npm status
9[![downloads](https://img.shields.io/npm/dt/node-tailor.svg)](https://npmjs.org/package/node-tailor)
10[![version](https://img.shields.io/npm/v/node-tailor.svg)](https://npmjs.org/package/node-tailor)
11
12
13Tailor 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](https://www.oreilly.com/ideas/better-streaming-layouts-for-frontend-microservices-with-tailor) as "a library that provides a middleware which you can integrate into any Node.js server." It's partially inspired by Facebook’s [BigPipe](https://www.facebook.com/notes/facebook-engineering/bigpipe-pipelining-web-pages-for-high-performance/389414033919/), but developed in an ecommerce context.
14
15Some of Tailor's features and benefits:
16
17- **Composes pre-rendered markup on the backend**. This is important for SEO and fastens the initial render.
18- **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.
19- **Enforces performance budget**. This is quite challenging otherwise, because there is no single point where you can control performance.
20- **Fault Tolerance**. Render the meaningful output, even if a page fragment has failed or timed out.
21
22Tailor is part of [Project Mosaic](https://www.mosaic9.org/), which aims to help developers create microservices for the frontend. The Mosaic also includes an extendable HTTP router for service composition ([Skipper](https://github.com/zalando/skipper)) with related RESTful API that stores routes ([Innkeeper](https://github.com/zalando/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.
23
24## Why a Layout Service?
25
26Microservices 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.
27
28What 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.
29
30## Installation
31
32Begin using Tailor with:
33
34`npm i node-tailor --save`
35
36```javascript
37const http = require('http');
38const Tailor = require('node-tailor');
39const tailor = new Tailor({/* Options */});
40const server = http.createServer(tailor.requestHandler);
41server.listen(process.env.PORT || 8080);
42```
43
44## Options
45
46* `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({})`
47* `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.
48Default implementation [`lib/fetch-template.js`](https://github.com/zalando/tailor/blob/master/lib/fetch-template.js) fetches the template from the file system
49* `templatesPath` - To specify the path where the templates are stored locally, Defaults to `/templates/`
50* `fragmentTag` - Name of the fragment tag, defaults to `fragment`
51* `handledTags` - An array of custom tags, check [`tests/handle-tag`](https://github.com/zalando/tailor/blob/master/tests/handle-tag.js) for more info
52* `handleTag(request, tag)` - Receives a tag or closing tag and serializes it to a string or returns a stream
53* `filterHeaders(attributes, request)` - Function that filters the request headers that are passed to fragment request, check default implementation in [`lib/filter-headers`](https://github.com/zalando/tailor/blob/master/lib/filter-headers.js)
54* `requestFragment(filterHeaders)(url, attributes, request)` - Function that returns a promise of request to a fragment server, check the default implementation in [`lib/request-fragment`](https://github.com/zalando/tailor/blob/master/lib/request-fragment.js)
55* `amdLoaderUrl` - URL to AMD loader. We use [RequireJS from cdnjs](https://cdnjs.com/libraries/require.js) as deafult
56* `pipeInstanceName` - Pipe instance name that is available in the browser window for consuming frontend hooks.
57* `pipeAttributes(attributes)` - Function that returns the minimal set of fragment attributes available on the frontend [hooks](https://github.com/zalando/tailor/blob/master/docs/hooks.md).
58
59# Template
60
61Tailor uses [parse5](https://github.com/inikulin/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.
62
63```html
64<html>
65<head>
66 <script type="fragment" src="http://assets.domain.com"></script>
67</head>
68<body>
69 <fragment src="http://header.domain.com"></fragment>
70 <fragment src="http://content.domain.com" primary></fragment>
71 <fragment src="http://footer.domain.com" async></fragment>
72</body>
73</html>
74```
75
76## Fragment attributes
77
78* *id* — optional unique identifier (autogenerated)
79* *src* — URL of the fragment
80* *primary* — denotes a fragment that sets the response code of the page
81* *timeout* - optional timeout of fragment in milliseconds (default is 3000)
82* *async* — postpones the fragment until the end of body tag
83* *public* — by default, Tailor forwards the headers it gets from upstream to the fragments. To prevent this from happening, use the public attribute.
84* *fallback-src* - URL of the fallback fragment in case of timeout/error on the current fragment
85
86## Fragment server
87
88A 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 `example/fragment.js` for the draft implementation.
89
90A 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.
91
92**Note: For compatability with AWS the `Link` header can also be passed as `x-amz-meta-link`**
93
94## Concepts
95
96Some of the concepts in tailor are described in detail on the specific docs.
97
98+ [Events](https://github.com/zalando/tailor/blob/master/docs/Events.md)
99+ [Base Templates](https://github.com/zalando/tailor/blob/master/docs/Base-Templates.md)
100+ [Hooks](https://github.com/zalando/tailor/blob/master/docs/hooks.md)
101+ [Performance](https://github.com/zalando/tailor/blob/master/docs/Performance.md)
102
103# Examples
104
105```sh
106git clone git@github.com:zalando/tailor.git && cd tailor
107npm install // install depedencies
108```
109
110**Note: Please run the examples with node versions > 6.0.0**
111
112+ Basic - `node examples/basic`
113
114+ CSS and JS - `node examples/basic-css-and-js`
115
116+ Multiple Fragments and AMD - `node examples/multiple-fragments-with-custom-amd`
117
118+ Fragment Performance - `node examples/fragment-performance`
119
120Go to [http://localhost:8080/index](http://localhost:8080/index) after running the specific example.
121
122### Contributing
123
124Please check the Contributing guidelines [here](https://github.com/zalando/tailor/blob/master/CONTRIBUTING.md)
125
126# Benchmark
127
128To start running benchmark execute `npm run benchmark` and wait for couple of seconds to see the results.