Skip to content

Commit

Permalink
Preload searchindex.js in background to optimize the search experiences
Browse files Browse the repository at this point in the history
Signed-off-by: Tao He <linzhu.ht@alibaba-inc.com>
  • Loading branch information
sighingnow committed May 9, 2023
1 parent b258999 commit 17fd3f6
Showing 1 changed file with 31 additions and 6 deletions.
37 changes: 31 additions & 6 deletions docs/_templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,6 @@

#}

{% block body %}
<script>
document.body.dataset.theme = "light";
</script>
{% endblock %}

{%- block extrahead %}

{{ super() }}
Expand Down Expand Up @@ -54,4 +48,35 @@
src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml.js">
</script>

<!-- preload searchindex.js -->
<script src='' id="searchindexjs" type="text/javascript"></script>

{% endblock %}

{% block body %}
<script>
document.body.dataset.theme = "light";
</script>

<script>
function ready(callback) {
if (document.readyState!='loading') {
// in case the document is already rendered
callback();
} else if (document.addEventListener) {
// modern browsers
document.addEventListener('DOMContentLoaded', callback);
} else document.attachEvent('onreadystatechange', function() {
// IE <= 8
if (document.readyState=='complete') {
callback();
}
});
}

ready(function() {
let searchindexjs = document.getElementById("searchindexjs");
searchindexjs.src = "{{ pathto('searchindex.js', 1) }}";
});
</script>
{% endblock %}

0 comments on commit 17fd3f6

Please sign in to comment.