forge42dev / remix-toast

Server side implementation of toast notifications in Remix
MIT License
173 stars 11 forks source link

After a redirect, the loader data doesn't include any toast data #22

Open chanhman opened 2 months ago

chanhman commented 2 months ago

I am having difficulties with the redirect methods. I am unable to get the data needed to display my toast. However, when I use jsonWithSuccess, I can. If there was a way to also include a redirect URL, I would continue to use it, but I cannot.

I have tested the following within my action function:

 return jsonWithSuccess(
      {
        result: 'Data saved successfully',
      },
      {
        message: 'Created connection!!',
      },
    );

And

    return redirectWithSuccess(
      '/web/dashboard', 
      {
        message: 'Created connection',
      },
    );

With the latter, the redirect works as expected. However, I can't get my toast to work like the jsonWithSuccess implementation. It is my understanding that my root loader data would also include it, but when I use the redirect methods, that is not the case.

root.tsx

const { toast, headers: toastHeaders } = await getToast(request);

  return json(
    {
      themeMode: cookie.themeMode,
      inactivityMins: loginInfo?.inactivityMins,
      csrfToken,
      isXcm,
      toast,
    },
    { headers: toastHeaders },
  );

I was able to reproduce this in the test-app:

  1. Go to src/test-apps/testing-app/app/routes/_index.tsx
  2. Change import { redirectWithError } from "~/toast"; to import { redirectWithError } from "remix-toast";
  3. Click on Click Me

Lastly, I tried implementing ~/toast into my app with no success.

goliney commented 1 week ago

I experience the same problem.

AlemTuzlak commented 3 days ago

I just looked at your repro and if you change the root to use getToast from remix-toast it works as expected. Are you throwing your redirect or returning it? IF you throw the redirect it has to be awaited, eg throw await redirectWithSuccess