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
  1. SvelteKit Server Fetching and Deployment

Finding correct query in api based on url slug

This code is an asynchronous JavaScript function that fetches project data from an external API and processes it to find a specific project based on a URL-friendly version of its name. Here’s a summar

Replace Name_Project with your own name in your api

[slug]/+page.js
import { error } from '@sveltejs/kit';
import { MakeUrlFriendly } from '$lib/utils.js';

export async function load({ fetch, params }) {
  try {
    const response = await fetch('https://api.example.com/data');
    
    if (!response.ok) {
      throw new Error(`HTTP error! status: ${response.status}`);
    }
    
    const data = await response.json();
    let posts = data.data;
    let matchingPost = posts.find((matchingPost) => MakeUrlFriendly(matchingPost.name) === params.slug);
    return { matchingPost };
  } catch (e) {
    console.error('Error fetching data:', e);
    throw error(500, 'Error fetching data from API');
  }
}
<script>
	export let data;
	console.log(data.matchingPost);
</script>

<main>
	<!-- code here -->
</main>
{
  "data": [
    {
      "id": 1,
      "sort": null,
      "date_created": "2024-07-25T08:58:03.569Z",
      "Name_Project": "Prosifier",
      "rol_en_service": "Design & Development",
      "Jaar": "2022",
      "urlsite": null,
      "featuredImage": "9e0a5de1-1605-4441-b08b-a100d35b76c9",
      "footage1": "9e0a5de1-1605-4441-b08b-a100d35b76c9",
      "footage2": "9e0a5de1-1605-4441-b08b-a100d35b76c9",
      "footage3": "9e0a5de1-1605-4441-b08b-a100d35b76c9",
      "mobilescreenshot1": "9e0a5de1-1605-4441-b08b-a100d35b76c9",
      "mobilescreenshot2": "9e0a5de1-1605-4441-b08b-a100d35b76c9"
    },
    {
      "id": 2,
      "sort": null,
      "date_created": "2024-07-25T15:30:34.110Z",
      "Name_Project": "Berg & Bos",
      "rol_en_service": "Design",
      "Jaar": "2024",
      "urlsite": null,
      "featuredImage": "5df29114-2660-4192-bf72-4c26087d9264",
      "footage1": "4370768a-5ecf-4398-ab27-ac58300b94c4",
      "footage2": "8162fd4b-83c7-40c5-9134-a2e8ac9ecb33",
      "footage3": "c3abc53e-24c7-41fb-938a-516971ff98c4",
      "mobilescreenshot1": null,
      "mobilescreenshot2": null
    }
  ]
}

PreviousCreating slug friendly urls based on query data

Last updated 9 months ago