vercel / next.js

The React Framework
https://nextjs.org
MIT License
122.91k stars 26.28k forks source link

fix prefetch bailout detection for nested loading segments #67358

Closed ztanner closed 4 days ago

ztanner commented 4 days ago

What

When PPR is off, app router prefetches will render the component tree up until it encounters a loading segment, at which point it'll just return some metadata about the segment and won't do any further rendering. This is an optimization to ensure prefetches are lightweight and don't potentially invoke expensive dynamic subtrees. However, there's a bug in this logic that is causing it to bail unexpectedly even if a segment deeper in the tree contained a loading.js file.

This would mean the loading state wouldn't be triggered until the second request for the full RSC data is initiated, resulting in an unintended delta between when a link is clicked and the loading state is shown.

Why

The shouldSkipComponentTree flag was incorrectly being set to true even if the loading.js segment appeared deeper in the tree. Prefetch requests from the client will always contain FlightRouterState, so the logic to check for loading deeper in the tree will always be missed.

How

This removes the flightRouterState check as it doesn't make sense: prefetches will currently always include the flightRouterState, causing this to always short-circuit. I believe that this check is vestigial from when we were generating static prefetch.rsc outputs which is no longer the case.

This mirrors a similar check when determining if parallel route(s) should be rendered.

ztanner commented 4 days ago

This stack of pull requests is managed by Graphite. Learn more about stacking.

Join @ztanner and the rest of your teammates on Graphite Graphite

ijjk commented 4 days ago

Tests Passed