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: README.md
+65-74Lines changed: 65 additions & 74 deletions
Original file line number
Diff line number
Diff line change
@@ -115,12 +115,13 @@ fastify.get('/path/without/cache/control', function (req, reply) {
115
115
116
116
#### `root` (required)
117
117
118
-
The absolute path of the directory that contains the files to serve.
119
-
The file to serve will be determined by combining `req.url` with the
120
-
provided root directory.
118
+
The absolute path of the directory containing the files to serve.
119
+
The file to serve is determined by combining `req.url` with the
120
+
root directory.
121
121
122
-
You can also provide an array of directories containing files to serve.
123
-
This is useful for serving multiple static directories under a single prefix. Files are served in a "first found, first served" manner, so the order in which you list the directories is important. For best performance, you should always list your main asset directory first. Duplicate paths will raise an error.
122
+
An array of directories can be provided to serve multiple static directories
123
+
under a single prefix. Files are served in a "first found, first served" manner,
124
+
so list directories in order of priority. Duplicate paths will raise an error.
124
125
125
126
#### `prefix`
126
127
@@ -132,7 +133,7 @@ A URL path prefix used to create a virtual mount path for the static directory.
132
133
133
134
Default: `{}`
134
135
135
-
Constraints that will be added to registered routes. See Fastify's documentation for
136
+
Constraints to add to registered routes. See Fastify's documentation for
-[`cacheControl`](https://www.npmjs.com/package/@fastify/send#cachecontrol)(Enable or disable setting Cache-Control response header, defaults to true. **Important:** If you want to provide a custom Cache-Control response header, this option must be false.)
176
+
-[`cacheControl`](https://www.npmjs.com/package/@fastify/send#cachecontrol)- Enable or disable setting Cache-Control response header (defaults to `true`). To provide a custom Cache-Control header, set this option to false
You're able to alter this options when calling `reply.download('filename.html', options)` or `reply.download('filename.html', 'otherfilename.html', options)` on each response to a request.
185
+
These options can be altered when calling `reply.sendFile('filename.html', options)` or `reply.sendFile('filename.html', 'otherfilename.html', options)` on each response.
185
186
186
187
#### `redirect`
187
188
188
189
Default: `false`
189
190
190
191
If set to `true`, `@fastify/static` redirects to the directory with a trailing slash.
191
192
192
-
This option cannot be set to `true` with `wildcard` set to `false` on a server
193
-
with `ignoreTrailingSlash` set to `true`.
193
+
This option cannot be `true` if `wildcard` is `false` and `ignoreTrailingSlash` is `true`.
194
194
195
-
If this option is set to `false`, then requesting directories without trailing
196
-
slash will trigger your app's 404 handler using `reply.callNotFound()`.
195
+
If `false`, requesting directories without a trailing slash triggers the app's 404 handler using `reply.callNotFound()`.
197
196
198
197
#### `wildcard`
199
198
200
199
Default: `true`
201
200
202
-
If set to `true`, `@fastify/static` adds a wildcard route to serve files.
203
-
If set to `false`, `@fastify/static` globs the filesystem for all defined
204
-
files in the served folder (`${root}/**/**`), and just creates the routes needed for
205
-
those and it will not serve the newly added file on the filesystem.
201
+
If `true`, `@fastify/static` adds a wildcard route to serve files.
202
+
If `false`, it globs the filesystem for all defined files in the
203
+
served folder (`${root}/**/**`) and creates the necessary routes,
204
+
but will not serve newly added files.
206
205
207
-
The default options of https://www.npmjs.com/package/glob are applied
208
-
for getting the file list.
206
+
The default options of [`glob`](https://www.npmjs.com/package/glob)
207
+
are applied for getting the file list.
209
208
210
-
This option cannot be set to `false` with `redirect` set to `true` on a server
211
-
with `ignoreTrailingSlash` set to `true`.
209
+
This option cannot be `false` if `redirect` is `true` and `ignoreTrailingSlash` is `true`.
212
210
213
211
#### `allowedPath`
214
212
215
213
Default: `(pathName, root, request) => true`
216
214
217
-
This function allows filtering the served files. Also, with the help of the request object a more complex path authentication is possible.
218
-
If the function returns `true`, the file will be served.
219
-
If the function returns `false`, Fastify's 404 handler will be called.
215
+
This function filters served files. Using the request object, complex path authentication is possible.
If `true` some extended information for folders will be accessible in `list.render` and in the json response.
363
+
If `true`, extended information for folders will be accessible in `list.render` and the JSON response.
368
364
369
365
```js
370
366
render(dirs, files) {
@@ -378,15 +374,15 @@ render(dirs, files) {
378
374
}
379
375
```
380
376
381
-
Warning: This will slightly decrease the performance, especially for deeply nested file structures.
377
+
> ⚠ Warning: This will slightly decrease the performance, especially for deeply nested file structures.
382
378
383
379
#### `list.jsonFormat`
384
380
385
381
Default: `names`
386
382
387
383
Options: `names`, `extended`
388
384
389
-
This option determines the output format when `json` is selected.
385
+
Determines the output format when `json` is selected.
390
386
391
387
`names`:
392
388
```json
@@ -410,13 +406,11 @@ This option determines the output format when `json` is selected.
410
406
"name": "dir1",
411
407
"stats": {
412
408
"dev": 2100,
413
-
"size": 4096,
414
-
...
409
+
"size": 4096
415
410
},
416
411
"extendedInfo": {
417
412
"fileCount": 4,
418
-
"totalSize": 51233,
419
-
...
413
+
"totalSize": 51233
420
414
}
421
415
}
422
416
],
@@ -425,8 +419,7 @@ This option determines the output format when `json` is selected.
425
419
"name": "file1.txt",
426
420
"stats": {
427
421
"dev": 2200,
428
-
"size": 554,
429
-
...
422
+
"size": 554
430
423
}
431
424
}
432
425
]
@@ -437,9 +430,9 @@ This option determines the output format when `json` is selected.
437
430
438
431
Default: `false`
439
432
440
-
Try to send the brotli encoded asset first (when supported within the `Accept-Encoding` headers), retry for gzip, then the fall back to the original `pathname`. You may choose to skip compression for smaller files that don't benefit from it.
433
+
First, try to send the brotli encoded asset (if supported by `Accept-Encoding` headers), then gzip, and finally the original `pathname`. Skip compression for smaller files that do not benefit from it.
441
434
442
-
Assume this structure with the compressed asset as a sibling of the un-compressed counterpart:
435
+
Assume this structure with the compressed asset as a sibling of the uncompressed counterpart:
443
436
444
437
```
445
438
./public
@@ -453,22 +446,21 @@ Assume this structure with the compressed asset as a sibling of the un-compresse
453
446
454
447
#### Disable serving
455
448
456
-
If you would just like to use the reply decorator and not serve whole directories automatically, you can simply pass the option `{ serve: false }`. This will prevent the plugin from serving everything under `root`.
449
+
To use only the reply decorator without serving directories, pass `{ serve: false }`.
450
+
This prevents the plugin from serving everything under `root`.
457
451
458
452
#### Disabling reply decorator
459
453
460
-
The reply object is decorated with a `sendFile` function by default. If you want to
461
-
disable this, pass the option `{ decorateReply: false }`. If @fastify/static is
462
-
registered to multiple prefixes in the same route only one can initialize reply
463
-
decorators.
454
+
The reply object is decorated with a `sendFile` function by default. To disable this,
455
+
pass `{ decorateReply: false }`. If `@fastify/static` is registered to multiple prefixes
456
+
in the same route, only one can initialize reply decorators.
464
457
465
458
#### Handling 404s
466
459
467
-
If a request matches the URL `prefix` but a file cannot be found for the
468
-
request, Fastify's 404 handler will be called. You can set a custom 404
469
-
handler with [`fastify.setNotFoundHandler()`](https://fastify.dev/docs/latest/Reference/Server/#setnotfoundhandler).
460
+
If a request matches the URL `prefix` but no file is found, Fastify's 404
461
+
handler is called. Set a custom 404 handler with [`fastify.setNotFoundHandler()`](https://fastify.dev/docs/latest/Reference/Server/#setnotfoundhandler).
470
462
471
-
When registering `@fastify/static` within an encapsulated context, the `wildcard` option may need to be set to `false`in order to support index resolution and nested not-found-handler:
463
+
When registering `@fastify/static` within an encapsulated context, the `wildcard` option may need to be set to `false` to support index resolution and nested not-found-handler:
472
464
473
465
```js
474
466
constapp=require('fastify')();
@@ -489,13 +481,12 @@ This code will send the `index.html` for the paths `docs`, `docs/`, and `docs/in
489
481
490
482
### Handling Errors
491
483
492
-
If an error occurs while trying to send a file, the error will be passed
493
-
to Fastify's error handler. You can set a custom error handler with
0 commit comments