(re)trigger the load function
See original GitHub issueIs your feature request related to a problem? Please describe.
The new invalidate(href)
feature in $app/navigation
is a great way to trigger the execution of a load
function that works in most cases. The problem is that this only works if one uses the fetch
function that is provided as an argument to load
. If a apiClientWrapper is used this does not work anymore.
Describe the solution you’d like
One solution would be to pass a invalidate
or update
function to the load
so that I can be passed as a prop or via context.
This solution would update a specific load
function instead of a specific fetch url.
<script context="module">
import { api } from '$lib/myapi';
export async function load({page, update}){
const item = await api.getItem(page.params.id);
return {
props: { item, update }
}
}
</script>
<script>
import Item from '$lib/Item.svelte';
export let item;
export let update;
</script>
<Item {item} />
<button on:click={update}>Reload</button>
Describe alternatives you’ve considered A alternative would be to write a second function in the (non module) script tags that replicates the load function to update data.
How important is this feature to you? Somewhat important. It would make the code cleaner and more reusable but there are easy workarounds.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:8
- Comments:16 (5 by maintainers)
@etienneburdet
goto()
will only runload()
ifpage.params
,page.path
,page.query
,session
orcontext
is used inside ofload
and one of those values has changed since the last run of the load function. This is the desired behavior ofgoto
or any form of navigation as this prevents unnecessary loading of (sub)routes.What I want is function that is similar to
invalidate
but it should be bound to aload
function instead of invalidating a URI.The use case is similar to the one described in #1277 but with a more general solution.
Perhaps something like?
There are still many different use cases when you want invalidate page data only or invalidate both page and layout altogether. Anyway, the
invalidate
function seems to be no good when you use customfetch()
lib.