ciscoheat / sveltekit-flash-message

Send temporary data after redirect, usually from endpoints. Works with both SSR and client.
https://www.npmjs.com/package/sveltekit-flash-message
MIT License
246 stars 5 forks source link

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

Closed erskingardner closed 11 months ago

erskingardner commented 1 year ago

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;
ciscoheat commented 1 year ago

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.

ciscoheat commented 1 year ago

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

erskingardner commented 1 year ago

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

ciscoheat commented 1 year ago

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.

ciscoheat commented 11 months ago

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.