Skip to content

Commit abe4b8f

Browse files
iBugminyoongi96
authored andcommitted
Lazy-load InstantSearch scripts and stylesheets (mmistakes#3691)
* Lazy-load InstantSearch scripts and stylesheets * Replace outdated script parts
1 parent ce9ae2e commit abe4b8f

File tree

1 file changed

+72
-52
lines changed

1 file changed

+72
-52
lines changed

_includes/search/algolia-search-scripts.html

+72-52
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,81 @@
1-
<!-- Including InstantSearch.js library and styling -->
2-
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/instantsearch.min.js"></script>
3-
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/instantsearch.min.css">
4-
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/instantsearch-theme-algolia.min.css">
5-
61
<script>
7-
// Instanciating InstantSearch.js with Algolia credentials
8-
const search = instantsearch({
9-
appId: '{{ site.algolia.application_id }}',
10-
apiKey: '{{ site.algolia.search_only_api_key }}',
11-
indexName: '{{ site.algolia.index_name }}',
12-
searchParameters: {
13-
restrictSearchableAttributes: [
14-
'title',
15-
'content'
16-
]
17-
}
18-
});
2+
// Including InstantSearch.js library and styling
3+
const loadSearch = function() {
4+
const loadCSS = function(src) {
5+
var link = document.createElement('link');
6+
link.rel = 'stylesheet';
7+
link.type = 'text/css';
8+
link.href = src;
9+
link.media = 'all';
10+
document.head.appendChild(link);
11+
};
12+
13+
var script = document.createElement('script');
14+
script.setAttribute("type", "text/javascript");
15+
script.setAttribute("src", "https://cdn.jsdelivr.net/npm/[email protected]/dist/instantsearch.min.js");
16+
script.addEventListener("load", function() {
17+
// Instantiating InstantSearch.js with Algolia credentials
18+
const search = instantsearch({
19+
appId: '{{ site.algolia.application_id }}',
20+
apiKey: '{{ site.algolia.search_only_api_key }}',
21+
indexName: '{{ site.algolia.index_name }}',
22+
searchParameters: {
23+
restrictSearchableAttributes: ['title', 'content']
24+
}
25+
});
26+
27+
const hitTemplate = function(hit) {
28+
const url = hit.url;
29+
const hightlight = hit._highlightResult;
30+
const title = hightlight.title && hightlight.title.value || "";
31+
const content = hightlight.html && hightlight.html.value || "";
32+
33+
return `
34+
<div class="list__item">
35+
<article class="archive__item" itemscope itemtype="https://schema.org/CreativeWork">
36+
<h2 class="archive__item-title" itemprop="headline"><a href="{{ site.baseurl }}${url}">${title}</a></h2>
37+
<div class="archive__item-excerpt" itemprop="description">${content}</div>
38+
</article>
39+
</div>
40+
`;
41+
}
1942

20-
const hitTemplate = function(hit) {
21-
const url = hit.url;
22-
const hightlight = hit._highlightResult;
23-
const title = hightlight.title && hightlight.title.value || "";
24-
const content = hightlight.html && hightlight.html.value || "";
25-
26-
return `
27-
<div class="list__item">
28-
<article class="archive__item" itemscope itemtype="https://schema.org/CreativeWork">
29-
<h2 class="archive__item-title" itemprop="headline"><a href="{{ site.baseurl }}${url}">${title}</a></h2>
30-
<div class="archive__item-excerpt" itemprop="description">${content}</div>
31-
</article>
32-
</div>
33-
`;
34-
}
35-
36-
// Adding searchbar and results widgets
37-
search.addWidget(
38-
instantsearch.widgets.searchBox({
39-
container: '.search-searchbar',
40-
{% unless site.algolia.powered_by == false %}poweredBy: true,{% endunless %}
41-
placeholder: '{{ site.data.ui-text[site.locale].search_placeholder_text | default: "Enter your search term..." }}'
42-
})
43-
);
44-
search.addWidget(
45-
instantsearch.widgets.hits({
46-
container: '.search-hits',
47-
templates: {
48-
item: hitTemplate,
49-
empty: '{{ site.data.ui-text[site.locale].search_algolia_no_results | default: "No results" }}',
43+
// Adding searchbar and results widgets
44+
search.addWidget(
45+
instantsearch.widgets.searchBox({
46+
container: '.search-searchbar',
47+
{% unless site.algolia.powered_by == false %}poweredBy: true,{% endunless %}
48+
placeholder: '{{ site.data.ui-text[site.locale].search_placeholder_text | default: "Enter your search term..." }}'
49+
})
50+
);
51+
search.addWidget(
52+
instantsearch.widgets.hits({
53+
container: '.search-hits',
54+
templates: {
55+
item: hitTemplate,
56+
empty: '{{ site.data.ui-text[site.locale].search_algolia_no_results | default: "No results" }}',
57+
}
58+
})
59+
);
60+
61+
if (!search.started) {
62+
search.start();
5063
}
51-
})
52-
);
64+
});
65+
document.body.appendChild(script);
66+
67+
loadCSS("https://cdn.jsdelivr.net/npm/[email protected]/dist/instantsearch.min.css");
68+
loadCSS("https://cdn.jsdelivr.net/npm/[email protected]/dist/instantsearch-theme-algolia.min.css");
69+
};
5370

5471
// Starting the search only when toggle is clicked
55-
$(document).ready(function () {
72+
$(document).ready(function() {
73+
var scriptLoaded = false;
74+
5675
$(".search__toggle").on("click", function() {
57-
if(!search.started) {
58-
search.start();
76+
if (!scriptLoaded) {
77+
loadSearch();
78+
scriptLoaded = true;
5979
}
6080
});
6181
});

0 commit comments

Comments
 (0)