1 |
|
2 | Output Filters
|
3 | ==============
|
4 |
|
5 | Output filters allow the rendered output to undergo some post processing before being sent to the client. One of our use cases has been to perform HTML optimizations on the content. This includes things like removing optional closing elements and normalizing boolean attributes.
|
6 |
|
7 | Filters are defined in ``filters/output`` with their corresponding tests living in the ``tests/server/filters/output`` folder.
|
8 |
|
9 | Defining an Output Filter
|
10 | -------------------------
|
11 |
|
12 | Output filters export a function that accepts up to four parameters. The parameters are a string containing the rendered content, the content type being returned, the request object and the shunter config. It should return the modified content, or undefined if it wants to pass the content through unmodified.
|
13 |
|
14 | In the following example we'll process responses with a ``text/plain`` content type and replace all instances of shunter with Shunter.
|
15 |
|
16 | ```js
|
17 | module.exports = function(content, contentType, request, config) {
|
18 | if (contentType === 'text/plain') {
|
19 | return content.replace(/shunter/ig, 'Shunter');
|
20 | }
|
21 | }
|
22 | ```
|
23 |
|
24 | ---
|
25 |
|
26 | Related:
|
27 |
|
28 | - [Full API Documentation](index.md)
|