Skip to content

Commit

Permalink
Run search on a web worker
Browse files Browse the repository at this point in the history
  • Loading branch information
neobrain committed Oct 5, 2024
1 parent c35d3fd commit 27a8b96
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 34 deletions.
53 changes: 19 additions & 34 deletions assets/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,9 @@

{{ $searchDataFile := printf "%s.search-data.json" .Language.Lang }}
{{ $searchData := resources.Get "search-data.json" | resources.ExecuteAsTemplate $searchDataFile . | resources.Minify | resources.Fingerprint }}
{{ $searchConfig := i18n "bookSearchConfig" | default "{}" }}

(function () {
const searchDataURL = '{{ $searchData.RelPermalink }}';
const indexConfig = Object.assign({{ $searchConfig }}, {
includeScore: true,
useExtendedSearch: true,
fieldNormWeight: 1.5,
threshold: 0.2,
ignoreLocation: true,
keys: [
{
name: 'title',
weight: 0.7
},
{
name: 'content',
weight: 0.3
}
]
});

const input = document.querySelector('#book-search-input');
const results = document.querySelector('#book-search-results');
Expand All @@ -31,6 +13,8 @@
return
}

const worker = new Worker('/searchWorker.js');

input.addEventListener('focus', init);
input.addEventListener('input', search);

Expand Down Expand Up @@ -72,9 +56,7 @@

fetch(searchDataURL)
.then(pages => pages.json())
.then(pages => {
window.bookSearchIndex = new Fuse(pages, indexConfig);
})
.then(pages => worker.postMessage(pages))
.then(() => input.required = false)
.then(search);
}
Expand All @@ -86,25 +68,28 @@
}

function runSearch() {
while (results.firstChild) {
results.removeChild(results.firstChild);
}

if (!input.value) {
return;
}

const searchHits = window.bookSearchIndex.search(input.value).slice(0,10);
searchHits.forEach(function (page) {
const li = element('<li><a href></a><small></small></li>');
const a = li.querySelector('a'), small = li.querySelector('small');
worker.onmessage = (ev) => {
while (results.firstChild) {
results.removeChild(results.firstChild);
}

const searchHits = ev.data;
searchHits.forEach(function (page) {
const li = element('<li><a href></a><small></small></li>');
const a = li.querySelector('a'), small = li.querySelector('small');

a.href = page.item.href;
a.textContent = page.item.title;
small.textContent = page.item.section;
a.href = page.item.href;
a.textContent = page.item.title;
small.textContent = page.item.section;

results.appendChild(li);
});
results.appendChild(li);
});
};
worker.postMessage(input.value);
}

/**
Expand Down
32 changes: 32 additions & 0 deletions assets/searchWorker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
importScripts("fuse.min.js")

{{ $searchConfig := i18n "bookSearchConfig" | default "{}" }}
const indexConfig = Object.assign({{ $searchConfig }}, {
includeScore: true,
useExtendedSearch: true,
fieldNormWeight: 1.5,
threshold: 0.2,
ignoreLocation: true,
keys: [
{
name: 'title',
weight: 0.7
},
{
name: 'content',
weight: 0.3
}
]
});

let index;
self.onmessage = (ev) => {
if (!index) {
let pages = ev.data;
index = new Fuse(pages, indexConfig);
return;
}

const searchHits = index.search(ev.data).slice(0,10);
self.postMessage(searchHits);
};
4 changes: 4 additions & 0 deletions layouts/partials/docs/inject/head.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{{- if default true .Site.Params.BookSearch -}}
{{- $searchWorkerJS := resources.Get "searchWorker.js" | resources.ExecuteAsTemplate "searchWorker.js" . }}
<link rel="preload" href="{{ $searchWorkerJS.RelPermalink }}" as="script" />
{{ end -}}

0 comments on commit 27a8b96

Please sign in to comment.