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
- Compress them using `gzip` and `zstd` (if beneficial)
42
+
- For only files in `assets/immutable`, add a `Cache-Control` header with `public, max-age=31536000, immutable` (since these are marked as cache-busted paths)
42
43
- Generate a `static_router()` function to serve these assets
43
44
44
45
#### Required parameter
@@ -49,10 +50,12 @@ This will:
49
50
50
51
-`compress = false` - compress static files with zstd and gzip, true or false (defaults to false)
51
52
52
-
-`ignore_dirs = [my_ignore_dir, other_ignore_dir]` - a bracketed list of `&str`s of the paths/subdirectories inside the target directory, which should be ignored and not included. (If this parameter is missing, no subdirectories will be ignored)
53
+
-`ignore_dirs = ["my_ignore_dir", "other_ignore_dir"]` - a bracketed list of `&str`s of the paths/subdirectories inside the target directory, which should be ignored and not included. (If this parameter is missing, no subdirectories will be ignored)
53
54
54
55
-`strip_html_ext = false` - strips the `.html` or `.htm` from all HTML files included. If the filename is `index.html` or `index.htm`, the `index` part will also be removed, leaving just the root (defaults to false)
55
56
57
+
-`cache_busted_paths = ["my_immutables_dir", "my_immutable_file"]` - a bracketed list of `&str`s of the subdirectories and/or single files which should gain the `Cache-Control` header with `public, max-age=31536000, immutable` for cache-busted paths. If this parameter is missing, the default is that no embedded files will have the `Cache-Control` header. Note: the files in `cache_busted_paths` need to already be compatible with cache-busting by having hashes in their file paths (for example). All `static-serve` does is set the appropriate header.
58
+
56
59
### Embedding a single static asset file
57
60
58
61
Use the `embed_asset!` macro to return a function you can use as a GET handler, which will include your static file, embedded into your binary:
@@ -61,7 +64,7 @@ Use the `embed_asset!` macro to return a function you can use as a GET handler,
- Include the file `my_file.png` from the `assets` directory
72
75
- Compress it using `gzip` and `zstd` (if beneficial)
76
+
- Add a `Cache-Control` header with the value `public, max-age=31536000, immutable` for cache-busted paths. Note: the file in `embed_asset!` needs to already be compatible with cache-busting by having a hash in its file path (for example). All `static-serve` does is set the appropriate header.
73
77
- Generate a `MethodRouter` "handler" you can add as a route on your router to serve the file
74
78
75
79
#### Required parameter
76
80
77
81
-`path_to_file` - a valid `&str` string literal of the path to the static file to be included
78
82
79
-
#### Optional parameter
83
+
#### Optional parameters
80
84
81
85
-`compress = false` - compress a static file with zstd and gzip, true or false (defaults to false)
86
+
-`cache_bust = false` - add a `Cache-Control` header with the value `public, max-age=31536000, immutable` for a cache-busted asset (defaults to false)
82
87
83
88
## Conditional Requests & Caching
84
89
@@ -87,6 +92,8 @@ The crate automatically handles:
87
92
-`Accept-Encoding` header to serve compressed versions if available
88
93
-`If-None-Match` header for ETag validation, returning `304 Not Modified` if unchanged
89
94
95
+
- With the optional cache-bust headers feature, each embedded file in the `cache_busted_paths` array (or single file in the case of `embed_asset!` with `cache_bust = true`) will be returned with a `Cache-Control` header with the value `public, max-age=31536000, immutable`. Note: the files involved need to already be compatible with cache-busting by having hashes in their file paths (for example). All `static-serve` does is set the appropriate header.
0 commit comments