Skip to content

Commit b178e98

Browse files
committed
add cache control header
1 parent 2712734 commit b178e98

File tree

3 files changed

+107
-9
lines changed

3 files changed

+107
-9
lines changed

middlewares/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ Fano Framework provides several built-in middlewares.
289289
- `TCsrfMiddleware`, middleware class which adds CSRF protection. Read [Cross-Site Request Forgery (CSRF)](/security/csrf-protection) for more information.
290290
- `TValidationMiddleware`, middleware class which validate request.
291291
- `TJsonContentTypeMiddleware`, middleware class which handle request with `application/json` in its header. For more information, read [Handling request with JSON body](/working-with-request#handling-request-with-json-body).
292-
- `TCacheControlMiddleware`, middleware class which adds `Cache-Control` response header. For more information, read [Http cache header](/working-with-response#http-cache-header).
292+
- `TCacheControlMiddleware`, middleware class which adds `Cache-Control` response header. For more information, read [Http cache header](/working-with-response/http-cache-header).
293293

294294
### Group several middlewares as one
295295

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
---
2+
title: Working with HTTP Cache Response Header
3+
description: Tutorial on how to work with HTTP Cache Response header in Fano Framework
4+
---
5+
6+
<h1 class="major">HTTP Cache Header</h1>
7+
8+
## Adding HttP Cache Header
9+
10+
To add http cache header ([RFC 7324](https://tools.ietf.org/html/rfc7234)), Fano Framework provides built-in [middleware](/middlewares)
11+
`TCacheControlMiddleware`.
12+
13+
To add HTTP cache header, create middleware or register its factory class `TCacheControlMiddlewareFactory` within container.
14+
15+
```
16+
container.add(
17+
'cacheControl',
18+
TCacheControlMiddlewareFactory.create().useETag()
19+
);
20+
```
21+
and then, attach middleware to [route](/working-with-router) that will need to be added with HTTP cache header.
22+
23+
```
24+
router.get(
25+
'/',
26+
container['homeController'] as IRequestHandler
27+
).add(container['cacheControl'] as IMiddleware);
28+
```
29+
## How it works
30+
When this middleware attached to route, additional steps is taken:
31+
32+
- If method is not cacheable (not GET nor HEAD) then nothing is done else,
33+
- `Cache-Control` header is added to response header,
34+
- If ETag is used then MD5 hash of response body is computed and `ETag` header is added to response with hash value of response body.
35+
- If request contains `If-None-Match` header, its value is compared with ETag value, if they are matched or `If-Non-Match` equals `"*"`, then response is considered not modified.
36+
- If response contains header `Last-Modified` and request contains header `If-Modified-Since`, both headers values are compared. If `Last-Modified` is older then response is considered not modified.
37+
- If response is not modified, instance `TNotModifiedResponse` is returned instead. Read [Not modified response](/working-with-response#not-modified-response) section for more information this class.
38+
39+
## Setting Cache-Control header value
40+
41+
### Set type
42+
`cacheType()` method set type of cache, accepts value of `ctPrivate` or `ctPublic` value. If it is not set `ctPrivate` is assumed.
43+
```
44+
container.add(
45+
'cacheControl',
46+
TCacheControlMiddlewareFactory.create().cacheType(ctPublic)
47+
);
48+
```
49+
50+
### Prevent cache to be stored
51+
`noStore()` method enable `no-store` value.
52+
```
53+
container.add(
54+
'cacheControl',
55+
TCacheControlMiddlewareFactory.create().noStore()
56+
);
57+
```
58+
### Set max-age value
59+
`maxAge()` method set `max-age` value.
60+
```
61+
container.add(
62+
'cacheControl',
63+
//set max-age=3600 (1 hour)
64+
TCacheControlMiddlewareFactory.create().maxAge(60*60)
65+
);
66+
```
67+
if max-age is set with 0 value, it implies `no-cache`.
68+
69+
### Enable ETag header
70+
`useETag()` method enable `ETag` header. When enable, `ETag` response header is added to response with value equals to MD5 hash of response body.
71+
72+
```
73+
container.add(
74+
'cacheControl',
75+
TCacheControlMiddlewareFactory.create().useETag()
76+
);
77+
```
78+
### Set must-revalidate
79+
`mustRevalidate` method enable `must-revalidate` value.
80+
81+
```
82+
container.add(
83+
'cacheControl',
84+
TCacheControlMiddlewareFactory.create().mustRevalidate()
85+
);
86+
```
87+
88+
All methods above can be chained,
89+
```
90+
container.add(
91+
'cacheControl',
92+
TCacheControlMiddlewareFactory.create()
93+
.cacheType(ctPublic)
94+
.mustRevalidate()
95+
.useETag()
96+
);
97+
```
98+
99+
## Explore more
100+
101+
- [Working with Request](/working-with-request)
102+
- [Working with Response](/working-with-response)
103+
- [Middlewares](/middlewares)

working-with-response/index.md

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,11 @@ end;
119119
By default, above code will redirect browser to [https://fanoframework.github.io](https://fanoframework.github.io) with HTTP 302 status. If you need to use different HTTP status code, set it in constructor's third parameter
120120

121121
```
122-
//redirect to fanoframework.github.io with HTTP 304
122+
//redirect to fanoframework.github.io with HTTP 301
123123
result := TRedirectResponse.create(
124124
response.headers(),
125125
'https://fanoframework.github.io',
126-
304
126+
301
127127
);
128128
```
129129

@@ -234,12 +234,7 @@ end;
234234
## <a name="not-modified-response"></a>Not modified response
235235

236236
`TNotModifiedResponse` is descendant of `THttpCodeResponse` class
237-
which specifically for handling HTTP 304 response and used when adding http cache header.
238-
239-
## <a name="http-cache-header"></a>Adding Http cache header
240-
241-
To add http cache header ([RFC 7324](https://tools.ietf.org/html/rfc7234)), Fano Framework provides built-in middleware
242-
`TCacheControlMiddleware`.
237+
which is specifically for handling HTTP 304 response and used when [adding http cache header](/working-with-response/http-cache-header).
243238

244239
## Explore more
245240

0 commit comments

Comments
 (0)