feat: SSR shopping list by fetching initial data server-side

Server load now fetches the shopping list from the DB and passes it as
initialList. The sync layer seeds state immediately in the script block
(not onMount) so SSR renders the full list. SSE connects client-side
in onMount for real-time updates.
This commit is contained in:
2026-04-09 23:20:11 +02:00
parent 6c4367b939
commit c3e829d3ca
3 changed files with 47 additions and 3 deletions
+16
View File
@@ -166,6 +166,20 @@ export function createShoppingSync() {
}
}
/** Seed state from server-loaded data (safe to call during SSR). */
function seed(initialList: { version: number; items: ShoppingItem[] }, token?: string | null) {
if (token) shareToken = token;
version = initialList.version;
items = initialList.items || [];
status = 'synced';
}
/** Connect SSE for real-time updates (client-only, call in onMount). */
function connect(token?: string | null) {
if (token) shareToken = token;
connectSSE();
}
function addItem(name: string, user: string, category = 'Sonstiges') {
items = [...items, {
id: generateId(),
@@ -211,6 +225,8 @@ export function createShoppingSync() {
get version() { return version; },
apiUrl,
init,
seed,
connect,
addItem,
toggleItem,
removeItem,