Skip to content

Conversation

@Stadly
Copy link

@Stadly Stadly commented Dec 3, 2025

takeFlash can be used to prevent the flash message store from being updated with the flash message. This can be useful for applying custom handling to the flash message in progressively enhanced forms:

<script lang="ts">
  import { toast } from "svelte-sonner";
  import { takeFlash } from 'sveltekit-flash-message';
  import { superForm } from "sveltekit-superforms";

  let toastId: string | number | null = null;
  const { form, enhance } = superForm(
    { test: "" },
    {
      onSubmit: () => {
        toastId = toast.loading("Pending...");
      },
      onResult: async ({ result }) => {
        if (toastId !== null) {
          // Prevent the flash message store in `src/routes/+layout.svelte` from being updated.
          // Instead, we manually take the flash message here and update the already pending toast.
          const flash = await takeFlash();
          if (!flash) return;
          if (result.type === "error" || result.type === "failure") {
            toast.error(flash.message, {
              id: toastId,
              action: { label: "Retry", onClick: () => { /* Retry logic here */ }, },
            });
          } else {
            toast.success(flash.message, { id: toastId });
          }
        }
      },
    },
  );
</script>

<form method="POST" action="/test" use:enhance>
  <input type="text" name="test" bind:value={$form.test} />
  <button>Submit</button>
</form>

@Stadly
Copy link
Author

Stadly commented Dec 6, 2025

On second thought, I think this is probably better done in userland by having more logic around the flash store subscription in +layout.svelte. Feel free to reopen if you disagrees and think this PR is worth merging.

@Stadly Stadly closed this Dec 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant