diff options
author | (quasar) nebula <qznebula@protonmail.com> | 2024-05-13 12:09:14 -0300 |
---|---|---|
committer | (quasar) nebula <qznebula@protonmail.com> | 2024-05-31 12:11:53 -0300 |
commit | 782a498333782d5230ad29390690a94ebc333b13 (patch) | |
tree | 66f633ff80488325c26f155f7cb56de78e9f67b2 /src/static/js/search-worker.js | |
parent | db2ebb010aada0739773769cbaf3c34a9763c1fd (diff) |
search, client: write indexes to individual files
Diffstat (limited to 'src/static/js/search-worker.js')
-rw-r--r-- | src/static/js/search-worker.js | 26 |
1 files changed, 19 insertions, 7 deletions
diff --git a/src/static/js/search-worker.js b/src/static/js/search-worker.js index a6ecd3ab..c3975380 100644 --- a/src/static/js/search-worker.js +++ b/src/static/js/search-worker.js @@ -6,7 +6,6 @@ import FlexSearch from '../lib/flexsearch/flexsearch.bundle.module.min.js'; let status = null; let indexes = null; -let searchData = null; onmessage = handleWindowMessage; postStatus('alive'); @@ -20,6 +19,10 @@ main().then( postStatus('setup-error'); }); +function rebase(path) { + return `/search-data/` + path; +} + async function main() { indexes = withEntries(searchSpec, entries => entries @@ -28,16 +31,25 @@ async function main() { makeSearchIndex(descriptor, {FlexSearch}), ])); - searchData = - await fetch('/search-data/index.json') + const indexData = + await fetch(rebase('index.json')) .then(resp => resp.json()); + await Promise.all( + Object.entries(indexData) + .map(([key, _info]) => + fetch(rebase(key + '.json')) + .then(res => res.json()) + .then(data => { + importIndex(key, data); + }))); +} + +function importIndex(indexKey, indexData) { // If this fails, it's because an outdated index was cached. // TODO: If this fails, try again once with a cache busting url. - for (const [indexName, indexData] of Object.entries(searchData)) { - for (const [key, value] of Object.entries(indexData)) { - indexes[indexName].import(key, value); - } + for (const [key, value] of Object.entries(indexData)) { + indexes[indexKey].import(key, JSON.stringify(value)); } } |