Documentation Shento Hendriks
Svelte / SvelteKit
Svelte / SvelteKit
  • Getting Started with SvelteKit
    • Installing SvelteKit
      • Tailwind Typography plugin
    • Base Syntax & Core Features of Svelte
      • Curly Braces & Understanding Core Syntax
      • Reactive Variables
      • Two-way-binding Shortcut
      • Using multiple components
      • Components & Communication via Props
      • Display HTML in Svelte
      • Dynamic classes
      • Using $effect
      • Working with Javascript and Rune reactivity
    • Working with Conditionals & Loops
      • Showing code conditionally in Svelte
      • Looping through arrays (lists)
      • Lists & Keys
    • Closer look at reactivity
      • Updating Arrays & Objects
      • Event Modifiers
    • Component Events
    • Best Practices
    • Adding Google Font in Tailwind and SvelteKit
    • Adding Local Fonts to SvelteKit with Tailwind
    • Prevent flicker in images
    • Enhanced Images
    • Form Submissions in SvelteKit
    • Effective SEO in SvelteKit
    • Automatic Site Maps in Svelte
    • Creating Custom Elements with Svelte, SvelteKit, and Vite
    • Creating a Svelte Library with JavaScript Functions
  • Useful Code Snippets
    • Hover effects
    • Navigation
    • Centering Items
    • Setting Footer Bottom Page
  • Installing Useful Libraries
    • Documenting Javascript with JSDocs (Crash Course)
    • Adding Lottie Animations to Svelte Application
    • SvelteKit Melt-ui
      • First Component - accordion
    • Installing Shadcn/ui Svelte
    • Getting Started with GSAP with SvelteKit
      • What is GSAP?
      • Installing GSAP & Your first animation
      • GSAP in SvelteKit
    • Installing Locomotive Scroll with Svelte 5 and SvelteKit
  • SvelteKit Server Fetching and Deployment
    • Preparing a SvelteKit application for Deployment
    • Utility Functions for URL Handling in SvelteKit
    • Fetching Directus Data from Rest API with SvelteKit
    • Fetching data from api with caching
    • Displaying Fetched Data in a Each Loop
    • Creating slug friendly urls based on query data
    • Finding correct query in api based on url slug
Powered by GitBook
On this page
  • Understanding the Cache Structure
  • Implementing the Load Function
  • Fetching Data from the API
  • Using the Cached Data in a Svelte Component
  • Conclusion
  1. SvelteKit Server Fetching and Deployment

Fetching data from api with caching

This guide will walk you through the process of creating a basic stale-while-revalidate system using SvelteKit. This technique is particularly useful for improving performance by reducing unnecessary API calls and serving data more quickly.

Understanding the Cache Structure

We'll start by creating a simple cache object:

const cache = {
  data: null,
  lastFetched: 0
};

const CACHE_DURATION = 60 * 1000; // 1 minute in milliseconds

This cache object has two properties:

  • data: Stores the cached data

  • lastFetched: Timestamp of when the data was last fetched

We also define CACHE_DURATION, which sets how long the cached data remains valid (1 minute in this case).

Adjusting the CACHE_DURATION allows you to balance between data freshness and performance. A longer duration reduces API calls but may serve slightly outdated data.

Implementing the Load Function

Next, we'll create an asynchronous load function that manages the caching logic:

const cache = {
  data: null,
  lastFetched: 0
};

const CACHE_DURATION = 60 * 1000; // 1 minute in milliseconds

export async function load({ setHeaders }) {
  const now = Date.now();

  // Check if we have cached data and it's still fresh
  if (cache.data && now - cache.lastFetched < CACHE_DURATION) {
    // Serve cached data immediately
    return cache.data;
  }

  // Fetch fresh data
  const freshData = await fetchDataFromAPI();

  // Update cache
  cache.data = freshData;
  cache.lastFetched = now;

  // Set cache headers
  setHeaders({
    'Cache-Control': `max-age=0, s-maxage=${CACHE_DURATION / 1000}, stale-while-revalidate`
  });

  return freshData;
}

This function does the following:

  1. Checks if cached data exists and is still fresh

  2. If cache is valid, returns cached data immediately

  3. If cache is invalid or doesn't exist, fetches fresh data from the API

  4. Updates the cache with fresh data

  5. Sets appropriate cache headers

  6. Returns the fresh data

The setHeaders function is framework-specific. Ensure your setup supports this method or adjust accordingly.

Fetching Data from the API

The fetchDataFromAPI function is responsible for making the actual API call:

// Rest of code

async function fetchDataFromAPI() {
  const response = await fetch('https://admin.shentohendriks.nl/items/projects');
  const json = await response.json();
  return json;
}

This function fetches data from a specific API endpoint and returns the parsed JSON response.

Using the Cached Data in a Svelte Component

Finally, let's look at how to use this cached data in a Svelte component:

<script>
  export let data;
</script>

<img src={`https://admin.shentohendriks.nl/assets/${data.data[0].featuredImage}?key=compression`} alt="">

This Svelte component receives the data prop, which contains the cached or freshly fetched data. It then uses this data to display an image.

Ensure that the data structure matches what your component expects. In this case, it assumes data.data.featuredImage exists.

Conclusion

This simple in-memory caching system can significantly improve your application's performance by reducing unnecessary API calls. Remember to adjust the cache duration and error handling to suit your specific needs.

While this example uses in-memory caching, for larger applications or server-side caching, consider using more robust caching solutions like Redis or Memcached.

PreviousFetching Directus Data from Rest API with SvelteKitNextDisplaying Fetched Data in a Each Loop

Last updated 9 months ago