Skip to content

Best practice for using svetch within load functions #7

Answered by Bewinxed
jessecoleman asked this question in Q&A
Discussion options

You must be logged in to vote

I'm happy that you're using it! Thank you :)

The one where you put a load function into it is for server-side, because if you fetch without passing it on backend, then relative urls + sveltekit optimizations won't apply.

The pattern I use is this (you can externalize this in a separate file for verbosity:

//+layout.svelte
import { Svetch } from 'svetch.ts'
const svetch = new Svetch(...)
setContext("svetch", svetch)

Then in child routes

import type { Svetch } from 'svetch.ts'
const svetch = getContext<Svetch>("svetch")

you can make this faster, in a separate file

import { hasContext, getContext, setContext } from "svelte";
import { Svetch } from "svetch.ts";

export function setSvetchContext(

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by Bewinxed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants