-
Notifications
You must be signed in to change notification settings - Fork 2
/
pre-compressed.ts
43 lines (36 loc) · 1.08 KB
/
pre-compressed.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/**
* This example shows how to use contentEncodingMappings option to serve pre-compressed files
*
* See ./pre-compressed-util.ts to have an example of file pre-compressing
*/
import { join } from 'node:path';
import { fastify } from 'fastify';
import { FileSystemStorage } from '../src/send-stream';
const app = fastify({ exposeHeadRoutes: true });
const storage = new FileSystemStorage(
join(__dirname, 'assets'),
{
contentEncodingMappings: [
{
// configure .html/.js/.css/.json files to have precompressed versions matching .br/.gz extensions
matcher: /^(?<path>.+\.(?:html|js|css|json))$/u,
encodings: [{ name: 'br', path: '$<path>.br' }, { name: 'gzip', path: '$<path>.gz' }],
},
],
},
);
app.get('*', async (request, reply) => {
const result = await storage.prepareResponse(request.url, request.raw);
if (result.statusCode === 404) {
reply.callNotFound();
return;
}
await result.send(reply.raw);
});
app.listen({ port: 3000 })
.then(() => {
console.info('listening on http://localhost:3000');
})
.catch((err: unknown) => {
console.error(err);
});