Skip to content

Commit 3acd652

Browse files
authored
Add docs for HTTP/2 Server Push (#714)
* Add docs for HTTP/2 Server Push * Minor tweak * Update push-relations.md * @bendavies's review
1 parent 9907ff7 commit 3acd652

File tree

3 files changed

+70
-4
lines changed

3 files changed

+70
-4
lines changed

core/performance.md

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,36 @@ fresh, because the cache is purged in real time.
2020
The support for most specific cases such as the invalidation of collections when a document is added or removed or for
2121
relationships and inverse relations is built-in.
2222

23-
We also included [Varnish](https://varnish-cache.org/) in the [Docker setup](../distribution/index.md#using-the-official-distribution-recommended) provided with the
24-
distribution of API Platform, so this feature works out of the box.
23+
Integration with Varnish and Doctrine ORM is shipped with the core library, and [Varnish](https://varnish-cache.org/) is included in the [Docker setup](../distribution/index.md#using-the-official-distribution-recommended) provided with the
24+
distribution of API Platform.
25+
If you use the distribution, this feature works out of the box.
2526

26-
Integration with Varnish and the Doctrine ORM is shipped with the core library. You can easily implement the support for
27-
any other proxy or persistence system.
27+
If you don't, add the following configuration to enable the cache invalidation system:
28+
29+
```yaml
30+
parameters:
31+
# Adds a fallback VARNISH_URL if the env var is not set.
32+
# This allows you to run cache:warmup even if your
33+
# environment variables are not available yet.
34+
# You should not need to change this value.
35+
env(VARNISH_URL): ''
36+
37+
api_platform:
38+
# ...
39+
http_cache:
40+
invalidation:
41+
enabled: true
42+
varnish_urls: ['%env(VARNISH_URL)%']
43+
# Adds sensitive default cache headers
44+
max_age: 0
45+
shared_max_age: 3600
46+
vary: ['Content-Type', 'Authorization']
47+
public: true
48+
```
49+
50+
Support for reverse proxies other than Varnish can easily be added by implementing the `ApiPlatform\Core\HttpCache\PurgerInterface`.
51+
52+
In addition to the cache invalidation mechanism, you may want to [use HTTP/2 Server Push to pre-emptively send relations to the client](push-relations.md).
2853

2954
### Extending Cache-Tags for invalidation
3055

core/push-relations.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Pushing Related Resources Using HTTP/2
2+
3+
> HTTP/2 allows a server to pre-emptively send (or "push") responses
4+
(along with corresponding "promised" requests) to a client in
5+
association with a previous client-initiated request. This can be
6+
useful when the server knows the client will need to have those
7+
responses available in order to fully process the response to the
8+
original request.
9+
- [RFC 7540](https://tools.ietf.org/html/rfc7540#section-8.2)
10+
11+
API Platform leverages this capability by pushing relations of a resource to clients.
12+
13+
```php
14+
<?php
15+
16+
namespace App\Entity;
17+
18+
use ApiPlatform\Core\Annotation\ApiProperty;
19+
use ApiPlatform\Core\Annotation\ApiResource;
20+
21+
/**
22+
* @ApiResource
23+
*/
24+
class Book
25+
{
26+
/**
27+
* @var Author
28+
* @ApiProperty(push=true)
29+
*/
30+
public $author;
31+
}
32+
```
33+
34+
By setting the `push` attribute to `true` on a property holding a relation, API Platform will automatically add a valid `Link` HTTP header with the `preload` relation.
35+
According to the [Preload W3C Candidate Recommendation](https://www.w3.org/TR/preload/), web servers and proxy servers can read this header, fetch the related resource and send it to the client using Server Push.
36+
[NGINX](https://www.nginx.com/blog/nginx-1-13-9-http2-server-push/), [Apache](https://httpd.apache.org/docs/current/howto/http2.html#push), [CloudFlare](https://www.cloudflare.com/website-optimization/http2/serverpush/), [Fastly](https://docs.fastly.com/guides/performance-tuning/http2-server-push) and [Akamai](https://blogs.akamai.com/2017/03/http2-server-push-the-what-how-and-why.html) honor this header.
37+
38+
Using this feature maximises HTTP cache hits for your API resources.
39+
For best performance, this feature should be used in conjunction with [the built-in HTTP cache invalidation system (based on Varnish)](performance.md#enabling-the-built-in-http-cache-invalidation-system).

core/serialization.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,8 @@ In order to optimize such embedded relations, the default Doctrine data provider
296296
marked as [`EAGER`](http://doctrine-orm.readthedocs.io/projects/doctrine-orm/en/latest/reference/annotations-reference.html#manytoone).
297297
This avoids the need for extra queries to be executed when serializing the related objects.
298298

299+
Instead of embedding relation in the main HTTP response, you may want [to "push" them to the client using HTTP/2 server push](push-relations.md).
300+
299301
### Denormalization
300302

301303
It is also possible to embed a relation in `PUT` and `POST` requests. To enable that feature, set the serialization groups

0 commit comments

Comments
 (0)