How does one use the pagefind JavaScript API within Sveltekit? #328
-
I want to use pagefind to search through my mostly statically generated website, but I'm running into a lot of issues trying to get pagefind to work. I'm also building a custom search UI, which is why I need the JavaScript API. I'm using Sveltekit 1.20.4 and am using Vite as the build tool. My build directory is Sveltekit's default build directory, The files below are inside the folder This is the search UI in a <script lang="ts">
export let data;
const results = data.results;
</script>
{#each results as result}
<div>{result.url}</div>
<div>{result.excerpt}</div>
{/each} This is the export const prerender = false;
export async function load({ params, url }) {
const searchQuery = url.searchParams.get("q")?.trim();
if (!searchQuery || searchQuery.length < 1) return { results: [] };
const pagefind = await import("/_pagefind/pagefind.js");
const search = await pagefind.search(searchQuery);
const results = await Promise.all(search.results.map(r => r.data()));
return { results: results };
} The Also, even when the site has been indexed, Sveltekit is unable to find the file that is being imported in the Please help, thank you. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Refer to issue #327 for the answer. |
Beta Was this translation helpful? Give feedback.
Refer to issue #327 for the answer.