Skip to content

Working with Server Side Includes

Edit this page

In a similar way to ESI (Edge Side Includes), SSI can be used to control HTTP caching on fragments of a response. The most important difference is that SSI is known directly by most web servers like Apache, Nginx etc.

The SSI instructions are done via HTML comments:

1
2
3
4
5
6
7
8
9
10
11
<!DOCTYPE html>
<html>
    <body>
        <!-- ... some content -->

        <!-- Embed the content of another page here -->
        <!--#include virtual="/..." -->

        <!-- ... more content -->
    </body>
</html>

There are some other available directives but Symfony manages only the #include virtual one.

Danger

Be careful with SSI: your website may fall victim to injections. Please read this OWASP article first!

When the web server reads an SSI directive, it requests the given URI or serves it directly from its cache. It repeats this process until there are no more SSI directives to handle. Then, it merges all responses into one and sends it to the client.

Using SSI in Symfony

First, to use SSI, be sure to enable it in your application configuration:

1
2
3
# config/packages/framework.yaml
framework:
    ssi: { enabled: true }

Suppose you have a page with private content like a Profile page and you want to cache a static GDPR content block. With SSI, you can add some expiration on this block and keep the page private:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// src/Controller/ProfileController.php
namespace App\Controller;

use Symfony\Component\HttpKernel\Attribute\Cache;
// ...

class ProfileController extends AbstractController
{
    public function index(): Response
    {
        // by default, responses are private
        return $this->render('profile/index.html.twig');
    }

    #[Cache(smaxage: 600)]
    public function gdpr(): Response
    {
        return $this->render('profile/gdpr.html.twig');
    }
}

The profile index page does not have public caching, but the GDPR block has 10 minutes of expiration. Let's include this block into the main one:

1
2
3
4
5
6
7
{# templates/profile/index.html.twig #}

{# you can use a controller reference #}
{{ render_ssi(controller('App\\Controller\\ProfileController::gdpr')) }}

{# ... or a path (it's common to use relative paths instead of absolute URLs in the server's SSI configuration) #}
{{ render_ssi(path('profile_gdpr')) }}

The render_ssi Twig helper will generate something like:

1
<!--#include virtual="/_fragment?_hash=abcdef1234&_path=_controller=App\Controller\ProfileController::gdpr" -->

render_ssi ensures that the SSI directive is generated only if the request has the header requirement like Surrogate-Capability: device="SSI/1.0" (normally given by the web server). Otherwise, it embeds the sub-response directly.

Note

For more information about Symfony cache fragments, read the ESI documentation.

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
TOC
    Version