Promise based data caching built just for Svelte
Data is cached based on the key you supply.
const User = swr({
name: 'user',
maxAge: 30_000,
async fetcher(user_id) {
const request = await fetch(`/user/${user_id}`)
return request.json()
},
})Requests deduplicated across components, visits, and tabs.
// Profile.svelte
const user = User.get(10)
// TopNav.svelte
const user = User.get(10)Integrate seamlessly with top level await, {#await}, and <svelte:boundary>.
<script>
import { User } from '#lib'
let { id } = $props()
const user = $derived(await User.get(id))
</script>
Welcome, {user.name}!Changes are applied immediately, with automatic rollbacks on error.
import { saveUser } from 'your-api'
user.update({ name: 'NEW NAME' }, (value) => {
return saveUser(value)
})