fix(svelte): replace $effect with onMount for side effects

$effect runs during SSR and re-runs on every reactive dependency change,
causing polling loops and URL reads to fire at the wrong time. onMount
runs once on the client after first render, which is the correct lifecycle
for polling, URL param reads, and async data loads.

- IndexingProgress: polling loop now starts on mount, not on reactive trigger
- search/+page.svelte: URL param init moved to onMount; use window.location
  directly instead of the page store to avoid reactive re-runs
- settings/+page.svelte: config load and local provider probe moved to onMount

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Giancarmine Salucci
2026-03-25 14:30:04 +01:00
parent 215cadf070
commit a63de39473
3 changed files with 224 additions and 203 deletions

View File

@@ -1,11 +1,11 @@
<script lang="ts">
import { goto } from '$app/navigation';
import { page } from '$app/state';
import LibraryResult from '$lib/components/search/LibraryResult.svelte';
import SnippetCard from '$lib/components/search/SnippetCard.svelte';
import SearchInput from '$lib/components/search/SearchInput.svelte';
import { copyToClipboard } from '$lib/utils/copy-to-clipboard';
import type { LibrarySearchJsonResult, SnippetJson } from '$lib/server/api/formatters';
import { onMount } from 'svelte';
// ---------------------------------------------------------------------------
// State
@@ -38,9 +38,9 @@
// Initialise from URL params on mount
// ---------------------------------------------------------------------------
$effect(() => {
const libParam = page.url.searchParams.get('lib');
const qParam = page.url.searchParams.get('q');
onMount(() => {
const libParam = new URL(window.location.href).searchParams.get('lib');
const qParam = new URL(window.location.href).searchParams.get('q');
if (libParam) {
selectedLibraryId = libParam;
@@ -50,7 +50,7 @@
query = qParam;
}
if (libParam && qParam) {
searchDocs();
void searchDocs(false);
}
});
@@ -81,7 +81,7 @@
}
}
async function searchDocs() {
async function searchDocs(syncUrl = true) {
if (!selectedLibraryId || !query.trim()) return;
loadingSnippets = true;
snippetError = null;
@@ -89,10 +89,11 @@
totalTokens = 0;
try {
const url = new URL('/api/v1/context', window.location.origin);
url.searchParams.set('libraryId', selectedLibraryId);
url.searchParams.set('query', query);
const res = await fetch(url);
const params = new URLSearchParams({
libraryId: selectedLibraryId,
query
});
const res = await fetch(`/api/v1/context?${params.toString()}`);
if (!res.ok) {
const data = await res.json();
throw new Error(data.error ?? `Request failed (${res.status})`);
@@ -101,11 +102,12 @@
snippets = data.snippets ?? [];
totalTokens = data.totalTokens ?? 0;
// Sync URL state.
goto(
`/search?lib=${encodeURIComponent(selectedLibraryId)}&q=${encodeURIComponent(query)}`,
{ replaceState: true, keepFocus: true }
);
if (syncUrl) {
goto(
`/search?lib=${encodeURIComponent(selectedLibraryId)}&q=${encodeURIComponent(query)}`,
{ replaceState: true, keepFocus: true }
);
}
} catch (e) {
snippetError = (e as Error).message;
} finally {