VCL provides the building blocks to access information inside the Edge-Control response header field from the origin. We support this by honoring cache-maxage
from Edge-Control as the time to live (TTL) of the object on the Fastly edge, and honoring downstream-ttl
from Edge-Control as the TTL to be sent down from the Fastly edge to the end user's browser.
In order to incorporate this Edge-Control header support, use VCL Snippets to update your vcl_fetch
:
- Log in to the Fastly web interface and click the Configure link.
- From the service menu, select the appropriate service.
- Click the Edit configuration button and then select Clone active. The Domains page appears.
- Click the VCL Snippets link. The VCL Snippets page appears.
-
Click Create Snippet. The Create a VCL snippet page appears.
- In the Name field, type an appropriate name (e.g.,
Edge-Control Header
). - From the Type controls, select within subroutine.
- From the Select subroutine menu, select fetch (vcl_fetch).
-
In the VCL field, add the following conditions:
1 2 3 4 5 6 7
if (parse_time_delta(subfield(beresp.http.Edge-Control, "downstream-ttl")) >= 0) { set beresp.http.Cache-Control = "max-age=" parse_time_delta(subfield(beresp.http.Edge-Control, "downstream-ttl")); } if (parse_time_delta(subfield(beresp.http.Edge-Control, "cache-maxage")) >= 0) { set beresp.ttl = parse_time_delta(subfield(beresp.http.Edge-Control, "cache-maxage")); }
- Click Create to create the snippet.
- Click the Activate button to deploy your configuration changes.
The subfield function parses the Edge-Control field for subfields, and the parse_time_delta
function converts time values like "7m" into a number of seconds. You can then use that number of seconds to populate beresp.ttl
(the TTL of the object on the Fastly edge) or you can use it to construct a Cache-Control header field for downstream. The parse_time_delta
function will return -1 if the subfield is not well-formed as a time value, or if it is entirely absent. The above snippet honors cache-maxage
and downstream-ttl
from Edge-Control if present and usable.