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.
API Platform leverages this capability by pushing relations of a resource to clients.
<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiProperty;
use ApiPlatform\Core\Annotation\ApiResource;
/**
* @ApiResource
*/
class Book
{
/**
* @var Author
* @ApiProperty(push=true)
*/
public $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.
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).
The Distribution: Create Powerful APIs with Ease
The API Component
- General Design Considerations
- GraphQL Support
- Enabling GraphQL
- GraphiQL
- GraphQL Playground
- Modifying or Disabling the Default IDE
- Request with application/graphql Content-Type
- Queries
- Mutations
- Workflow of the Resolvers
- Events
- Filters
- Pagination
- Security
- Serialization Groups
- Name Conversion
- Custom Types
- Modify the Extracted Types
- Changing the Serialization Context Dynamically
- Export the Schema in SDL
- Handling File Upload
- Filters
- Doctrine ORM and MongoDB ODM Filters
- Elasticsearch Filters
- Serializer Filters
- Creating Custom Filters
- ApiFilter Annotation
- The Serialization Process
- Overall Process
- Available Serializers
- The Serialization Context, Groups and Relations
- Using Serialization Groups
- Using Serialization Groups per Operation
- Changing the Serialization Context Dynamically
- Changing the Serialization Context on a Per-item Basis
- Name Conversion
- Decorating a Serializer and Adding Extra Data
- Entity Identifier Case
- Embedding the JSON-LD Context
- Collection Relation
- Overriding Default Order
- OpenAPI Specification Support (formerly Swagger)
- Pushing Related Resources Using HTTP/2
- Using External Vocabularies
- NelmioApiDocBundle Integration
- Configuration
The Schema Generator Component
- Configuration
- Customizing PHP Namespaces
- Forcing a Field Range
- Forcing a Field Cardinality
- Forcing a Relation Table Name
- Forcing (or Disabling) a Class Parent
- Forcing a Class to be Abstract
- Forcing a Nullable Property
- Forcing a Unique Property
- Making a Property Read-Only
- Making a Property Write-Only
- Forcing a Property to be in a Serialization Group
- Forcing an Embeddable Class to be Embedded
- Author PHPDoc
- Disabling Generators and Creating Custom Ones
- Skipping Accessor Method Generation
- Disabling the id Generator
- Generating UUIDs
- User submitted UUIDs
- Generating Custom IDs
- Disabling Usage of Doctrine Collections
- Changing the Field Visibility
- Generating @Assert\Type Annotations
- Forcing Doctrine Inheritance Mapping Annotation
- Interfaces and Doctrine Resolve Target Entity Listener
- Custom Schemas
- Checking GoodRelation Compatibility
- PHP File Header
- Full Configuration Reference