Fix: Optimistic routing bugs leading to repeated prefetch loops (#97128)
Fixes a few bugs related to optimistic routing. Originally reported as a next-intl request waterfall in MarkBekooy/prefetching-request-waterfall-bug#1, then extracted into an isolated regression test: A proxy that rewrites every URL to inject a leading path segment (`/one/two` → `/en/one/two`, i.e. i18n with the default locale hidden from the URL) plus a fully dynamic target route like `/[locale]/[...pages]` leads to an infinite prefetch loop that never resolves. This regression uncovered several oversights in the optimistic routing implementation: when receiving a prefetch response, we did not check whether the response matched the expected result. If the tree mismatched, in some cases the prefetch task would fall into a loop, repeatedly attempting to fulfill the missing data. Now when this happens, we record on the local route definition that a dynamic rewrite occurred, disabling further attempts to optimistically resolve the route. This is the same strategy we were already using for normal navigation responses, now applied to the prefetch path. We also received another bug report with a similar root cause: parallel routes with conflicting dynamic params at the same level (`@modal/[...catchAll]` next to `[username]`) cannot be distinguished using the current traversal algorithm, because it assumes that each segment is resolvable independently without inspecting the children or sibling branches. To work around this issue for now, when this scenario is detected, we disable optimistic routing for the conflicting route. We can model this properly in a future PR by having the server send down the "sibling" dynamic route segments, similar to what we do for static siblings already. Fixes #97135 <!-- NEXT_JS_LLM -->
Showing
Please
register
or
sign in
to comment