Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fetch Prismic.io data #6

Open
2 tasks
M4TThys123 opened this issue Jan 21, 2023 · 1 comment
Open
2 tasks

Fetch Prismic.io data #6

M4TThys123 opened this issue Jan 21, 2023 · 1 comment

Comments

@M4TThys123
Copy link
Owner

M4TThys123 commented Jan 21, 2023

  • Load data using *Best-Practice`
  • Add a Prefetch
@M4TThys123 M4TThys123 assigned M4TThys123 and unassigned M4TThys123 Jan 21, 2023
@M4TThys123
Copy link
Owner Author

Data Fetching/Loading

IMG_4976
Bron

Met de load functie kan je data vanuit de +page.js naar de +page.svelte sturen.

+page.svelte

<script>
	export let data;
	console.log(data)
	const { products } = data; 
</script>

<h1>Shop</h1>
{#each products as product}
	<h2>{product.title}</h2>
	<p>{product.description}</p>
{/each}

Best Practice

  • Gebruiken bij 1 of meer fetches
  • Nu heb je geen waterfall
  • fetches worden paralel uitgevoerd.

+page.js

export const load = async ({ fetch }) => {
	// Async Fetch 1
	const fetchProducts = async () => {
		const productRes = await fetch('https://dummyjson.com/products?limit=10');
		const productData = await productRes.json();
		return productData.products;
	};

	// Async Fetch 2
	const fetchUsers = async () => {
		const usersRes = await fetch('https://dummyjson.com/users?limit=10');
		const usersData = await usersRes.json();
		return usersData.users;
	};

	return {
		products: fetchProducts(),
		users: fetchUsers()
	};
};

Bad Practice

  • Gebruiken bij 1 of meer fetches
export const load = async ({ fetch }) => {
    // Fetch 1
    const productRes = await fetch ('https://dummyjson.com/products?limit=10')
    const productData = await productRes.json()
    const products = productData.products

    // Fetch 2
    const usersRes = await fetch ('https://dummyjson.com/users?limit=10')
    const usersData = await usersRes.json()
    const users = usersData.users

    return {
        products: products,
        users: users
    }
}

Prefetch

+layout.svelte

Voeg data-sveltekit-prefetch toe aan een <a></a>

<nav>
	<a href="/">Home</a>
	<a href="/shop" **data-sveltekit-prefetch**>Shop</a>
	<a href="/movies" **data-sveltekit-prefetch**>Movies</a>
</nav>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant