trpc icon indicating copy to clipboard operation
trpc copied to clipboard

bug: error: NEXT_REDIRECT

Open Dieber opened this issue 1 year ago โ€ข 3 comments

Provide environment information

System: OS: Linux 5.10 Ubuntu 22.04.1 LTS 22.04.1 LTS (Jammy Jellyfish) CPU: (8) x64 Intel(R) Core(TM) i7-9700K CPU @ 3.60GHz Memory: 3.48 GB / 7.69 GB Container: Yes Shell: 5.8.1 - /usr/bin/zsh Binaries: Node: 18.17.1 - ~/.volta/tools/image/node/18.17.1/bin/node npm: 9.6.7 - ~/.volta/tools/image/node/18.17.1/bin/npm pnpm: 8.7.1 - ~/.volta/bin/pnpm bun: 1.1.4 - ~/.bun/bin/bun npmPackages: @tanstack/react-query: ^5.25.0 => 5.32.0 @trpc/client: next => 11.0.0-rc.354+ab11f2310 @trpc/react-query: next => 11.0.0-rc.354+ab11f2310 @trpc/server: next => 11.0.0-rc.354+ab11f2310 next: ^14.2.1 => 14.2.3 react: 18.2.0 => 18.2.0 typescript: ^5.4.2 => 5.4.5

Describe the bug

error: NEXT_REDIRECT I'm making a server call in RSC, but it crashes the app and displays this error, as shown in the picture. Now, how can I set up a redirect when user validation fails in a trpc middleware?

Link to reproduction

https://github.com/Dieber/t3-test-redirect

To reproduce

just run bun dev

Additional information

No response

๐Ÿ‘จโ€๐Ÿ‘งโ€๐Ÿ‘ฆ Contributing

  • [ ] ๐Ÿ™‹โ€โ™‚๏ธ Yes, I'd be down to file a PR fixing this bug!

Dieber avatar Apr 28 '24 04:04 Dieber

As far as i know, there is no easy way for now. But once i've faced with a same issue i found out there is a workaround: I have used next-rsc-error-handler to catch RSC errors and:

import { isNotFoundError } from 'next/dist/client/components/not-found';
import { isRedirectError } from 'next/dist/client/components/redirect';
import { TRPCError } from '@trpc/server';
import { type GlobalServerErrorContext } from 'next-rsc-error-handler';

export default function onGlobalServerError(err: unknown, ctx: GlobalServerErrorContext) {
  if (err instanceof TRPCError) {
    if (isNotFoundError(err.cause) || isRedirectError(err.cause)) {
      throw err.cause;
    }
  }
}

infodusha avatar Jun 04 '24 17:06 infodusha