kiliman / remix-express-vite-plugin

This package includes a Vite plugin to use in your Remix app. It configures an Express server for both development and production using TypeScript.
131 stars 9 forks source link

No response found for routeId "<route>" #39

Open flp-stephen opened 2 weeks ago

flp-stephen commented 2 weeks ago

I'm bumping in to this error when I throw a redirect from my requireAuth middleware in the case where auth fails during a client navigation.

Here is where the error is thrown: https://github.com/remix-run/remix/blob/b98a53eeb551459062a076f14186951b1cae7fec/packages/remix-react/single-fetch.tsx#L498

I have middleware that is declared like so:

export const middleware = serverOnly$([requireAuth]);

and in the middleware I am throwing a redirect if the user has no tokens.

This works fine for a server render, but on client I get the "no response" error. I could handle this in an error boundary from the client, but I am wondering if I'm missing a piece here?

kiliman commented 2 weeks ago

Thanks for the report. Do you have a simple repro? Or at least a basic idea of how your middleware works? And is this export on the root route?

diegobsit commented 1 week ago

Hello! I'm facing the same issue.

I'm trying to create middleware that redirects my users to / if they are logged in and /sign-in if they aren't.

export async function requireAuth({ request, context, next }: MiddlewareFunctionArgs) {
  const url = new URL(request.url);
  const user = await authenticator.isAuthenticated(request);

  if (user) {
    context.user = user;
  }

  if (!user && url.pathname !== "/sign-in") {
    throw redirect(`/sign-in`);
  } else if (user && url.pathname === "/sign-in") {
    throw redirect("/");
  }

  return next();
}

Everything works as expected if I change the URL in my browser but doing a client navigation breaks. Here is the full error I get:

Error: No response found for routeId "routes/sign-in"
    at unwrapSingleFetchResult (http://localhost:5173/node_modules/.vite/deps/@remix-run_react.js?v=c6436774:2816:11)
    at unwrapSingleFetchResults (http://localhost:5173/node_modules/.vite/deps/@remix-run_react.js?v=c6436774:2791:12)
    at http://localhost:5173/node_modules/.vite/deps/@remix-run_react.js?v=c6436774:2657:16
    at async fetchServerHandlerAndMaybeUnwrap (http://localhost:5173/node_modules/.vite/deps/@remix-run_react.js?v=c6436774:3089:23)
    at async http://localhost:5173/node_modules/.vite/deps/@remix-run_react.js?v=c6436774:2655:20
    at async http://localhost:5173/node_modules/.vite/deps/chunk-FIMYXYCB.js?v=c6436774:2949:19
    at async Promise.all (index 0)
    at async callLoaderOrAction (http://localhost:5173/node_modules/.vite/deps/chunk-FIMYXYCB.js?v=c6436774:2968:23)
    at async Promise.all (index 1)
    at async singleFetchLoaderNavigationStrategy (http://localhost:5173/node_modules/.vite/deps/@remix-run_react.js?v=c6436774:2684:3)

Here is a simple repo to reproduce the issue: Remix Middleware test

I was wondering if I did something wrong or if there is something going wrong with the package, could you help me?

UPDATE: After some more testing it seems that this package works fine with remix versions up to 2.11.2 so I assume some breaking changes were introduced in version 2.12.