You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/techniques/compression.md
+30-4Lines changed: 30 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,8 +1,12 @@
1
1
### Compression
2
2
3
-
Compression can greatly decrease the size of the response body, thereby increasing the speed of a web app. Use the [compression](https://github.com/expressjs/compression) middleware package to enable gzip compression.
3
+
Compression can greatly decrease the size of the response body, thereby increasing the speed of a web app.
4
4
5
-
#### Installation
5
+
For **high-traffic** websites in production, it is strongly recommended to offload compression from the application server - typically in a reverse proxy (e.g., Nginx). In that case, you should not use compression middleware.
6
+
7
+
#### Use with Express (default)
8
+
9
+
Use the [compression](https://github.com/expressjs/compression) middleware package to enable gzip compression.
6
10
7
11
First install the required package:
8
12
@@ -18,6 +22,28 @@ import * as compression from 'compression';
18
22
app.use(compression());
19
23
```
20
24
21
-
> info **Hint** If using the `FastifyAdapter`, consider using [fastify-compress](https://github.com/fastify/fastify-compress) instead.
25
+
#### Use with Fastify
22
26
23
-
For **high-traffic** websites in production, it is strongly recommended to offload compression from the application server - typically in a reverse proxy (e.g., Nginx). In that case, you should not use compression middleware.
27
+
If using the `FastifyAdapter`, you'll want to use [fastify-compress](https://github.com/fastify/fastify-compress):
28
+
29
+
```bash
30
+
$ npm i --save fastify-compress
31
+
```
32
+
33
+
Once the installation is complete, apply the fastify-compress middleware as global middleware.
34
+
35
+
```typescript
36
+
import*ascompressionfrom'fastify-compress';
37
+
// somewhere in your initialization file
38
+
app.register(compression);
39
+
```
40
+
41
+
By default, fastify-compress will use Brotli compression (on Node >= 11.7.0) when browsers indicate support for the encoding. While Brotli is quite efficient in terms of compression ratio, it's also quite slow. Due to this, you may want to tell fastify-compress to only use deflate and gzip to compress responses; you'll end up with larger responses but they'll be delivered much more quickly.
42
+
43
+
To specify encodings, provide a second argument to `app.register`:
0 commit comments