Skip to content

perf: Enable React Compiler in Next.js - #14510

Draft
amoore108 wants to merge 27 commits into
latestfrom
agents/enable-react-compiler-nextjs
Draft

amoore108 wants to merge 27 commits into
latestfrom
agents/enable-react-compiler-nextjs

Conversation

@amoore108

@amoore108 amoore108 commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Enable React Compiler in the Next.js app so eligible components and hooks can reuse previous work instead of repeating it on every render.

This should be a very passive change, not requiring additional effort to write components. The idea is that the compiler will help 'auto-memoise' components, so having to use the likes of useMemo and useCallback shouldn't be needed as the compiler will handle that for you.

You can read more about the React Compiler here.

You can opt-out of the React Compiler using the use no memo directive at the top of components and hooks: React Compiler escape hatch but this should be used sparingly, if at all.

Similarly, you can add 'overrides' to the eslintrc file to ignore or change the level of error for compiler rules. This too should be used very sparingly.

If code breaks one of the compiler's rules, ESLint reports it as an error from the react-hooks plugin. For example, calling setState synchronously inside an effect produces:

src/app/hooks/useActionTooltip/index.ts
  24:23  error  Error: Calling setState synchronously within an effect can trigger cascading renders

Effects are intended to synchronize state between React and external systems such as manually updating the DOM, state management libraries, or other platform APIs. In general, the body of an effect should do one or both of the following:
* Update external systems with the latest state from React.
* Subscribe for updates from some external system, calling setState in a callback function when external state changes.

Calling setState synchronously within an effect body causes cascading renders that can hurt performance, and is not recommended. (https://react.dev/learn/you-might-not-need-an-effect).

  22 |
  23 |   useEffect(() => {
> 24 |     if (actionResult) setIsTooltipVisible(true);
     |                       ^^^^^^^^^^^^^^^^^^^ Avoid calling setState() directly within an effect
  25 |   }, [actionResult]);
  26 |  react-hooks/set-state-in-effect

The fix is usually to derive the value during render, or to move the work into an event handler, rather than to disable the rule.

Code changes

  • Enable the supported top-level reactCompiler: true Next.js option.
  • Add babel-plugin-react-compiler dependency. Required to allow the compiler to work.
  • Update various components to 'satisfy' the React Compiler and adds tests around the new behaviour to ensure they behave as expected under the new compiler

Useful Links

Enable automatic client-side memoization across all services using the supported Next.js integration. Preserve the existing Emotion, SSR and AMP/Lite pipelines.

Keep configuration, the pinned dependency and tracked cache, regression tests, and documentation together so the rollout can be reverted atomically.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@amoore108
amoore108 requested a review from a team as a code owner October 19, 2026 10:04
Copilot AI balanced review requested due to automatic review settings October 19, 2026 10:04
@amoore108 amoore108 self-assigned this Oct 19, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The configuration, dependencies, tests, and documentation are consistent, with no unresolved findings.

No open issues 🎉

What changed in this PR

Enables React Compiler across the Next.js app, including shared World Service components, while preserving existing compilation settings.

Changes:

  • Enables React Compiler and pins its dependency to 1.0.0.
  • Adds configuration regression tests.
  • Documents memoization, scoped opt-outs, and unchanged rendering/test pipelines.
File Description
yarn.lock Locks the compiler dependency and Babel resolution.
ws-nextjs-app/​README.md Documents compiler behavior and opt-outs.
ws-nextjs-app/​package.json Pins the compiler plugin.
ws-nextjs-app/​next.config.test.ts Tests compiler enablement and preserved settings.
ws-nextjs-app/​next.config.js Enables React Compiler.

Review effort: Balanced

amoore108 and others added 2 commits October 19, 2026 11:09
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@amoore108
amoore108 marked this pull request as draft October 19, 2026 10:12
amoore108 and others added 14 commits October 19, 2026 11:42
Keep curation links and Optimizely provider instances stable across renders. Cover focus, context updates and isolated mounts, then remove the static-components lint overrides.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Depend directly on the extracted experiment props so React Compiler can preserve context memoization. Cover stable references and analytics updates, and remove the manual-memoization lint override.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Keep polling callbacks and parameters fresh without mutating refs during render, and ignore in-flight responses after effect cleanup. Cover updates, cancellation and timing, and remove the resolved polling refs override.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Derive redundant UI state directly, subscribe to browser state with hydration-safe snapshots, and track scroll milestones without extra renders. Cover browser changes, image cancellation, PWA prompt cleanup and time-machine lifecycle.

Remove ten resolved set-state-in-effect overrides; retain the blocked media and webcore entries pending their styling migrations.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Keep Promo and radio-schedule fixture clocks stable outside render, preserving relative-time coverage and removing the purity lint overrides.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Allow error-summary tests to render more than twice without falling back to an empty context, and restore spies between tests. Cover the hydration-safe Android hook without relying on internal render counts.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Pass experiment variants into pure ATI enrichment functions and own experiment Hooks in stable React component boundaries. Preserve eligible page types and reset experiment state safely when eligibility changes.

Cover page transitions and real Hook state. Verified production hydration on seven routes and client-side navigation between home, article and non-experiment pages.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Derive translated download labels without mutating input links. Preserve existing JSX and Emotion styles, with frozen-input and service-translation regressions.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Remove shared mutable render variables from related-content, top-stories and latest-media lists without changing markup or styles.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Reuse hydration detection instead of synchronously copying localized dates and times into state from effects. Preserve existing component types, markup and styles; cover hydration and fixture/service updates.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Keep render-time refs immutable while retaining fresh fullscreen and swipe callbacks without reloading players. Reuse hydration detection for the fullscreen portal, preserving all existing component formats and styling.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Read dropdown DOM measurements in the layout lifecycle instead of during render, preserving existing JSX and Emotion styling. Remove the final compiler warning override after validating menu height and open/close behavior.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Live display-mode detection can discard a saved install prompt when a user enters video fullscreen.

1 open issue

Review effort: Balanced

Comment thread src/app/hooks/useIsPWA/index.tsx Resolved
Exclude Fullscreen API elements from fullscreen PWA detection while retaining fullscreen installed-app support. Cover video fullscreen enter/exit and reuse of the saved install prompt without another beforeinstallprompt event.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

PWA fullscreen detection and pending install-event cleanup have unresolved behavioral regressions.

1 open issue
1 previously missed issue

In code that hasn't changed since last review

Medium severity Deferred prompt toggle leaves consumed install event reusable

src/​app/​hooks/​usePWAInstallPrompt/​index.ts:76

If deferPrompt changes while prompt() or userChoice is pending, effect cleanup deactivates the captured session but leaves installPrompt in state. Once the choice settles, the inactive-session guards prevent clearing that consumed event. Re-enabling deferral then reports it as installable and permits another prompt() call on the used event. Keep callback cancellation separate from event cleanup: finish consuming the choice and clear only the matching event, so a newer event is not discarded. Add a regression test that toggles deferPrompt during the pending operation.

1 resolved since last review

Review effort: Balanced

Comment thread src/app/hooks/useIsPWA/index.tsx Resolved
Keep a stable per-hook snapshot reader that retains the established app mode while native video fullscreen temporarily replaces display-mode. Continue observing real display-mode changes outside fullscreen, with regression coverage for all supported app modes and rerenders.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Compiler-wide activation and broad lifecycle refactors require cross-service build and runtime validation before human approval.

No open issues 🎉

1 previously missed issue

In code that hasn't changed since last review

Medium severity Return immutable location snapshots to trigger navigation-dependent effects

src/​app/​hooks/​useLocation/​index.tsx:18

The href snapshot changes on navigation, but this hook still returns the same mutable window.location object. Consumers that depend on its identity, such as the effect in components/Ad/Canonical/index.tsx:46–64, therefore do not rerun; compiler memoization can also reuse calculations based on that unchanged identity. Return a read-only location snapshot cached per href, and test a consumer with [location] dependencies across navigation. Update the existing tests that expect previously returned values to mutate after pushState.

1 resolved since last review

Review effort: Balanced

Preserve the published commit history while integrating upstream changes. Retain the validated conflict resolutions and Jest mock compatibility fixes.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@amoore108
amoore108 marked this pull request as ready for review October 21, 2026 14:54
Comment thread .eslintrc.js
Comment on lines +147 to +152
{
files: ['src/app/components/Navigation/index.canonical.tsx'],
rules: {
'react-hooks/set-state-in-effect': 'warn',
},
},

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This override is due to the sticky nav we're currently testing. It has a lot of state management which works for test purposes, but will need refactored to satisfy the compiler. Don't want to do that here in this piece of work as its code thats still under development.

@amoore108
amoore108 marked this pull request as draft October 22, 2026 10:16
…-compiler-nextjs

# Conflicts:
#	src/app/components/Curation/Subhead/index.test.tsx
#	src/app/components/Curation/Subhead/index.tsx
#	src/app/components/SaveArticleButton/SaveArticleButtonAuthenticated/index.tsx
#	src/app/contexts/ReverbParamsContext/getEnrichedHomePageATIData/index.ts
#	src/app/legacy/containers/RadioSchedule/Canonical/index.test.jsx
#	src/app/legacy/containers/RadioSchedule/utilities/testHelpers.jsx
#	ws-nextjs-app/package.json
#	yarn.lock
Resetting moment.now during render mutates a module-level value, which the
React Compiler's react-hooks/immutability rule rejects. Restore it in an
effect cleanup instead so the fixed story time does not leak into other
stories.
@amoore108 amoore108 changed the title Enable React Compiler in Next.js perf: Enable React Compiler in Next.js Nov 28, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants