Api Platform Conference

The conference dedicated to API Platform and its ecosystem

Sep 15, 16 2022 | Lille & online

Pushing Related Resources Using HTTP/2

HTTP/2 allows a server to pre-emptively send (or "push") responses (along with corresponding "promised" requests) to a client in association with a previous client-initiated request. This can be useful when the server knows the client will need to have those responses available in order to fully process the response to the original request.

RFC 7540

API Platform leverages this capability by pushing relations of a resource to clients.

Note: We strongly recommend using Vulcain instead of this feature. Vulcain is faster, cleaner, more flexible, and is supported out of the box in the API Platform distribution.

<?php
// api/src/Entity/Book.php
namespace App\Entity;

use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\ApiResource;

#[ApiResource]
class Book
{
     #[ApiProperty(push: true)]
    public Author $author;
    
    // ...
}

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. According to the Preload W3C Candidate Recommendation, web servers and proxy servers can read this header, fetch the related resource and send it to the client using Server Push.

With the Caddy web server (the server shipped as part of the distribution), you must add the push directive to your Caddyfile to be able to use this feature.

NGINX, Apache, Cloudflare, Fastly and Akamai honor this header.

Using this feature maximises HTTP cache hits for your API resources. For best performance, this feature should be used in conjunction with the built-in HTTP cache invalidation system (based on Varnish).

What' new?

API platform conference 2022

Sep 15,16 2022: new edition of our conference dedicated to API Platform and its ecosystem!