Skip to content

Instantly share code, notes, and snippets.

@simonwistow
Last active August 29, 2015 14:04
Show Gist options
  • Save simonwistow/7f2716f2230a52548ede to your computer and use it in GitHub Desktop.
Save simonwistow/7f2716f2230a52548ede to your computer and use it in GitHub Desktop.
Caching Like Buttons
#
# All of this can be done easily via the Fastly API but we present it here in VCL for brevity
# Simply create a new "Content" object and attach a request condition with the condition
# req.url ~ "^/like" && req.request == "POST"
# (or whatever your "like" API endpoint is).
#
# Then create a new Logging object and attach the same condition to it.
#
sub vcl_recv {
if (req.url ~ "^/like" && req.request == "POST") {
error 702 "OK";
}
}
sub vcl_error {
if (obj.status == 702) {
set obj.status = 200;
set obj.http.Content-Type = "text/plain; charset=utf-8";
synthetic {"OK"};
}
return (deliver);
}
sub vcl_log {
# The log line can be whatever you'd like it to be - here I just use the standard log line
if (req.url ~ "^/like" && req.method == "POST") {
log {"syslog " service.id " my_like_endpoint :: "} req.http.Fastly-Client-IP {" "} {""-""} {" "} {""-""} {" "} now {" "} req.request " " req.url {" "} resp.status;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment