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.
- From the Home page, select the appropriate service. You can use the search box to search by ID, name, or domain.
- Click the Edit configuration button and then select the option to clone the active version. 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, enter an appropriate name (e.g.,
Edge-Control Header parse_time_delta
). - 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
if (parse_time_delta(beresp.http.Edge-Control:cache-maxage) >= 0) { set beresp.ttl = parse_time_delta(beresp.http.Edge-Control:cache-maxage); }
- Click Create to create the snippet.
-
Click Create Snippet. The Create a VCL snippet page appears.
- In the Name field, enter an appropriate name (e.g.,
Edge-Control Header downstream-ttl handling
). - From the Type controls, select within subroutine.
- From the Select subroutine menu, select deliver (
vcl_deliver
). -
In the VCL field, add the following conditions:
1 2 3 4 5 6 7 8 9
if (fastly.ff.visits_this_service == 0) { if (resp.http.Edge-Control:downstream-ttl == "-1") { set resp.http.Cache-Control = "no-cache, no-store, must-revalidate, proxy-revalidate, private, max-age=0, s-maxage=0"; set resp.http.Expires = "1970-01-01 00:00:00"; } else if (parse_time_delta(resp.http.Edge-Control:downstream-ttl) >= 0) { set resp.http.Cache-Control:max-age = parse_time_delta(resp.http.Edge-Control:downstream-ttl); } unset resp.http.Edge-Control; }
- 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.