Skip to content

Commit bca6430

Browse files
committed
docs(book): 📝 added docs on header modification
1 parent 018945f commit bca6430

File tree

4 files changed

+24
-0
lines changed

4 files changed

+24
-0
lines changed

docs/0.2.x/src/SUMMARY.md

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
- [Debugging](./debugging.md)
1313
- [Templates and Routing](./templates/intro.md)
1414
- [Modifying the `<head>`](./templates/metadata-modification.md)
15+
- [Modifying HTTP Headers](./templates/setting-headers.md)
1516
- [Error Pages](./error-pages.md)
1617
- [Static Content](./static-content.md)
1718
- [Internationalization](./i18n/intro.md)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Modifying HTTP Headers
2+
3+
Most of the time, you shouldn't need to touch the HTTP headers of your Perseus templates, but sometimes you will need to. A particular example of this is if you want your users' browsers to only cache a page for a certain amount of time (the default for Perseus if five minutes), then you'd need to set the [`Cache-Control`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control) header.
4+
5+
Perseus supports inserting arbitrary HTTP headers for any response from the server that successfully returns a page generated from the template those headers are defined for. You can do this like so (taken from [here](https://github.com/arctic-hen7/perseus/blob/main/examples/basic/src/templates/index.rs)):
6+
7+
```rust,no_run,no_playground
8+
{{#include ../../../../examples/basic/src/templates/index.rs}}
9+
```
10+
11+
Of note here is the `set_headers_fn` function, which returns a `HeaderMap`. This is then used on the template with `.set_headers_fn()`. Note that the function you provide will be given the state as an argument (ignored here), and you must return some headers (you can't return an error).

docs/next/src/SUMMARY.md

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
- [Debugging](./debugging.md)
1313
- [Templates and Routing](./templates/intro.md)
1414
- [Modifying the `<head>`](./templates/metadata-modification.md)
15+
- [Modifying HTTP Headers](./templates/setting-headers.md)
1516
- [Error Pages](./error-pages.md)
1617
- [Static Content](./static-content.md)
1718
- [Internationalization](./i18n/intro.md)
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Modifying HTTP Headers
2+
3+
Most of the time, you shouldn't need to touch the HTTP headers of your Perseus templates, but sometimes you will need to. A particular example of this is if you want your users' browsers to only cache a page for a certain amount of time (the default for Perseus if five minutes), then you'd need to set the [`Cache-Control`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control) header.
4+
5+
Perseus supports inserting arbitrary HTTP headers for any response from the server that successfully returns a page generated from the template those headers are defined for. You can do this like so (taken from [here](https://github.com/arctic-hen7/perseus/blob/main/examples/basic/src/templates/index.rs)):
6+
7+
```rust,no_run,no_playground
8+
{{#include ../../../../examples/basic/src/templates/index.rs}}
9+
```
10+
11+
Of note here is the `set_headers_fn` function, which returns a `HeaderMap`. This is then used on the template with `.set_headers_fn()`. Note that the function you provide will be given the state as an argument (ignored here), and you must return some headers (you can't return an error).

0 commit comments

Comments
 (0)