fix: throw navigation errors immediately to support Next.js cacheComp… #394
+443
−6
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
When using
useActionoruseOptimisticActionwith server actions that callredirect(), the redirect fails when Next.jscacheComponentsoption is enabled.Proposed changes
Problem
When using
useActionoruseOptimisticActionhooks with server actions that callredirect()from Next.js, the redirect fails to execute when the Next.js cacheComponents feature is enabled.Root Cause
The library was catching navigation errors (from
redirect(),notFound(), etc.) and storing them in React state, then re-throwing them later in auseLayoutEffecthook. This deferred error handling created a timing issue:redirect(), throwing a navigation erroruseLayoutEffecttries to re-throw the errorWith cacheComponents enabled, Next.js caches and restores component trees during React transitions. The component state could be restored from cache before the
useLayoutEffectruns, causing the redirect to be cancelled or ignored.Solution
Modified the hooks to throw navigation errors immediately (synchronously) when caught in the promise chain, instead of deferring them to
useLayoutEffect.What Changed:
setNavigationError(e); return;tosetNavigationError(e); throw e;Related issue(s) or discussion(s)
re #393