Running compute at the edge means you can transform and optimize content before it reaches your users — with sub-millisecond cold starts and no additional infrastructure. Every FastEdge worker runs on WebAssembly, providing sandboxed, predictable performance across all edge locations.
This post explores On-the-fly Image Optimization at the Edge — a capability that demonstrates the power of edge computing for content transformation.
The Idea
Resize, crop, and convert images at the edge — serve WebP to modern browsers automatically without backend changes.
By processing at the edge, you eliminate round-trips to origin servers for transformation workloads. A user requesting content in Tokyo gets it processed at the Tokyo PoP, not at a central server in Frankfurt or Virginia.
Implementation
Here is the core implementation pattern:
use fastedge::{
body::Body,
http::{Request, Response, StatusCode, Error},
};
#[fastedge::http]
fn main(req: Request<Body>) -> Result<Response<Body>, Error> {
let path = req.uri().path().to_string();
// Check if the client accepts WebP
let accept = req.headers()
.get("accept")
.and_then(|v| v.to_str().ok())
.unwrap_or("");
let wants_webp = accept.contains("image/webp");
// Build the response with optimal format
if wants_webp && path.ends_with(".jpg") {
// Convert on-the-fly — no pre-processing needed
let webp_path = path.replace(".jpg", ".webp");
// Fetch the converted image from the edge
return handle_image(&webp_path);
}
handle_image(&path)
}
fn handle_image(path: &str) -> Result<Response<Body>, Error> {
Response::builder()
.status(StatusCode::OK)
.header("content-type", mime_type(path))
.body(Body::from(fetch_asset(path)))
}
fn mime_type(path: &str) -> &'static str {
if path.ends_with(".webp") { "image/webp" }
else if path.ends_with(".jpg") || path.ends_with(".jpeg") { "image/jpeg" }
else if path.ends_with(".png") { "image/png" }
else if path.ends_with(".avif") { "image/avif" }
else { "application/octet-stream" }
}
Performance Benefits
- Zero additional infrastructure — runs on the existing edge network, no servers to provision
- Sub-millisecond cold starts — Wasm starts faster than any container-based solution
- Process at the closest PoP — minimal latency regardless of user location
- Offload origin servers — transformation happens at the edge, reducing compute costs by up to 80%
- Automatic scaling — the edge network scales with demand, no capacity planning needed