Skip to content

Instantly share code, notes, and snippets.

@xquery
Last active December 22, 2015 00:28
Show Gist options
  • Save xquery/6389383 to your computer and use it in GitHub Desktop.
Save xquery/6389383 to your computer and use it in GitHub Desktop.
serving up gzipped content from MarkLogic
In a lot of cases, gzipping content before you send it to an HTTP client can make a huge difference in download times.
compare downloaded file size of the following two approaches;
xquery version "1.0-ml";
let $page :=<html>
<body>
<h2>Without Gzip</h2>
{
for $a in (1 to 100)
return
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Donec hendrerit tempor tellus. Donec pretium posuere tellus. Proin quam nisl, tincidunt et, mattis eget, convallis nec, purus. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Nulla posuere. Donec vitae dolor. Nullam tristique diam non turpis. Cras placerat accumsan nulla. Nullam rutrum. Nam vestibulum accumsan nisl.</p>
}
</body>
</html>
return
(
xdmp:set-response-content-type("text/html"),
$page
)
to the gzipped enabled version
xquery version "1.0-ml";
let $page :=<html>
<body>
<h2>Gzip test</h2>
{
for $a in (1 to 100)
return
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Donec hendrerit tempor tellus. Donec pretium posuere tellus. Proin quam nisl, tincidunt et, mattis eget, convallis nec, purus. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Nulla posuere. Donec vitae dolor. Nullam tristique diam non turpis. Cras placerat accumsan nulla. Nullam rutrum. Nam vestibulum accumsan nisl.</p>
}
</body>
</html>
return
if( contains(xdmp:get-request-header("Accept-Encoding"), "gzip") )
then
(
xdmp:set-response-content-type("text/html"),
xdmp:add-response-header("Content-encoding", "gzip"),
xdmp:gzip($page)
)
else
(
xdmp:set-response-content-type("text/html"),
$page
)
its not a magic solution but its something that can almost immediately speed up most MarkLogic web app today.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment