Skip to content

Commit 6f0a7da

Browse files
authored
Fixed BrowserPod search results returning 404s (#364)
1 parent 604d3fa commit 6f0a7da

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

packages/astro-theme/components/search/Pagefind.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575
{:else}
7676
<ol class="flex flex-col gap-2">
7777
{#each results as result (result.id)}
78-
<Result {result} />
78+
<Result {result} {baseUrl} />
7979
{/each}
8080
</ol>
8181
{/if}

packages/astro-theme/components/search/Result.svelte

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
11
<script lang="ts">
22
export let result: PagefindResult;
3+
export let baseUrl: string = "/";
34
45
const showExcerpt = false; // Makes the UI quite busy.
56
7+
// Add base path (e.g., /docs/) to search result URLs for browser routing.
8+
// Only does so if base isnt root. If it is docs would be a subdir and be already in URL
9+
// Also checks if url already starts with base for extra defense against /docs/docs
10+
function prependBase(url: string): string {
11+
if (baseUrl === "/" || url.startsWith(baseUrl)) {
12+
return url;
13+
}
14+
return baseUrl + url;
15+
}
16+
617
// Based on thin_sub_results from https://github.com/CloudCannon/pagefind/blob/main/pagefind_ui/default/svelte/result_with_subs.svelte
718
function thinSubResults(
819
pageUrl: string,
@@ -38,7 +49,7 @@
3849
{:then document}
3950
<li class="rounded-md bg-stone-900">
4051
<a
41-
href={document.url}
52+
href={prependBase(document.url)}
4253
class="rounded-t-md block border border-transparent hover:border-stone-500 py-3 px-4 text-lg"
4354
class:rounded-b-md={document.sub_results.length <= 1}
4455
>
@@ -57,7 +68,7 @@
5768
{#each document.sub_results as subResult, i}
5869
<li>
5970
<a
60-
href={subResult.url}
71+
href={prependBase(subResult.url)}
6172
class="block border border-transparent hover:border-stone-500 text-stone-300 py-2 px-4 whitespace-nowrap text-ellipsis overflow-hidden"
6273
class:rounded-b-md={i === document.sub_results.length - 1}
6374
>

0 commit comments

Comments
 (0)