I've got a different pattern working pretty well now.
For the record, I'm doing all of this to use realtime updates from supabase.
This pattern allows me to create a cache and load everything on the server while still taking advantage of realtime updates using a store.
Here's how it works.
Get the notes on the server
const getNotes = cache(async () => {
"use server";
}, "notes");
Call load() to take effect of loading the data on hover.
export const route = {
load: () => getNotes(),
};
Create a reactive notes value with createAsync()
const notes = createAsync(() => getNotes());
Pass notes to a child component
<Notes notes={notes} />
Inside of <Notes /> load the data into a store and react to any realtime updates.
interface NoteType {
id: string;
note: string;
}
interface NotesProps {
notes: Accessor<any[] | undefined>;
}
export default function Notes(props: NotesProps) {
const [notesStore, setNotesStore] = createStore<NoteType[]>([]);
createEffect(async () => {
const notes = props.notes() ?? [];
setNotesStore(notes);
});
}
This seems like a pretty good solution. Would you agree?