Skip to content

Instantly share code, notes, and snippets.

@m3mike
Forked from xquery/gist:6389383
Last active August 29, 2015 14:05
Show Gist options
  • Save m3mike/ed13ffe87acf0d96fafb to your computer and use it in GitHub Desktop.
Save m3mike/ed13ffe87acf0d96fafb to your computer and use it in GitHub Desktop.
(:
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
:)
(: commented out to make parser happy...
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