diff --git a/js/search.js b/js/search.js index 807360ca99..4bb837410f 100644 --- a/js/search.js +++ b/js/search.js @@ -35,12 +35,26 @@ const initPHPSearch = async (language) => { return data; }; + const cleanupOld = async() => { + // Previously used cache key + const key = `search-${language}`; + const oldCache = window.localStorage.getItem(key); + if (!oldCache) { + return; + } + + // Clear is used to clear all cached languages at the same time + // This reduces the chances of hitting quota + window.localStorage.clear(); + } + /** * Fetch the search index. * * @returns {Promise} The search index. */ const fetchIndex = async () => { + await cleanupOld(); const key = `search2-${language}`; let items; if (language === 'local') { @@ -60,7 +74,7 @@ const initPHPSearch = async (language) => { } catch (e) { // Local storage might be full, or other error. // Just continue without caching. - console.error("Failed to cache search index", e); + console.info("Failed to cache search index", e); } }