├── .editorconfig ├── .eslintignore ├── .eslintrc ├── .gitignore ├── .travis.yml ├── CHANGELOG.md ├── CONTRIBUTING.md ├── LICENSE.txt ├── NOTICE.md ├── README.md ├── codecov.yml ├── docs ├── Base-Templates.md └── Events.md ├── examples ├── basic-css-and-js │ ├── index.js │ └── templates │ │ └── index.html ├── basic-typescript │ ├── README.md │ ├── index.ts │ ├── templates │ │ └── index.html │ └── tsconfig.json ├── basic │ ├── index.js │ └── templates │ │ └── index.html └── custom-tags │ ├── index.js │ └── templates │ └── index.html ├── index.d.ts ├── index.js ├── lib ├── errors.js ├── fetch-template.js ├── filter-headers.js ├── fragment.js ├── parse-link-header.js ├── parse-template.js ├── process-fragment-response.js ├── process-template.js ├── request-fragment.js ├── request-handler.js ├── serializer.js ├── streams │ ├── async-stream.js │ ├── buffer-concat-stream.js │ ├── content-length-stream.js │ ├── head-injector-stream.js │ ├── seobots-guard-stream.js │ └── stringifier-stream.js ├── template-cutter.js ├── tracing.js ├── transform.js ├── utils.js └── wait-fragment-responses.js ├── logo ├── README.md ├── tailor-logo.svg └── tailorx-logo.png ├── package.json ├── perf ├── benchmark.js ├── fragment-server.js └── loadtest.js └── tests ├── fetch-template.js ├── filter-headers.js ├── fragment.events.js ├── fragment.js ├── handle-tag.js ├── parse-link-header.js ├── parse-template.js ├── process-template.js ├── request-fragment.js ├── serializer.js ├── streams ├── async-stream.js ├── buffer-concat-stream.js ├── content-length-stream.js ├── header-injector-stream.js ├── seobots-guard-stream.js └── stringifier-stream.js ├── tailor.events.js ├── tailor.js ├── template-cutter.js └── transform.js /.editorconfig: -------------------------------------------------------------------------------- 1 | # EditorConfig is awesome: http://EditorConfig.org 2 | 3 | # top-most EditorConfig file 4 | root = true 5 | 6 | # For most files 7 | [*] 8 | end_of_line = lf 9 | insert_final_newline = true 10 | charset = utf-8 11 | trim_trailing_whitespace = true 12 | indent_style = space 13 | indent_size = 4 14 | 15 | # Override specific settings for npm generated json files to avoid big diffs 16 | [package.json] 17 | indent_size = 2 18 | 19 | [*.{yml,yaml}] 20 | indent_size = 2 21 | -------------------------------------------------------------------------------- /.eslintignore: -------------------------------------------------------------------------------- 1 | src/pipe.min.js 2 | coverage 3 | examples/fragment-performance/hooks.js 4 | examples/fragment-performance/test.js 5 | -------------------------------------------------------------------------------- /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "parser": "babel-eslint", 3 | "parserOptions": { 4 | "sourceType": "script" 5 | }, 6 | "env": { 7 | "browser": false, 8 | "es2020": true, 9 | "node": true 10 | }, 11 | "plugins": [ 12 | "prettier" 13 | ], 14 | "rules": { 15 | "curly": 2, 16 | "no-underscore-dangle": 0, 17 | "no-constant-condition": 2, 18 | "no-dupe-args": 2, 19 | "no-debugger": 2, 20 | "no-duplicate-case": 2, 21 | "no-empty": 2, 22 | "no-empty-character-class": 2, 23 | "no-eval": 2, 24 | "no-unused-vars": 2, 25 | "no-lonely-if": 2, 26 | "quotes": [0, true, "single"], 27 | "strict": [2, "global"], 28 | "prettier/prettier": [2, { 29 | "singleQuote": true, 30 | "tabWidth": 4 31 | }] 32 | }, 33 | "globals": { 34 | "require": false, 35 | "module": false 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | .DS_Store 3 | npm-debug.log 4 | /coverage 5 | /.nyc_output 6 | src/pipe.min.js 7 | jsconfig.json 8 | /.idea 9 | package-lock.json 10 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | language: node_js 3 | cache: 4 | directories: 5 | - node_modules 6 | after_script: 7 | - npm run codecov 8 | stages: 9 | - lint 10 | - test 11 | jobs: 12 | fast_finish: true 13 | allow_failures: 14 | - script: npm run test:frontend 15 | include: 16 | - stage: lint 17 | node_js: 12 18 | script: npm run lint 19 | - stage: test 20 | node_js: 13 21 | script: 22 | - npm run test 23 | - stage: test 24 | node_js: 12 25 | script: 26 | - npm run test 27 | - stage: test 28 | node_js: 14 29 | script: 30 | - npm run coverage 31 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Tailor Changelog 2 | 3 | # 7.0.0 4 | * [#27](https://github.com/StyleT/tailorx/pull/27) CI for Node.js 14 5 | * [#26](https://github.com/StyleT/tailorx/pull/27) (**breaking change**) simplified error handling of `fetchContext(request)` function 6 | 7 | This ensures that it's impossible to accidentally render page with incorrect context. To preserve old behaviour - wrap your 8 | `fetchContext` function with custom error handler which returns `{}`. 9 | Done by replacing `context:error(request, error)` event with `error(request, error, response)`. 10 | 11 | ## 6.1.1 12 | * [#25](https://github.com/StyleT/tailorx/pull/25) Fixed support for custom attributes, see issue at upstream https://github.com/zalando/tailor/issues/287 13 | 14 | ## 6.1.0 15 | * Added `ignore-invalid-ssl` fragment atrribute [#24](https://github.com/StyleT/tailorx/pull/24) 16 | 17 | ## 6.0.0 18 | * Added `processFragmentResponse(response, context): response` option: 19 | * See PR [#23](https://github.com/StyleT/tailorx/pull/23) 20 | * (**breaking change**) Modified `requestFragment(filterHeaders, processFragmentResponse)(url, attributes, request)` option 21 | 22 | ## 5.7.0 23 | * Treat non 2xx http status codes from non-primary fragments as error by default 24 | [#22](https://github.com/StyleT/tailorx/pull/22) 25 | 26 | ## 5.6.0 27 | * `shouldSetPrimaryFragmentAssetsToPreload` option added [#20](https://github.com/StyleT/tailorx/pull/20) 28 | 29 | ## 5.5.0 30 | * Now it's possible to provide links for preload on per-request basis 31 | [#19](https://github.com/StyleT/tailorx/pull/19) 32 | 33 | ## 5.4.0 34 | * Add ignoring special content during parsing to speed up performance 35 | [#18](https://github.com/StyleT/tailorx/pull/18) 36 | 37 | ## 5.3.0 38 | * Memoization of the CPU expensive template parsing added [#17](https://github.com/StyleT/tailorx/pull/17) 39 | 40 | ## 5.2.0 41 | * keep-alive support 42 | 43 | ## 5.1.0 44 | * "forward-querystring" fragment attribute added [#16](https://github.com/StyleT/tailorx/pull/16) 45 | 46 | ## 5.0.0 47 | * See PR [#15](https://github.com/StyleT/tailorx/pull/15) 48 | * (**breaking change**) removal of the frontend logic & Pipe.js 49 | * "fragmentHooks" option added to TailorX 50 | * More advanced `Link` header parsing logic 51 | * (**breaking change**) `amdLoaderUrl()` option was replaced with `getAssetsToPreload()` 52 | 53 | ## 4.1.0 54 | * added injection of the "title" & "meta" tags onto page header [#11](https://github.com/StyleT/tailorx/pull/11) 55 | * better error handing capabilities [#12](https://github.com/StyleT/tailorx/pull/12) 56 | * SeoBotsGuardStream added [#13](https://github.com/StyleT/tailorx/pull/13) 57 | * "return-headers" fragment attribute added [#14](https://github.com/StyleT/tailorx/pull/14) 58 | 59 | ## 4.0.0 60 | * removal of the "fallback-src" attribute support for fragments 61 | 62 | ### 3.11.0 63 | * adding "data-fragment-id" attr to the CSS links for fragments with "id" attribute specified 64 | * support of "invalid" Link headers added 65 | 66 | ### 3.10.0 67 | * Re-branding to TailorX 68 | 69 | ### 3.9.2 70 | * add timeout field to fragment opentracing span([#270](https://github.com/zalando/tailor/pull/270)) 71 | 72 | ### 3.9.1 73 | * handle streams in object mode while buffering([#258](https://github.com/zalando/tailor/pull/258)) 74 | 75 | ### 3.9.0 76 | * (perf) consume streams in parallel and flush them in series([#256](https://github.com/zalando/tailor/pull/256)) 77 | 78 | ### 3.8.0 79 | * change in parent span operation name([#238](https://github.com/zalando/tailor/pull/238)) 80 | * support multiple subscribers on tailor onDone hook([#243](https://github.com/zalando/tailor/pull/243)) 81 | * (perf) Minor optimization on attributes extraction([#247](https://github.com/zalando/tailor/pull/247)) 82 | * (perf) Improve link header extraction logic for fragments([#248](https://github.com/zalando/tailor/pull/248)) 83 | 84 | ### 3.7.1 85 | * handle parsing & primary error properly([#235](https://github.com/zalando/tailor/pull/235)) 86 | * Opentracing - Pass correct span contexts and add tests([#236](https://github.com/zalando/tailor/pull/236)) 87 | 88 | ### 3.7.0 89 | * Added Opentracing Instrumentation([#232](https://github.com/zalando/tailor/pull/232)) 90 | * Added Typescript definitions([#226](https://github.com/zalando/tailor/pull/226)) 91 | * Support asset preloading for aws custom headers([#229](https://github.com/zalando/tailor/pull/229)) 92 | 93 | ### 3.6.0 94 | * Pass all custom fragment attributes to filterRequestHeaders([#209](https://github.com/zalando/tailor/pull/209)) 95 | * Custom API for adding TTFMP from fragments([#214](https://github.com/zalando/tailor/pull/214)) 96 | 97 | ### 3.5.1 98 | 99 | * (fix) - Pipe the AMD loader script from extended options ([#205](https://github.com/zalando/tailor/pull/205)) 100 | 101 | ### 3.5.0 102 | * Parse comment tags without error in child templates ([#195](https://github.com/zalando/tailor/pull/195)) 103 | * Preload the module loader script with HTTP link headers ([#203](https://github.com/zalando/tailor/pull/203)) 104 | 105 | ### 3.4.0 106 | * Fix for handling comment nodes in child tempaltes ([#191](https://github.com/zalando/tailor/pull/191)) 107 | * Two headers (`x-request-uri` & `x-request-host`) are added to the whitelist along with documentation on how to use them ([#192](https://github.com/zalando/tailor/pull/192)) 108 | 109 | ### 3.3.0 110 | * Add API support for custom performance entries([#187](https://github.com/zalando/tailor/pull/187)) 111 | 112 | ### 3.2.1 113 | * End asyncStream later in the process (before piping) ([#185](https://github.com/zalando/tailor/pull/185)) 114 | 115 | ### 3.2.0 116 | * Extract tag handling logic from request handler([#173](https://github.com/zalando/tailor/pull/173)) 117 | * Prettier Integration([#181](https://github.com/zalando/tailor/pull/181)) 118 | * Proper error propagation on template error([#179](https://github.com/zalando/tailor/pull/179)) 119 | * Code coverage improvements([#182](https://github.com/zalando/tailor/pull/182), [#183](https://github.com/zalando/tailor/pull/183)) 120 | 121 | ### 3.1.1 122 | 123 | * Allow file to be used a template instead of directory([#171](https://github.com/zalando/tailor/pull/171)) 124 | * Use `promisify` module to simpify the code([#174](https://github.com/zalando/tailor/pull/174)) 125 | 126 | ### 3.0.1 127 | * Custom performance hooks should be called for all fragments([#168](https://github.com/zalando/tailor/pull/168)) 128 | 129 | ### 3.0.0 130 | * Support for multiple link headers from fragments ([#140](https://github.com/zalando/tailor/pull/140)) 131 | * Update Buffer to Node 8 Syntax ([#154](https://github.com/zalando/tailor/pull/154)) 132 | * Update fragment performance hooks to support multiple link headers ([#159](https://github.com/zalando/tailor/pull/159)) 133 | * Support to forward headers from primary fragment via filterResponseHeaders ([#148](https://github.com/zalando/tailor/pull/148)) 134 | 135 | ##### Contributors 136 | - Aditya Pratap Singh ([addityasingh](https://github.com/addityasingh)) 137 | - Ramiro Rikkert ([rikkert](https://github.com/rikkert)) 138 | - Iilei ([iilei](https://github.com/iilei)) 139 | 140 | ### 2.3.0 141 | * write response headers once before flushing([#145](https://github.com/zalando/tailor/pull/145)) 142 | 143 | ### 2.2.0 144 | * Fix issue with preloading primary fragment assets([#141](https://github.com/zalando/tailor/pull/141)) 145 | 146 | ### 2.1.1 147 | * Opt out of server push for preloaded JS and CSS([#139](https://github.com/zalando/tailor/pull/139)) 148 | 149 | ### 2.1.0 150 | * Fix uglify-js options to preserve implicit return in IIFE ([#133](https://github.com/zalando/tailor/pull/133)) 151 | * Lock down the dependencies version to avoid issues with external libs ([#135](https://github.com/zalando/tailor/pull/135)) 152 | 153 | ### 2.0.2 154 | * Fix preloading headers for crossorigin scripts([#130](https://github.com/zalando/tailor/pull/130)) 155 | 156 | ### 2.0.1 157 | * [Perf] Preload the Primary fragment's assets ([#127](https://github.com/zalando/tailor/issues/127)) 158 | 159 | ### 2.0.0 160 | * Allow Lazy fragment initialization through promise ([#94](https://github.com/zalando/tailor/issues/94)) 161 | * Hooks for measuring performance of fragments initialization on frontend ([#95](https://github.com/zalando/tailor/issues/95)) 162 | * Migrate codebase to ES6 ([#109](https://github.com/zalando/tailor/issues/109)) 163 | * Html compatible for script tags ([#86](https://github.com/zalando/tailor/issues/86)) 164 | * Configurable options for filtering headers ([#91](https://github.com/zalando/tailor/issues/91)) 165 | 166 | ##### Breaking changes 167 | * Dropped node 4.x.x support 168 | * Modified logic for `pipeInstanceName` and `requestFragment`. Please check the [options](https://github.com/zalando/tailor#options) 169 | 170 | ##### Contributors 171 | - Aditya Pratap Singh ([addityasingh](https://github.com/addityasingh)) 172 | - Simeon Cheeseman ([SimeonC](https://github.com/SimeonC)) 173 | - Boopathi Rajaa ([boopathi](https://github.com/boopathi)) 174 | - Dan Peddle ([dazld](https://github.com/dazld)) 175 | - Vignesh Shanmugam ([vigneshshanmugam](https://github.com/vigneshshanmugam)) 176 | 177 | ### 1.1.0 178 | * Support fragment level compression (gzip/deflate) 179 | 180 | ### 1.0.7 181 | * Inline AMD loader if specified as file URL (Performance) 182 | 183 | ### 1.0.6 184 | * Asynchronous file read in built-in fetch 185 | * Respond 404 in case of not found template 186 | 187 | ### 1.0.5 188 | * Add support for fallback slots 189 | 190 | ### 1.0.4 191 | * Fragment initialization metrics 192 | 193 | ### 1.0.3 194 | * Update loadCSS to fix FF 38 crash on Async Fragments. 195 | 196 | ### 1.0.2 197 | * Fix issue related to unnamed slot behaviour 198 | 199 | ### 1.0.1 200 | * Introduced unnamed default slot 201 | 202 | ### 1.0.0 203 | * Introduced HTML compatible parser 204 | * Base templates using slots 205 | * Flattens nested templates 206 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing to TailorX 2 | 3 | **Thank you for your interest in making TailorX even better and more awesome. Your contributions are highly welcome.** 4 | 5 | There are multiple ways of getting involved: 6 | 7 | - [Report a bug](#report-a-bug) 8 | - [Suggest a feature](#suggest-a-feature) 9 | - [Contribute code](#contribute-code) 10 | 11 | Below are a few guidelines we would like you to follow. 12 | If you need help, please reach out to one or more of the maintainers. 13 | 14 | ## Report a bug 15 | Reporting bugs is one of the best ways to contribute. Before creating a bug report, please check that an issue reporting the same problem does not already exist. If there is an such an issue, you may add your information as a comment. 16 | 17 | To report a new bug you should open an issue that summarizes the bug and set the label to "bug". 18 | 19 | If you want to provide a fix along with your bug report: That is great! In this case please send us a pull request as described in section [Contribute Code] (#contribute-code). 20 | 21 | ## Suggest a feature 22 | To request a new feature you should open a GitHub issue and summarize the desired functionality and its use case. Set the issue label to "feature". 23 | 24 | ## Contribute code 25 | This is a rough outline of what the workflow for code contributions looks like: 26 | - Check the list of open issues at GitHub. Either assign an existing issue to yourself, or create a new one that you would like work on and discuss your ideas and use cases. 27 | - Fork the repository on GitHub 28 | - Create a topic branch from where you want to base your work. This is usually master. 29 | - Make commits of logical units. 30 | - Write good commit messages (see below). 31 | - Push your changes to a topic branch in your fork of the repository. 32 | - Submit a pull request 33 | - Your pull request must receive a :thumbsup: from two maintainers 34 | 35 | Thanks for your contributions! 36 | 37 | ### Commit messages 38 | Your commit messages ideally can answer two questions: what changed and why. The subject line should feature the “what” and the body of the commit should describe the “why”. 39 | 40 | When creating a pull request, its comment should reference the corresponding issue id. 41 | 42 | **Have fun and enjoy hacking!** 43 | 44 | ## New version release 45 | To release new version of package use the following steps: 46 | ``` 47 | $ npx -p conventional-changelog-angular -p conventional-changelog-preset-loader -p conventional-recommended-bump conventional-recommended-bump --preset angular 48 | $ npm version [major | minor | patch] 49 | # Review last commit 50 | $ git push && git push --tags 51 | $ npm publish 52 | ``` 53 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. -------------------------------------------------------------------------------- /NOTICE.md: -------------------------------------------------------------------------------- 1 | This product contains a modified version of Zalando's "node-tailor", 2 | which can be obtained at: 3 | * LICENSE: 4 | * [MIT](https://github.com/zalando/tailor/blob/master/LICENSE) 5 | * HOMEPAGE: 6 | * https://github.com/zalando/tailor 7 | 8 | ------------------------------------------------------------------------------- 9 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |  2 | 3 | --- 4 | 5 | [](https://npmjs.org/package/tailorx) 6 | [](https://travis-ci.com/StyleT/tailorx) 7 | [](https://codecov.io/github/StyleT/tailorx) 8 | [](http://opentracing.io) 9 | 10 | ## npm status 11 | 12 | [](https://npmjs.org/package/tailorx) 13 | [](https://npmjs.org/package/tailorx) 14 | 15 | TailorX is a layout service that uses streams to compose a web page from fragment services. 16 | O'Reilly describes it in the title of 17 | [this blog post](https://www.oreilly.com/ideas/better-streaming-layouts-for-frontend-microservices-with-tailor) 18 | as "a library that provides a middleware which you can integrate into any Node.js server." 19 | It's partially inspired by Facebook’s [BigPipe](https://www.facebook.com/notes/facebook-engineering/bigpipe-pipelining-web-pages-for-high-performance/389414033919/) 20 | and based on [Zalando Tailor](https://github.com/zalando/tailor). 21 | 22 | Some of TailorX's features and benefits: 23 | 24 | * **Composes pre-rendered markup on the backend**. This is important for SEO and fastens the initial render. 25 | * **Ensures a fast Time to First Byte**. TailorX requests fragments in parallel and streams them as soon as possible, without blocking the rest of the page. 26 | * **Enforces performance budget**. This is quite challenging otherwise, because there is no single point where you can control performance. 27 | * **Fault Tolerance**. Render the meaningful output, even if a page fragment has failed or timed out. 28 | 29 | TailorX is part of [Isomorphic Layout Composer Project](https://github.com/StyleT/icl), which aims to help developers create microservices for the frontend. If your front-end team is making the monolith-to-microservices transition, you might find TailorX and its available siblings beneficial. 30 | 31 | ## Why a Layout Service? 32 | 33 | 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. 34 | 35 | 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. 36 | 37 | ## Installation 38 | 39 | Begin using TailorX with: 40 | 41 | ```sh 42 | npm i tailorx 43 | ``` 44 | 45 | ```javascript 46 | const http = require('http'); 47 | const Tailor = require('tailorx'); 48 | const tailor = new Tailor({/* Options */}); 49 | const server = http.createServer(tailor.requestHandler); 50 | server.listen(process.env.PORT || 8080); 51 | ``` 52 | 53 | ## Options 54 | 55 | * `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({})` 56 | * `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. 57 | Default implementation [`lib/fetch-template.js`](./lib/fetch-template.js) fetches the template from the file system 58 | * `templatesPath` - To specify the path where the templates are stored locally, Defaults to `/templates/` 59 | * `fragmentTag` - Name of the fragment tag, defaults to `fragment` 60 | * `handledTags` - An array of custom tags, check [`tests/handle-tag`](./tests/handle-tag.js) for more info 61 | * `baseTemplatesCacheSize` - It is off by default. This cache can speed up parsing base templates. You need to specify it as a number of your base templates to cache the parsing of your templates but don't specify it less than the number of templates that your app has because when you are going to change your template several times without server rebooting than all these changed template's versions are going to be saved on your server which is causing cache issues. 62 | * `handleTag(request, tag, options, context)` - Receives a tag or closing tag and serializes it to a string or returns a stream 63 | * `filterRequestHeaders(attributes, request)` - Function that filters the request headers that are passed to fragment request, check default implementation in [`lib/filter-headers`](./lib/filter-headers.js) 64 | * `filterResponseHeaders(attributes, headers)` - Function that maps the given response headers from the primary & `return-headers` fragments to the final response 65 | * `maxAssetLinks` - Number of `Link` Header directives for CSS and JS respected per fragment - defaults to `1` 66 | * `requestFragment(filterHeaders, processFragmentResponse)(url, attributes, request)` - Function that returns a promise of request to a fragment server, check the default implementation in [`lib/request-fragment`](./lib/request-fragment.js) 67 | * `processFragmentResponse(response, context): response` - Function that processes response from the fragment. Returns response or throws an error. Check the default implementation in [`lib/process-fragment-response`](./lib/process-fragment-response.js) 68 | * `tracer` - Opentracing [compliant Tracer implementation](https://doc.esdoc.org/github.com/opentracing/opentracing-javascript/class/src/tracer.js~Tracer.html). 69 | * `botsGuardEnabled` - `false` by default. This option forces TailorX to respond with 500 error code even if non-primary fragment fails in case the request comes from SEO/SM bot. 70 | Bot detection is done via [device-detector-js](https://www.npmjs.com/package/device-detector-js). 71 | * `fragmentHooks` - Allows to override default behaviour of the `insertStart` & `insertEnd` hooks & wrap response from the fragment with custom code. 72 | * `insertStart(stream, attributes, headers, index)` 73 | * `insertEnd(stream, attributes, headers, index)` 74 | * `getAssetsToPreload()` - If specified, should return array of assets that should be added to the response `Link` header for preload. 75 | Return value format: `{styleRefs: ['https://ex.com/style.css'], scriptRefs: ['https://ex.com/script.js']}` 76 | * `shouldSetPrimaryFragmentAssetsToPreload` - `true` by default. This option allows or disallows TailorX to set a primary fragment's assets to the response `Link` header for preload. 77 | 78 | ## Template 79 | 80 | TailorX 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. 81 | 82 | ```html 83 | 84 |
85 | 86 | 87 | 88 |