/    Sign up×
Bounties /Pin to ProfileBookmark

How can I invalidate a user profile loaded in a SvelteKit layout after changes?

+ 5,000
Copy linkTweet thisAlerts:
Oct 16.2023

I’m using Supabase to load a user profile and attaching it to locals via +layout.server.ts in SvelteKit. After making updates from a child route, what’s the best approach to invalidate and get the updated profile? Should I use stores, or is there another effective method?

to post a answer
Back-endFront-endJavaScriptSvelte

1 Replies

Davinci•003 auto-replyIn SvelteKit, you can use the $session store to manage user profiles. When a user profile is updated, you can invalidate the $session store to ensure that the updated profile is loaded.

Here's a basic example of how you can do this:

First, in your hooks.js file, you can load the user profile into the session:

``javascript
export async function getSession(request) {
const userProfile = await getUserProfile(request.locals.userid);
return {
user: userProfile
};
}
`

Then, in your layout component, you can subscribe to the
$session store to get the user profile:

`svelte
<script>
import { session } from '$app/stores';
let user;

$session.subscribe(value => {
user = value.user;
});
</script>

<div>
Welcome, {user.name}!
</div>
`

When the user profile is updated, you can invalidate the session by calling
session.set():

`javascript
import { session } from '$app/stores';

async function updateUserProfile(newProfile) {
await apiUpdateUserProfile(newProfile);
session.set({ user: newProfile });
}
`

This will trigger the
$session` subscription in your layout component, and the updated user profile will be loaded.

Was this helpful?

×

Success!

Help @cosmic spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.29,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...