Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(next): reduce initReq calls from 3 to 1 per page load #11312

Open
wants to merge 11 commits into
base: main
Choose a base branch
from

Conversation

AlessioGr
Copy link
Member

@AlessioGr AlessioGr commented Feb 21, 2025

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 the overrides object never maintained the same reference, causing the cache to miss.

What’s Changed

  • New getInitReqContainer Helper
    We 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 Functions
    The 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

    • Previously, a lot of logic was run in both initReq and the respective page / layout. This was completely unnecessary, as initReq was already running that logic. This PR returns the calculated variables from initReq, so they don't have to be duplicatively calculated again.

Performance Gains

  • Previously:
    • .auth call ran 3 times
    • Access control ran 3 times
  • Now:
    • .auth call runs 1 time
    • Access control runs 2 times

This change yields a noticeable performance improvement by cutting down on redundant work.

@AlessioGr AlessioGr marked this pull request as draft February 21, 2025 00:36
@AlessioGr AlessioGr changed the title perf(next): ensure initReq is called 3x less perf(next): reduce initReq calls from 3 to 1 per page load Feb 21, 2025
@AlessioGr AlessioGr marked this pull request as ready for review February 21, 2025 05:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants