Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
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)
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 };
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)
<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
You can’t perform that action at this time.