fix: refactor onMount to avoid unnecessary async calls for search (#481)

* fix: refactor onMount to avoid unnecessary async calls for search

* Production debugging to check the console

* feat: enhance Pagefind integration with improved loading checks and error handling

* fix: improve Pagefind integration with event handling for loading and errors
This commit is contained in:
Katsuyuki Karasawa authored and GitHub committed 2025-06-06 21:11:03 +09:00
1 parent 690158abca
commit 9de7aa10e4
3 files changed
+86 -14

No files matched your search

+31 -8
View File
@@ -105,13 +105,36 @@ loadButtonScript();
{import.meta.env.PROD && <script is:inline define:vars={{scriptUrl: url('/pagefind/pagefind.js')}}>
async function loadPagefind() {
const pagefind = await import(scriptUrl)
await pagefind.options({
'excerptLength': 20
})
pagefind.init()
window.pagefind = pagefind
pagefind.search('') // speed up the first search
try {
const response = await fetch(scriptUrl, { method: 'HEAD' });
if (!response.ok) {
throw new Error(`Pagefind script not found: ${response.status}`);
}
const pagefind = await import(scriptUrl);
await pagefind.options({
excerptLength: 20
});
window.pagefind = pagefind;
document.dispatchEvent(new CustomEvent('pagefindready'));
console.log('Pagefind loaded and initialized successfully, event dispatched.');
} catch (error) {
console.error('Failed to load Pagefind:', error);
window.pagefind = {
search: () => Promise.resolve({ results: [] }),
options: () => Promise.resolve(),
};
document.dispatchEvent(new CustomEvent('pagefindloaderror'));
console.log('Pagefind load error, event dispatched.');
}
}
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', loadPagefind);
} else {
loadPagefind();
}
loadPagefind()
</script>}