AI summary by TwinMind
New York Times Sees 30% Perf Win By Switching To React...18?
Theo - t3․gg
26m 11s
React 18 Background & Benefits
- React 18 introduces concurrent mode for simultaneous rendering of updates and user interaction
- Automatic batching optimizes performance by grouping state updates within single render cycle
- Breaking up tasks in main thread vs synchronous execution in prior versions
- New useTransition hook allows certain state updates without blocking UI
- Batching example: multiple setState calls in same function trigger single re-render vs multiple renders in React 17
New York Times Migration Context
- NYT places high value on page performance, SEO, and latest technology
- SEO critical for news organizations - lose entirely if not first results in Google searches
- Interaction to Next Paint (INP) scores vital - newest core web vital metric Google uses for search ranking
- React 18 promised significant INP improvements making upgrade high priority, high stakes initiative
- News sites are biggest SEO hackers, push everything quickly for maximum indexing potential
Migration Process Step 1: Removing Deprecated Dependencies
- Biggest time commitment: removing Enzyme testing library
- Enzyme used in hundreds of test files across repository
- Required significant manual effort, dozens of pull requests over several months
- Migrated to Testing Library (created by Kent Dodds)
- Incremental PRs to accommodate other product work and avoid developer fatigue
Migration Process Step 2: Foundation Setting
- Upgraded major dependencies: types, react, react-test-renderer, react-dom, testing-library
- Refactored tests and type definitions to conform to latest versions
- Relatively easy upgrade to React 18 itself - minimal notes about difficulty
Migration Process Step 3: Enabling New Features
- Utilized new APIs: createRoot and hydrateRoot
- Multiple web servers with shared UI components requiring React 18 functionality
- Initial expectation: simple change from react.hydrate to hydrateRoot
- Reality: enabling new features caused significant challenges
Unexpected Challenges: Embedded Interactives
- Problem with highly trafficked content: embedded interactives
- Custom embedded interactives render server-side using dangerouslySetInnerHTML
- Independent HTML, links, scripts running separately from React tree
- Allows editors/journalists to inject self-contained visual/interactive elements
- Key to most impactful reporting but poses real dev challenges
Hydration Mismatch Issues
- React 18 stricter hydration mismatch requirements vs React 17
- React 17: left DOM in invalid state during mismatches
- React 18: discards server-rendered content, falls back to client-side rendering
- Problem: script tags added via dangerouslySetInnerHTML don't run due to browser security
- Result: embedded interactives render as if JavaScript never executed
Script Tag Execution Challenge
- Browser security: script tags added after page load don't automatically run
- Script tags only run when manually appended/replaced as child nodes
- Solution: extract script tags from interactive HTML, append back in correct locations
Complex Script Extraction Solution
- Function replaces script tags with empty placeholders for later replacement
- Returns object with scripts to run on client and scriptless HTML
- Couldn't rerun scripts due to global variable redeclaration issues
- Some scripts clear variables globally vs function closure
Script Load Ordering Requirements
- Interactive scripts must load in correct order when appended back
- Prioritization queue for script tag reappending:
- Non-functional manifest scripts with static data
- Scripts with source attribute and async set
- Scripts with vanilla JavaScript in innerHTML
- Prevents scripts from referencing each other before proper loading
Performance Results
- Almost immediate performance improvement post-deployment
- INP scores (p75 range) dropped ~30%
- Initial Next Paint: ~500ms → under 350ms
- Render times cut essentially in half
- Direct result of React 18's automatic batching and concurrency features
Validation & Rollout
- 40,000 custom embedded interactives impossible to QA extensively
- Relied on reusable templates graphics team uses
- Validated specific behavior in Svelte and Adobe Illustrator-based interactives
- Successful rollout to other sites after core news site success
- INP scores moved out of poor zone before Google's March deadline
- No negative SEO results when became part of search algorithm
Future Plans
- Exploring new features: startTransition, server components
- Primary goal: continuously bring INP scores down, improve functionality
- Commitment to stable, reliable performance of current React version
- Newsroom continues producing powerful interactives without rendering framework concerns
