Allow conditional GET requests to origin with 304 response, even if 'outer' request from browser is not conditional.
Code examples for publishing
Our code examples library is an always-evolving set of samples to do just about anything on the Fastly edge cloud. Browse or search for a keyword to find the perfect, ready-made solution you can paste into your service or adapt and customize for your own needs. Using Fastly Fiddle, play with any example and take advantage of the Fastly edge cloud, all without registering or affecting any existing account.
Add GeoIP data about the client browser as extra headers in any requests from Fastly to your origin.
Park request, make a different request first, use the response to annotate the real origin request (or make decisions about how to route it).
Use a custom Paywall header to trigger preflight requests to authenticate every article view with a backend paywall service.
Prioritize human traffic over search crawlers by serving stale content to crawlers.
Set TTLs at the edge based on the type of resource. Better done at origin, but this can be a great 'quick fix' or a solution if you don't control the origin.
Stream responses to the browser while still receiving data from the origin and also saving it to cache. Great for spreading out server-sent-events streams to millions of users from a single source stream.
Send request to different origin servers based on the URL path.
If origin responds with 500 internal server error, modify status to 503 and serve a 'safe' error message.
Browsers send OPTIONS requests before performing cross-origin POSTs. You can answer these requests directly from the edge.
Store username/password list in an edge dictionary, authorize user at the edge, reject requests that don't have correct credentials.
Include full text of robots.txt in VCL, serve as a synthetic response to avoid robots.txt requests hitting your origin.
Deal with all potential scenarios for using stale content to satisfy requests when origin is unhealthy or misbehaving.
Load balance requests randomly across multiple backends, dropping them automatically if they become unhealthy.
Map requests to backends consistently, which can be useful to improve your internal cache and replication efficiency.
Use Fastly Image Optimizer to transform and serve images at the edge, closer to your users.
Go from an F to an A grade on securityheaders.io by adding security policy headers to your responses at the edge.
Add, remove, and sort querystring parameters.
Compress HTML, SVG, and other compressible formats at the edge and store and serve both compressed and uncompressed versions
Read individual cookies, set new cookies in response.
Use a public GCS bucket as a backend for your Fastly service.
Block a list of IP addresses from accessing your service and include an expiry time.
Intercept suspicious traffic and display a CAPTCHA challenge. If the user passes, allow the request to go to the origin server.
Improve cache performance by normalizing requests. Filter and reorder query params, convert to lowercase, filter headers, and more.
Build raw JSON strings matching your BigQuery table schema to send log data to BigQuery.
Useful for comparing capabilities with required permissions.
Detect and reject requests from third party websites that attempt to embed your images on their pages.
Use Fastly's support for ESI to combine multiple origin-hosted objects into a single response at the edge.
Remove headers added by backends that you don't want to emit to the browser, like amz- or goog- headers.
Protect clients from redirects by chasing them internally at the edge, and then return the eventual non-redirect response.
If a backend returns a 429, cache it for the requesting IP but continue to allow other clients to use origin.
Return different objects based on the presence of a cookie.
Use the new Sec-Fetch-Dest header or URL patterns to identify assets that should not allow querystrings to be part of the cache key.
Group countries to cache content by custom regions or reject requests from some regions entirely.
The web application firewall runs only on traffic to your origin, but you can further refine when it should be invoked.
Use custom, predefined classnames like large, medium, small, teaser, thumb, or article to control Fastly Image Optimizer and optionally prevent end-user access to native properties like 'width'.
Shielding directs requests through two Fastly POPs instead of one, improving cache hit ratio and reducing traffic to your origin.
Combine multiple source images into a single image and then optimize and serve the result.
Serve different responses to separate user cohorts.
Use a dictionary of URL mappings to serve your redirects at lightning speed.
Send required resources along with page responses, before the resources are requested by the browser.
Match URL prefixes and make use of configurable response status and querystring preservation.
All code on this page is provided under both the BSD and MIT open source licenses.