How to send toast from server-side? #1130
Unanswered
svelterust
asked this question in
Help
Replies: 1 comment
-
Hello, @knarkzel A good bet would be Andreas' package, https://github.com/ciscoheat/sveltekit-flash-message It's well documented, but here are snippets for reference: redirect(
303,
'/auth/login',
[
{
type: 'success',
title: 'Registered successfully.',
message:
'Please check your email for a verification link. If you do not receive an email, please check your spam folder.'
}
],
event
); const flash = getFlash(page);
onMount(() => {
const un_sub = flash.subscribe(($flash) => {
if (!$flash) return;
// since $flash is an array of flash messages, render each one of them after the other
// but set an interval to avoid rendering them all at once
$flash.forEach((f, i) => {
const interval = setInterval(() => {
toast[f.type](f.title, { description: f.message });
clearInterval(interval);
}, i * 500);
});
// Clearing the flash message could sometimes
// be required here to avoid double-toasting.
flash.set(undefined);
});
// Stores should be unsubscribed from when no longer needed.
// This is to avoid memory leaks.
return () => {
un_sub();
};
}); My use case seems somewhat complicated/extra, but the gist is really simple. Cheers. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Let's say an error occurs in
+page.server.ts
. How can we redirect the user and show a toast at the same time?Beta Was this translation helpful? Give feedback.
All reactions