using-remote-functions
Create SvelteKit components using Remote Functions for type-safe client-server communication. Use when building components that need to fetch data, submit forms, or execute server commands. Remote Functions work at the component level, not page level.
Installation
Pick a client and clone the repository into its skills directory.
Installation
About this skill
Create SvelteKit components using Remote Functions for type-safe client-server communication. Use when building components that need to fetch data, submit forms, or execute server commands. Remote Functions work at the component level, not page level.
How to use
Otwórz plik
svelte.config.jsw projekcie SvelteKit i dodaj flagęremoteFunctions: truew sekcjikit.experimental. Opcjonalnie włączasync: truewcompilerOptions.experimental, aby móc używaćawaitbezpośrednio w komponentach.W swoim komponencie utwórz plik
data.remote.tsw tym samym katalogu. W pliku tym zdefiniuj funkcję zdalną — na przykład funkcjęquerydo pobierania danych lubformdo obsługi wysyłania formularza. Każda funkcja powinna być eksportowana i zawierać logikę serwera.W komponencie
.sveltezaimportuj funkcję zdalną i wywołaj ją jak zwykłą funkcję JavaScript. SvelteKit automatycznie obsługuje komunikację między klientem a serwerem — nie musisz pisać żadnych tras API.Dla formularzy użyj typu
formzamiastcommand— Remote Functions preferują deklaratywne podejście do mutacji danych. Formularz będzie działać nawet bez JavaScript dzięki progresywnej poprawie.Zorganizuj logikę tak, aby jak najmniej kodu było w pliku
+page.svelte, a jak najwięcej wdata.remote.ts. Strona powinna być czystym renderowaniem — mapowaniem typów na komponenty, podczas gdy serwer buduje ostateczne struktury danych.