-
-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Invalidate fetched URLs #1277
Comments
So just to make sure I'm understanding, this is an app-level invalidation, sort of along the lines of things we said only something like Sapper/Kit could do. The idea is that when one invalidates, it reruns every page load function (drawing on the cached response) w/ a matching path? Hard to see any drawbacks. |
This sounds sensible to me. I'm wondering now, though, what a fat-client-middle-ground thing would look like. Since props no longer need to be serializable, it feels like it might be as simple as having In cases where you don't want to bother with a fat client and where you just want to invalidate/refetch certain data, I think the proposed API seems very reasonable. |
I too like this idea. The simplicity and explicitly also makes it possible to enhance this as you please. For example as Conduitry mentioned, you could then enhance your load function with stuff like store calls. Would it be a good idea to be able to pass data along with the invalidate? Suppose you have a todo list, the load function returns a store of the todos and can control that state. Now you enter a new item, you invalidate and send along the new item. The load function could then do an optimistic update on the store it controls. Is this a good thing to be able to do or does this blow up load too much (doing too many things at once)? |
I think for something like that you'd just update the store yourself and then call the invalidate function, so that when If you're passing data to the invalidate function along with a URL to invalidate, it's not clear to me where that would go. |
What if you have multiple What if there was an alternative option to export an array of loads instead? Rough example: <!-- src/routes/$layout.svelte -->
<script context="module">
export const load = [
async ({ fetch }) => {
const notifications = await fetch('/notifications.json').then(r => r.json());
return {
props: { notifications }
}
},
async ({ fetch }) => {
const updates = await fetch('/updates.json').then(r => r.json());
return {
props: { updates }
}
},
async ({ fetch }) => {
const cart = await fetch('/cart.json').then(r => r.json());
return {
props: { cart }
}
},
async ({ fetch }) => {
const menuItems= await fetch('/menu-items.json').then(r => r.json());
return {
props: { menuItems }
}
}
]
</script> Invalidating would work the same as in the original proposal, but instead you could pass the invalidate(2); // Invalidates the `/cart.json` load |
This would be another good use case for passing an explicit parameter to the load function that is defined on subsequent loads. The array feels a little boilerplate-y and maybe too inflexible for me. |
* invalidate resource * failing test for #1277 * fix bad this reference, strengthen test * remove code typescript doesnt like * rename invalidates -> dependencies * implement batching, bypass cache * changeset and docs Co-authored-by: Tan Li Hau <[email protected]>
It would be nice if we could regex match resources here... It's a little annoying if you have dynamic fetches based on for example query parameters. So it would be cool if you could do something like My usecase is that I am planning on calling |
I have a similar use case, we have some pretty complex |
Had the same issue, needed to invalidate some endpoints, wrote a plugin to automate it https://github.com/n1kk/vite-plugin-sveltekit-fetch-invalidate It can automatically figure out what to invalidate if your endpoints are simple and don't have parameters in them, otherwise, you can specify a custom list. In one of my projects it's a simple glob |
Does |
Is your feature request related to a problem? Please describe.
Suppose you were building an e-commerce site. You might have a shopping cart icon in your root
src/routes/$layout.svelte
component. Insidesrc/routes/products/[category]/[id].svelte
you might have an 'add to cart' button. When the user clicks the button, the cart icon should update.One perfectly valid way to model this would be to put the user's cart on the
session
store, and then update the store after the POST triggered by 'add to cart' succeeds:But you might have valid reasons for not wanting to put that data in
session
— perhaps the cart is only visible on some parts of the app and you don't want to always pay the cost of serializing the data. Instead, you might do something like this:This creates a problem. POSTing to
/cart.json
will add the product(s) to the cart, but will no longer cause the cart icon to update, because the layout component has no way of knowing that the result of fetching/cart.json
has changed.Describe the solution you'd like
Since we're using the passed-in
fetch
, we know which URLs the layout component depends on. We could therefore expose an API like this:SvelteKit could straightforwardly keep track of which
load
functions fetched the invalidated URL, and rerun them (assuming the components they belong to are still mounted), much as we already do forload
functions that usesession
when that store changes value.Describe alternatives you've considered
This is a somewhat low-tech solution. A more common approach might be to use a full-blown fat-client state management system that has a copy of the model in memory (see e.g. Firebase). One possibility would be to integrate more closely with such systems or even build one ourselves.
Personally I'm more inclined towards simple, explicit mechanisms that are easy to implement and understand; in any case this would be complementary towards the more full-blown solutions (or at the very least, not in conflict).
Other possibilities that I'm not keen on:
load
functions automaticallyHow important is this feature to you?
It's a nice-to-have — it solves a problem that can already be solved in various ways, but arguably in a more elegant way. And it's the one feature I've seen in Remix that we don't already have a (more elegant, IMHO 💅 ) equivalent of that I'm aware of :)
(Their version re-runs all loaders indiscriminately whenever an 'action' — their word for POST/PUT/PATCH/DELETE etc requests — takes place. AFAICT it wouldn't work with external APIs, just the app's own endpoint. But it does have the nice characteristic that it runs automatically.)
The text was updated successfully, but these errors were encountered: