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
+64-73Lines changed: 64 additions & 73 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,28 +133,28 @@ 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.)
170
+
-[`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.
179
+
These options can be altered when calling `reply.sendFile('filename.html', options)` or `reply.sendFile('filename.html', 'otherfilename.html', options)` on each response.
179
180
180
181
#### `redirect`
181
182
182
183
Default: `false`
183
184
184
185
If set to `true`, `@fastify/static` redirects to the directory with a trailing slash.
185
186
186
-
This option cannot be set to `true` with `wildcard` set to `false` on a server
187
-
with `ignoreTrailingSlash` set to `true`.
187
+
This option cannot be `true` if `wildcard` is `false` and `ignoreTrailingSlash` is `true`.
188
188
189
-
If this option is set to `false`, then requesting directories without trailing
190
-
slash will trigger your app's 404 handler using `reply.callNotFound()`.
189
+
If `false`, requesting directories without a trailing slash triggers the app's 404 handler using `reply.callNotFound()`.
191
190
192
191
#### `wildcard`
193
192
194
193
Default: `true`
195
194
196
-
If set to `true`, `@fastify/static` adds a wildcard route to serve files.
197
-
If set to `false`, `@fastify/static` globs the filesystem for all defined
198
-
files in the served folder (`${root}/**/**`), and just creates the routes needed for
199
-
those and it will not serve the newly added file on the filesystem.
195
+
If `true`, `@fastify/static` adds a wildcard route to serve files.
196
+
If `false`, it globs the filesystem for all defined files in the
197
+
served folder (`${root}/**/**`) and creates the necessary routes,
198
+
but will not serve newly added files.
200
199
201
-
The default options of https://www.npmjs.com/package/glob are applied
202
-
for getting the file list.
200
+
The default options of [`glob`](https://www.npmjs.com/package/glob)
201
+
are applied for getting the file list.
203
202
204
-
This option cannot be set to `false` with `redirect` set to `true` on a server
205
-
with `ignoreTrailingSlash` set to `true`.
203
+
This option cannot be `false` if `redirect` is `true` and `ignoreTrailingSlash` is `true`.
206
204
207
205
#### `allowedPath`
208
206
209
207
Default: `(pathName, root, request) => true`
210
208
211
-
This function allows filtering the served files. Also, with the help of the request object a more complex path authentication is possible.
212
-
If the function returns `true`, the file will be served.
213
-
If the function returns `false`, Fastify's 404 handler will be called.
209
+
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.
357
+
If `true`, extended information for folders will be accessible in `list.render` and the JSON response.
362
358
363
359
```js
364
360
render(dirs, files) {
@@ -372,15 +368,15 @@ render(dirs, files) {
372
368
}
373
369
```
374
370
375
-
Warning: This will slightly decrease the performance, especially for deeply nested file structures.
371
+
> ⚠ Warning: This will slightly decrease the performance, especially for deeply nested file structures.
376
372
377
373
#### `list.jsonFormat`
378
374
379
375
Default: `names`
380
376
381
377
Options: `names`, `extended`
382
378
383
-
This option determines the output format when `json` is selected.
379
+
Determines the output format when `json` is selected.
384
380
385
381
`names`:
386
382
```json
@@ -404,13 +400,11 @@ This option determines the output format when `json` is selected.
404
400
"name": "dir1",
405
401
"stats": {
406
402
"dev": 2100,
407
-
"size": 4096,
408
-
...
403
+
"size": 4096
409
404
},
410
405
"extendedInfo": {
411
406
"fileCount": 4,
412
-
"totalSize": 51233,
413
-
...
407
+
"totalSize": 51233
414
408
}
415
409
}
416
410
],
@@ -419,8 +413,7 @@ This option determines the output format when `json` is selected.
419
413
"name": "file1.txt",
420
414
"stats": {
421
415
"dev": 2200,
422
-
"size": 554,
423
-
...
416
+
"size": 554
424
417
}
425
418
}
426
419
]
@@ -431,9 +424,9 @@ This option determines the output format when `json` is selected.
431
424
432
425
Default: `false`
433
426
434
-
Try to send the brotli encoded asset first (when supported within the `Accept-Encoding` headers), retry for gzip, and then fall back to the original `pathname`. You may choose to skip compression for smaller files that do not benefit from it.
427
+
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.
435
428
436
-
Assume this structure with the compressed asset as a sibling of the un-compressed counterpart:
429
+
Assume this structure with the compressed asset as a sibling of the uncompressed counterpart:
437
430
438
431
```
439
432
./public
@@ -447,20 +440,19 @@ Assume this structure with the compressed asset as a sibling of the un-compresse
447
440
448
441
#### Disable serving
449
442
450
-
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`.
443
+
To use only the reply decorator without serving directories, pass `{ serve: false }`.
444
+
This prevents the plugin from serving everything under `root`.
451
445
452
446
#### Disabling reply decorator
453
447
454
-
The reply object is decorated with a `sendFile` function by default. If you want to
455
-
disable this, pass the option `{ decorateReply: false }`. If @fastify/static is
456
-
registered to multiple prefixes in the same route only one can initialize reply
457
-
decorators.
448
+
The reply object is decorated with a `sendFile` function by default. To disable this,
449
+
pass `{ decorateReply: false }`. If `@fastify/static` is registered to multiple prefixes
450
+
in the same route, only one can initialize reply decorators.
458
451
459
452
#### Handling 404s
460
453
461
-
If a request matches the URL `prefix` but a file cannot be found for the
462
-
request, Fastify's 404 handler will be called. You can set a custom 404
463
-
handler with [`fastify.setNotFoundHandler()`](https://fastify.dev/docs/latest/Reference/Server/#setnotfoundhandler).
454
+
If a request matches the URL `prefix` but no file is found, Fastify's 404
455
+
handler is called. Set a custom 404 handler with [`fastify.setNotFoundHandler()`](https://fastify.dev/docs/latest/Reference/Server/#setnotfoundhandler).
464
456
465
457
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:
466
458
@@ -483,13 +475,12 @@ This code will send the `index.html` for the paths `docs`, `docs/`, and `docs/in
483
475
484
476
### Handling Errors
485
477
486
-
If an error occurs while trying to send a file, the error will be passed
487
-
to Fastify's error handler. You can set a custom error handler with
0 commit comments