Use js.Build to fetch fuseOpts;

also load Search on window load
This commit is contained in:
Aditya Telange 2021-04-10 00:58:23 +05:30
parent b756217126
commit ba48765e2c
No known key found for this signature in database
GPG Key ID: 82E844EF3DA99E77
2 changed files with 8 additions and 6 deletions

View File

@ -1,11 +1,13 @@
import * as params from '@params';
var fuse; // holds our search engine
var resList = document.getElementById('searchResults');
var sInput = document.getElementById('searchInput');
var first, last = null
var resultsAvailable = false;
// load our search index, only executed onload
function loadSearch() {
// load our search index
window.onload = function () {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
@ -24,7 +26,7 @@ function loadSearch() {
'content'
]
};
{{ if . }}options = {{ jsonify . }}{{ end }} // load custom options from .Site.Params.fuseOpts
if (params.fuseOpts) options = params.fuseOpts;
fuse = new Fuse(data, options); // build the index from the json file
}
} else {

View File

@ -61,14 +61,14 @@
{{- /* Search */}}
{{- if (eq .Layout `search`) -}}
<link crossorigin="anonymous" rel="preload" as="fetch" href="../index.json">
{{- $fastsearch := resources.Get "js/fastsearch.js" | resources.ExecuteAsTemplate "js/fastsearch.js" .Site.Params.fuseOpts }}
{{- $fastsearch := resources.Get "js/fastsearch.js" | js.Build (dict "params" (dict "fuseOpts" .Site.Params.fuseOpts)) }}
{{- $fusejs := resources.Get "js/fuse.js" }}
{{- if not .Site.Params.assets.disableFingerprinting }}
{{- $search := (slice $fusejs $fastsearch ) | resources.Concat "assets/js/search.js" | minify | fingerprint }}
<script defer crossorigin="anonymous" src="{{ $search.RelPermalink }}" onload="loadSearch();" integrity="{{ $search.Data.Integrity }}"></script>
<script defer crossorigin="anonymous" src="{{ $search.RelPermalink }}" integrity="{{ $search.Data.Integrity }}"></script>
{{- else }}
{{- $search := (slice $fusejs $fastsearch ) | resources.Concat "assets/js/search.js" | minify }}
<script defer crossorigin="anonymous" src="{{ $search.RelPermalink }}" onload="loadSearch();"></script>
<script defer crossorigin="anonymous" src="{{ $search.RelPermalink }}"></script>
{{- end }}
{{- end -}}