Edgio

Compression

Compress content through:
  • Your origin server(s). This is known as origin server compression.
  • Our edge servers. This is known as edge server compression.
  • Our cloud.

Origin Server Compression

Origin server compression occurs when a web server associated with your origin configuration compresses the response it provides to Edgio. This type of compression requires both of the following conditions to be met:
  1. The client’s request must contain an Accept-Encoding request header set to one or more of the following values:
    br | gzip | deflate | bzip2
    Example: Accept-Encoding: br, gzip, deflate
  2. The web server(s) associated with your origin configuration must support the compression method defined within the Accept-Encoding request header.
The Accept-Encoding header allows the user agent (e.g., a web browser) to indicate which compression methods it supports to the origin server.

Edge Server Compression

Edge server compression occurs when an edge server compresses cached content and provides this compressed response to the client. It requires all of the following conditions to be met:
RequirementDescription
Accept-encoding request headerThe client’s request must contain an Accept-Encoding request header set to one or more of the following values:
br | gzip | deflate | bzip2

Example: Accept-Encoding: br, gzip
Content type enablementCompression must be enabled for the requested content type (aka MIME type or media type).
Cached contentAn uncompressed version of the requested content must already be cached on the POP closest to the client that requested it.
By default, Edgio caches the response as provided by an origin server or the Edgio cloud. Specifically, if the response is uncompressed and eligible to be cached, then Edgio will cache the uncompressed response.
Eligible file sizeThe file size of the requested content must fall within the following range:
  • Greater than approximately 128 bytes (Content-Length: 128)
  • Less than approximately 3 MB

Enabling Edge Server Compression

Edge server compression requires enabling compression for the desired content types (aka MIME type or media type). Common content types are provided below.
Content TypeFile Type
application/javascriptJavaScript
application/jsonJavaScript Object Notation (JSON)
application/xmlExtensible Markup Language (XML)
text/cssCascading Style Sheets (CSS)
text/htmlHTML files
text/javascriptJavaScript
text/plainText files
text/xmlExtensible Markup Language (XML)
Enable compression for each desired content type through the following steps:
  1. Create or modify a rule that identifies the set of requests on which compression will be enabled.
  2. Add the Compress Content Types feature (compress_content_types) to it. Set it to the desired set of content types.
Key information:
  • Once you have enabled edge server compression on the desired content types, Edgio will compress content when all of the required conditions have been met. If one or more conditions have not been met, then Edgio will serve either uncompressed cached content or the response provided by the origin server or the Edgio cloud.
  • Edge server compression is incompatible with Image Optimization. Requests that are processed by Image Optimization will not undergo edge server compression.
Sample configurations:
  • Rules:
    The following configuration enables edge server compression by setting the Compress Content Types feature to common content types:
    Compress Content Types Feature
  • CDN-as-Code:
    The following sample rule enables edge server compression across all requests for the common content types described above.
    JavaScript./routes.js
    1export default new Router().match(
    2 {},
    3 {
    4 response: {
    5 compress_content_types: [
    6 "application/javascript",
    7 "application/json",
    8 "application/xml",
    9 "text/css",
    10 "text/html",
    11 "text/javascript",
    12 "text/plain",
    13 "text/xml ",
    14 ],
    15 },
    16 }
    17);

Cache Implications

If your caching policy allows the requested content to be cached, then Edgio can cache each version of the requested content that it serves.
For example, if Edgio serves a Gzip, DEFLATE, and an uncompressed version of the requested content, then it can potentially cache 3 different versions of that content on our network.

How Does Compression Work?

The process through which requested content is compressed is outlined below.
  1. Does the request contain an Accept-Encoding header?
    • Yes: Does it contain a supported compression method?
      • Yes: Proceed to step 2.
      • No: Our edge servers will check whether an asset compressed using that method has been cached. If this check results in a cache hit, then our edge servers will serve it immediately. Otherwise, it is a cache miss and our edge servers will retrieve it from your origin configuration or the Edgio cloud. Skip to step 3.
    • No: Requests that are missing the Accept-Encoding header are served in an uncompressed format. This response is derived from either an origin server, the Edgio cloud, or cache.
  2. An edge server on the POP closest to the client will check to see if the requested content has been cached and if it still has a valid TTL.
    • Cache Miss: If a cached version of the requested content is not found, then the request will be forwarded to an origin server or the Edgio cloud. Proceed to step 3.
    • Cache Hit & Matching Compression Method: An edge server will immediately deliver the compressed content to the client.
    • Cache Hit & Different Compression Method: If the client requests a supported compression method that is different from the cached content’s compression method and the request is eligible for edge server compression, then an edge server will transcode the asset to the requested compression method and deliver it.
    • Uncompressed Cache Hit: If the initial request caused the asset to be cached in an uncompressed format, then a check will be performed to see whether the request is eligible for edge server compression.
      • Eligible: An edge server will serve the uncompressed content to the client. After which, if the request is eligible for caching, an edge server may compress the requested content and then cache the compressed version. Your caching policy dictates whether the compressed asset is eligible to be cached.
      • Ineligible: An edge server will immediately deliver the uncompressed content to the client.
  3. The request will be forwarded to either an origin server or the Edgio cloud. Either entity will provide a compressed or uncompressed response according to whether it can apply compression. Edgio will serve the response to the client. Your caching policy dictates whether the response will be cached.

Applying Brotli Compression through the Edgio Cloud

The Edgio cloud supports Brotli encoding if the web browser accepts Brotli and the response is considered eligible for compression. Apply Brotli compression under these conditions through brotliCompressSync.
JavaScript
1import { brotliCompressSync } from 'zlib'
2
3const BROTLI_ENCODING_REGEX = /\bbr\b/
4
5// This function will respond with Brotli encoded body if the user agent
6// accepts Brotli.
7// Prior to invoking this function, evaluate all the custom criteria that you want to apply
8// (e.g. content type, caching, size of the response, etc). If all those are satisfied then
9// invoke this function which will then check if the downstream is Brotli compatible and
10// if so compress the body and respond with it, returning true, otherwise returning false.
11// You can of course optimize this to first check the downstream compatibility
12// before even considering other criteria.
13const sendBrotliEncoded = (req, res, body) => {
14 const acceptEncoding = req.getHeader('Accept-Encoding')
15 const acceptBrotliEncoding = BROTLI_ENCODING_REGEX.test(acceptEncoding)
16 if (!acceptBrotliEncoding) {
17 return false
18 }
19
20 const encodedBody = brotliCompressSync(Buffer.from(body))
21 res.setHeader('content-length', Buffer.byteLength(encodedBody))
22 res.setHeader('content-encoding', 'br')
23 res.send(encodedBody)
24 return true
25}
You would need to invoke the above just prior to sending back the response, similar to this:
JavaScript
1const useBrotliEncoding = /* Evaluate all the custom criteria that you would like to apply */;
2 if (!useBrotliEncoding || !sendBrotliEncoded(req, res, body)) {
3 res.send(body)
4 }

Compressible Types

The Edgio cloud considers a response eligible for compression when either of the following conditions are satisfied:
  • The Content-Type header contains any of the following values:
    • text/html
    • application/x-javascript
    • text/css
    • application/javascript
    • text/javascript
    • application/json
    • application/vnd.ms-fontobject
    • application/x-font-opentype
    • application/x-font-truetype
    • application/x-font-ttf
    • application/xml
    • font/eot
    • font/opentype
    • font/otf
    • image/svg+xml
    • image/vnd.microsoft.icon
    • text/plain
    • text/xml
  • The URL ends with one of these file extensions:
    • .css
    • .js
    • .html
    • .eot
    • .ico
    • .otf
    • .ttf
    • .json
    • .svg