Next.js
next/redirect-outside-try
Call redirect(), notFound() and their kin outside try blocks; they work by throwing, and a catch swallows them.
poważnastatycznefe-nextjs
Dlaczego
redirect(), permanentRedirect(), notFound(), forbidden() and unauthorized() throw a special error that Next.js catches to change the response. Inside a try whose catch handles every error, that signal is caught by your code instead: the user stays on the page, sees a generic error message, or the action reports a failure after it succeeded.
Poprawka
Do the risky work inside try, keep the result in a variable, and call redirect() after the try/catch. If the call must stay inside, call unstable_rethrow(error) first in the catch, or rethrow what you do not handle.
Przykłady
// bad
try { await createPost(data); redirect('/posts') } catch (error) { return { message: 'Could not save' } }
// good
try { await createPost(data) } catch (error) { return { message: 'Could not save' } }
revalidatePath('/posts')
redirect('/posts')Źródła
Fakty
- Identyfikator
next/redirect-outside-try- Waga
- poważna
- Sprawdzanie
- statyczne sprawdzane w kodzie źródłowym przez
audit - Dotyczy plików
jsx- Tylko w projektach z
next- Uczy jej skill
fe-nextjsNext.js App Router- Od wersji
- 0.1.0
Przeczytaj w terminalu
npx github:dawidolko/frontend-skills explain next/redirect-outside-try