refactor: load search index only when the search bar is focused
This commit is contained in:
parent
f42d71a948
commit
6474fbcac1
1 changed files with 38 additions and 13 deletions
|
@ -19,16 +19,26 @@
|
||||||
|
|
||||||
let fuse;
|
let fuse;
|
||||||
let posts = [];
|
let posts = [];
|
||||||
|
let searchInitialized = false;
|
||||||
|
|
||||||
async function initializeSearch() {
|
async function initializeSearch() {
|
||||||
const response = await fetch('/search-index.json');
|
// Prevent multiple initializations
|
||||||
posts = await response.json();
|
if (searchInitialized) return;
|
||||||
|
try {
|
||||||
fuse = new Fuse(posts, {
|
// Fetch the search index
|
||||||
keys: ['title', 'description', 'content'],
|
const response = await fetch('/search-index.json');
|
||||||
threshold: 0.3,
|
posts = await response.json();
|
||||||
includeMatches: true
|
|
||||||
});
|
fuse = new Fuse(posts, {
|
||||||
|
keys: ['title', 'description', 'content'],
|
||||||
|
threshold: 0.3,
|
||||||
|
includeMatches: true
|
||||||
|
});
|
||||||
|
searchInitialized = true;
|
||||||
|
document.getElementById('search-results').innerHTML = '';
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error fetching search index:', error);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function performSearch(query) {
|
function performSearch(query) {
|
||||||
|
@ -37,6 +47,8 @@
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!searchInitialized) return;
|
||||||
|
|
||||||
const results = fuse.search(query);
|
const results = fuse.search(query);
|
||||||
const resultsElement = document.getElementById('search-results');
|
const resultsElement = document.getElementById('search-results');
|
||||||
|
|
||||||
|
@ -58,14 +70,27 @@
|
||||||
|
|
||||||
resultsElement.innerHTML = html;
|
resultsElement.innerHTML = html;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initialize search when the component mounts
|
|
||||||
initializeSearch();
|
|
||||||
|
|
||||||
// Add event listener for search input
|
// Add event listener for search input
|
||||||
const searchInput = document.getElementById('search-input');
|
const searchInput = document.getElementById('search-input');
|
||||||
|
|
||||||
|
// Initialize search only when the input is focused or clicked
|
||||||
|
searchInput.addEventListener('focus', initializeSearch);
|
||||||
|
searchInput.addEventListener('click', initializeSearch);
|
||||||
|
|
||||||
searchInput.addEventListener('input', (e) => {
|
searchInput.addEventListener('input', (e) => {
|
||||||
performSearch(e.target.value);
|
if (!searchInitialized) {
|
||||||
|
initializeSearch().then(() => {
|
||||||
|
performSearch(e.target.value);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
performSearch(e.target.value);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
searchInput.addEventListener('focus', () => {
|
||||||
|
if (!searchInitialized) {
|
||||||
|
document.getElementById('search-results').innerHTML = '<p>Loading search index...</p>';
|
||||||
|
}
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue