ciscoheat/sveltekit-flash-message

Flash not showing when redirecting from +layout.server.ts

erskingardner opened this issue · 5 comments

I'm having trouble getting flash messages to show after a redirect.

  1. I've set up my flash messages and when I manually provide a flash object in +layout.svelte I can see it show up.
  2. However, when I trigger the redirect in +layout.server.ts, which provides a flash message, the redirect happens fine but the flash message doesn't show up on the page.

Any help much appreciated.

+layout.server.ts

import type { LayoutServerLoad } from './$types';
import { loadFlashMessage } from 'sveltekit-flash-message/server';
import { get } from 'svelte/store';
import { currentUser } from '$lib/stores/currentUser';
import { redirect } from 'sveltekit-flash-message/server';

export const load = loadFlashMessage(async (event) => {
    const user = get(currentUser);

    if (!user && event.request.url.match('/new')) {
        const message = { type: 'error', message: 'You need to log in to create lists' } as const;
        throw redirect(303, '/', message, event);
    }
}) satisfies LayoutServerLoad;

Hi, I'm trying to reproduce, but pretty much the exact same code works for me on the test suite. Can you check the cookies in the request and in the browser afterwards, if the flash cookie (called flash) is present after redirect? You may have to circumvent loadFlashMessage to test, since it deletes the cookie after extracting the data from it.

Hopefully this will work in 1.0.0-rc.2, just released.

I just updated and it doesn't seem to be any different. :/ I'll take another look through my code and see if there's something I'm doing wrong her.e

It's still working for me, I'm happy to take a look at a MRE for it, if you cannot figure out what's wrong.

Closing this since it's been about a month of inactivity. Feel free to open it again, or a new issue, if there still is a problem.