perf(next): reduce initReq calls from 3 to 1 per page load #11312
+241
−91
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.
This PR significantly improves performance when navigating through the admin panel by reducing the number of times
initReq
is called. Previously,initReq
—which handles expensive tasks like initializing Payload and running access control—was called three times for a single page load (for the root layout, the root page, and the notFound page).We initially tried to use React Cache to ensure
initReq
only ran once per request. However, because React Cache performs a shallow object reference check on function arguments, the configuration object we passed (configPromise
) and theoverrides
object never maintained the same reference, causing the cache to miss.What’s Changed
New
getInitReqContainer
HelperWe introduced a helper that provides a stable object reference throughout the entire request. This allows React to properly cache the output, ensuring
initReq
doesn’t get triggered multiple times by mistake.Splitting
initReq
into Two FunctionsThe
initReq
logic was split into:initPartialReq
: Runs only once per request, handling tasks that do not depend on page-level data (e.g., calling.auth
, which performs a DB request).initReq
: Runs twice (once for Layout+NotFound page and once for main page), handling tasks, most notably access control, that rely on page-level data such as locale or query parameters. The NotFound page will share the same req as the layout page, as it's not localized, and its access control wouldn't need to access page query / url / locale, just like the layout.Remove duplicative logic
initReq
and the respective page / layout. This was completely unnecessary, asinitReq
was already running that logic. This PR returns the calculated variables frominitReq
, so they don't have to be duplicatively calculated again.Performance Gains
.auth
call ran 3 times.auth
call runs 1 timeThis change yields a noticeable performance improvement by cutting down on redundant work.