Skip to content

Instantly share code, notes, and snippets.

@tcrowe
Created September 25, 2019 21:17
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tcrowe/0b4a44fb76418e7c6e77138fb8cd79aa to your computer and use it in GitHub Desktop.
Save tcrowe/0b4a44fb76418e7c6e77138fb8cd79aa to your computer and use it in GitHub Desktop.
Sapper preload instead of using stores for dynamic data
<script context="module">
/*
Preload runs on the client AND server
*/
export async function preload({ path, query, params, session }) {
console.log('path', path)
console.log('query', query)
console.log('params', params)
console.log('session', session)
// https://sapper.svelte.dev/docs#Server_routes
const res = await this.fetch("/request-to-your-server-route-path");
const data = await res.json();
// ⚠️ customize with your data properties
const { prop1, prop2, prop3 } = data;
return { prop1, prop2, prop3 };
}
</script>
<script>
/*
Preload has given the requested data to the page as props
*/
// ⚠️ customize with your data properties
export let prop1;
export let prop2;
export let prop3;
console.log('prop1', prop1)
console.log('prop2', prop2)
console.log('prop3', prop3)
</script>
<div>prop1: {prop1}</div>
<div>prop2: {prop2}</div>
<div>prop3: {prop3}</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment