Skip to content

Instantly share code, notes, and snippets.

@paxtonhare
Created April 5, 2012 18:32
Show Gist options
  • Save paxtonhare/2313109 to your computer and use it in GitHub Desktop.
Save paxtonhare/2313109 to your computer and use it in GitHub Desktop.
GZip compression in the browser using MarkLogic 5.0-3 and greater
if (fn:contains(xdmp:get-request-header("Accept-Encoding", ""), "gzip")) then
(: do your gzipping here :)
else
(: don't gzip :)
xdmp:add-response-header("Content-Encoding", "gzip")
let $content :=
<html>
<head>
<title>Hello gzip compression</title>
</head>
<body>
<h1>gzip compression, FTW!</h1>
</body>
</html>
return
(: make sure the browser can handle gzip compression :)
if (fn:contains(xdmp:get-request-header("Accept-Encoding", ""), "gzip")) then
(
xdmp:add-response-header("Content-Encoding", "gzip"),
(: make sure $content is valid. xdmp:zip expects a node() :)
if ( fn:exists($content) ) then
xdmp:gzip($content)
else
$content
)
else
$content
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment