anggoran
anggoran4mo ago

Signals & Data Fetching

1. Should I declare useSignal on route or island? 2. How to fetch data? Make an async route? Do useEffect?
8 Replies
anggoran
anggoran4mo ago
I'm kinda confused about the solution, since we can't declare useSignal within async route.
marvinh.
marvinh.4mo ago
1. to declare a signal in an async route use:
const mySignal = signal(123)
const mySignal = signal(123)
The useSignal doesn't work because async routes are not components 2. Depends on where you want to fetch data. If on the server, fetch it in your route and pass it down. On the client, trigger fetch based on user interaction like on click or inside a useEffect
anggoran
anggoran4mo ago
in the tutorial it is stated that As a shortcut for combining a GET handler with a route, you can define your route as async. , so to fetch on server, async route and GET handler are the same?
marvinh.
marvinh.4mo ago
yep
anggoran
anggoran4mo ago
alright, thank you very much!
mrk
mrk4mo ago
Some thoughts: Would there be a reason to pass data wrapped in a signal from an async route to a client component/island? Reactivity is not carried over the wire and the server has no client/user interaction, so no need for reactivity there, right? I guess you where just answering for completeness @marvinh. , since signals are indeed serializable by fresh and sent over-the-wire. Just making sure I'm not missing out on some server signal use case 😃 This reminds me of a blogpost I read about Ryan Carniato where he hints on a future with server-to-client signals, that is, where reactivity trascends the server/client border. Maybe this could be done using WebSockets/SSE, definitively interesting...
marvinh.
marvinh.4mo ago
Yeah the reactivity transcending across the server client boundary is something I'd love to explore more this year. I feel like that's the direction frameworks are moving towards. Creating a signal in an async route may still make sense if it is passed to different islands. Both islands will subscribe to the exact same signal in the client in this case.
mrk
mrk4mo ago
Oh I see, so it does make sense as a great place to declare shared global signals for multiple islands to import 👌 ... And yeah, after using fresh for a while, the server-client boundary is what takes the most time to get used to and requires changing one's mental model (coming from SPA's myself at least). I see how this could hugely enhance DX if signal reactivity could cross this border. Apart from frameworks looking into these patterns, there's also SignalDB which accomplishes something similar (more like a Signals-based abstraction over arbitrary databases/datastores/apis), though it's framework agnostic. Might be of interest too 👍