Replies: 3 comments 1 reply
-
|
getPost(foo).refresh() |
Beta Was this translation helpful? Give feedback.
-
I have to agree that as I read this, I see the words, but I don't understand at all. What is helpful is concrete examples (and counter-examples), and breaking down step by step what is happening in each case. Summarizing key take-aways and highlighting possible misconceptions is also helpful. |
Beta Was this translation helpful? Give feedback.
-
|
I agree, the docs could use some work in general. Some more in depth explanations from what I know in case that helps: "same reference"Parameters passed to remote functions are stringified (think of This string key is used to look up queries in the cache. // same object reference of foo is not relevant here, but they will both look up the same JSON.stringify key of '{"id":1}'
const foo = { id: 1 };
getPost(foo) === getPost(foo);
// this is true, as both times JSON.stringify({ id: 2 }) will look up the same string key of '{"id":2}'
getPost({ id: 2 }) === getPost({ id: 2 }) One thing to note here though, is that this also means that order of items is important! Which is quite a bummer and should either be addressed by the implementation, or noted in the docs. // this will be false and will lead to another network request, as one will use the key '{"a":1,"b":2}' and the other will use '{"b":2,"a":1}'
getPost({ a: 1, b: 2 }) === getPost({ b: 2, a: 1 })"while they're on the page"Its actually pretty simple if you're familiar with the let cachedValue = null;
const subscribe = createSubscriber(() => {
// called one time when getPosts is called in a "tracked" context.
return () => {
// called when no more subscribers exist from those "tracked" environments.
cachedValue = null; // invalidates cache
}
});
async function getPosts() {
subscribe();
if (!cachedValue) cachedValue = await fetch(...);
return cachedValue;
}This is just a very contrived example, it gets a bit more complicated if there are parameters involved, but I hope you get the idea. So by "navigating away" you are destroying all svelte components that have initially called the query, which in turn unsubscribes all the signals depending on it. Another note here: This only works if the query is actually called in a tracked context. <script>
const posts = await getPosts() // this is untracked!
const posts = $derived(await getPosts()) // this is tracked
function example() {
const posts = await getPosts() // this is untracked (unless, example itself is called inside a $derived())
}
</script>
{await getPosts()} This is trackedIn the untracked cases, it will invalidate the cache immediately, unless there are active subscribers to the cache that are active at the moment. So if you care about performance, don't forget |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
In the Remote Function docs, it says:
WTF does "while they're on the page" mean? What does "cached" (an overloaded term) mean?
My working interpretation of what was meant is:
Queries run once per navigation. Re-running the same query returns the exact same data reference (so
getPosts() === getPosts()istrue). Because the query value is stable, you don’t need to store it in a local reference (for example const posts = getPosts()) in order to update the query.Is my interpretation correct? If it's cached (in memory?), what busts the cache? Navigation? 5 second timer?
Beta Was this translation helpful? Give feedback.
All reactions