Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docs: Client side redirect() usage incompatible with error boundaries. #62458

Open
staticshock opened this issue Feb 23, 2024 · 4 comments
Open
Labels
Documentation Related to Next.js' official documentation.

Comments

@staticshock
Copy link

staticshock commented Feb 23, 2024

What is the improvement or update you wish to see?

Right now the docs for the redirect() function say this:

redirect can be called in Client Components during the rendering process but not in event handlers.

Is there any context that might help us understand?

Since internally it's implemented as an exception, it's caught by error boundaries. I think that's worth calling out, because it almost completely negates the utility of ever using redirect() client-side.

I ran into this when I combined Sentry's ErrorBoundary with Next.js's redirect().

Does the docs page already exist? Please link to it.

https://nextjs.org/docs/app/building-your-application/routing/redirecting#redirect-function

@staticshock staticshock added the Documentation Related to Next.js' official documentation. label Feb 23, 2024
@staticshock
Copy link
Author

staticshock commented Feb 23, 2024

Actually, after some digging I see that Next.js internally uses an error boundary that is redirect-aware, but not in a way that I can re-use in my repo. Have you considered exporting RedirectBoundary so that devs can use it directly?

That would allow me to make an in-house error boundary in which the redirect always comes first:

export const MyErrorBoundary = ({
  children,
  ...props
}: SentryErrorBoundaryProps => (
  <SentryErrorBoundary {...props}>
    <NextRedirectBoundary>
      {children}
    </NextRedirectBoundary>
  </SentryErrorBoundary>
)

@AhmedBaset
Copy link
Contributor

Have you considered exporting RedirectBoundary so that devs can use it directly?

Everything in next.js is importable. It's might not typed but it's still exported.

"files": [
"dist",

Have you tried to import it?

@staticshock
Copy link
Author

staticshock commented Feb 23, 2024

Oh, good call! This works:

import { RedirectBoundary } from 'next/dist/client/components/redirect-boundary'

@staticshock
Copy link
Author

Ok, I've had a couple of days to play with it and I've come to the conclusion that even with the boundaries in place (as I've suggested above) this is still pretty error prone and should not be recommended as a pattern. I think the docs should discourage it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Documentation Related to Next.js' official documentation.
Projects
None yet
Development

No branches or pull requests

2 participants