Last active
October 15, 2024 12:34
-
-
Save david-plugge/33cd821a4c829059f67ce18468b8dfca to your computer and use it in GitHub Desktop.
SvelteKit shared load
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
import { getApiV1UserOptions } from '$lib/api/client/@tanstack/svelte-query.gen.js'; | |
import { getLoadContext } from '$lib/load-context.js'; | |
export const load = async (event) => { | |
const { queryClient, fetch } = getLoadContext(event); | |
await queryClient.prefetchQuery( | |
getApiV1UserOptions({ | |
fetch, | |
}), | |
); | |
}; |
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
import { keepPreviousData, QueryClient } from '@tanstack/svelte-query'; | |
import { createLoadContext } from './load-singleton'; | |
import { browser } from '$app/environment'; | |
import { getApiV1UserOptions } from './api/client/@tanstack/svelte-query.gen'; | |
import type { GetApiV1UserResponse } from './api/client'; | |
export const getLoadContext = createLoadContext(({ fetch }) => { | |
const queryClient = new QueryClient({ | |
defaultOptions: { | |
queries: { | |
enabled: browser, | |
refetchOnMount: false, | |
gcTime: 1000 * 60, | |
placeholderData: keepPreviousData, | |
}, | |
}, | |
}); | |
return { | |
queryClient, | |
}; | |
}); |
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
import { browser } from '$app/environment'; | |
import type { LoadEvent } from '@sveltejs/kit'; | |
export function createLoadSingleton<T>( | |
factory: (event: LoadEvent) => T, | |
): (event: LoadEvent) => T { | |
if (browser) { | |
let value: T; | |
return (event: LoadEvent) => { | |
return (value ??= factory(event)); | |
}; | |
} else { | |
const map = new WeakMap<URL, T>(); | |
return (event: LoadEvent) => { | |
if (map.has(event.url)) { | |
return map.get(event.url)!; | |
} | |
const value = factory(event); | |
map.set(event.url, value); | |
return value; | |
}; | |
} | |
} | |
type GenericLoadContext = Record<string, any>; | |
interface GetLoadContext<Context extends GenericLoadContext> { | |
<T extends LoadEvent>(event: T): Omit<Context, keyof T> & T; | |
} | |
export function createLoadContext<Context extends GenericLoadContext>( | |
factory: (event: LoadEvent) => Context, | |
): GetLoadContext<Context> { | |
const getter = createLoadSingleton(factory); | |
return (event) => { | |
return { | |
...getter(event), | |
...event, | |
}; | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment