question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

(re)trigger the load function

See original GitHub issue

Is 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:closed
  • Created 2 years ago
  • Reactions:8
  • Comments:16 (5 by maintainers)

github_iconTop GitHub Comments

5reactions
icalvin102commented, Jun 16, 2021

@etienneburdet goto() will only run load() if page.params, page.path, page.query, session or context is used inside of load and one of those values has changed since the last run of the load function. This is the desired behavior of goto 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 a load function instead of invalidating a URI.

The use case is similar to the one described in #1277 but with a more general solution.

1reaction
lemmoncommented, Jun 17, 2021

Perhaps something like?

goto(href, { replaceState: true, invalidate: true })

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 custom fetch() lib.

Read more comments on GitHub >

github_iconTop Results From Across the Web

What triggers the firing of the "onLoad" event? - Stack Overflow
It's a function to execute when the DOM is fully loaded. $(document).ready(function() { // Handler for .ready() called.
Read more >
.load() | jQuery API Documentation
An object containing data that will be passed to the event handler. ... A function to execute each time the event is triggered....
Read more >
jQuery load() Method - W3Schools
The load() method attaches an event handler to the load event. The load event occurs when a specified element has been loaded. This...
Read more >
Window: load event - Web APIs - MDN Web Docs - Mozilla
The load event is fired when the whole page has loaded, including all dependent resources such as stylesheets, scripts, iframes, and images.
Read more >
Trigger Lazy-Loading yourself through code - Extensions
And that's it, the image will be lazy-loaded. This function displaymedia() works with any element that has an src, srcset, or style attribute....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found