diff --git a/package-lock.json b/package-lock.json index 49fc6592..b421dd81 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "coryd.dev", - "version": "1.5.1", + "version": "1.5.2", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "coryd.dev", - "version": "1.5.1", + "version": "1.5.2", "license": "MIT", "dependencies": { "@cdransf/api-text": "^1.5.0", diff --git a/package.json b/package.json index cac6267a..4a1f3d7d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "coryd.dev", - "version": "1.5.1", + "version": "1.5.2", "description": "The source for my personal site. Built using 11ty (and other tools).", "type": "module", "engines": { diff --git a/src/assets/scripts/index.js b/src/assets/scripts/index.js index f66daafc..2680a363 100644 --- a/src/assets/scripts/index.js +++ b/src/assets/scripts/index.js @@ -173,17 +173,25 @@ window.addEventListener('load', () => { const loadSearchIndex = async (query = '', types = []) => { const typeQuery = types.join(',') - const response = await fetch(`https://coryd.dev/api/search?q=${query}&type=${typeQuery}`) - const index = await response.json() - resultsById = index.reduce((byId, result) => { - byId[result.id] = result - return byId - }, {}) + try { + const response = await fetch(`https://coryd.dev/api/search?q=${query}&type=${typeQuery}`) + const index = await response.json() - miniSearch.removeAll() - miniSearch.addAll(index) - return resultsById + if (!Array.isArray(data)) return {} + + resultsById = data.reduce((acc, item) => { + acc[item.id] = item + return acc + }, {}) + + miniSearch.removeAll() + miniSearch.addAll(index) + return resultsById + } catch (error) { + console.error('Error fetching search data:', error) + return {} + } } loadSearchIndex().then(loadedResultsById => resultsById = loadedResultsById)