Created
September 25, 2019 21:17
-
-
Save tcrowe/0b4a44fb76418e7c6e77138fb8cd79aa to your computer and use it in GitHub Desktop.
Sapper preload instead of using stores for dynamic data
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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