Quantcast
Channel: Active questions tagged svelte - Stack Overflow
Viewing all articles
Browse latest Browse all 1541

Svelte: is there a way to cache the API result in a way that it won't trigger the API call every time the component renders?

$
0
0

It could be that I'm typing the wrong things in Google and can't get a good answer.

Is there a "svelte recommended" way to store the value of a GET result, so that, on every refresh or link switch, the result in the store is used in the component until a timeout (where the API is called again)?

My purpose is to fetch blogposts from an external API and show them in a list but not on every refresh, or link switch.

My code:

<script>  let posts = [];  onMount(async () => {    const res = await fetch(apiBaseUrl +"/blogposts");    posts = await res.json();  });</script>{#each posts as post}<h5>{post.title}</h5>{/each}

In pseudo-code what I want:

if (store.blogposts.timeout === true){  onMount(...);  // renew component} 

Viewing all articles
Browse latest Browse all 1541

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>