AI summary by TwinMind
Please Stop Patching Globals
Theo - t3․gg
54m 3s
Introduction & Context
- Theo reviewing blog post by Artem (creator of Mock Service Worker) about React/Next.js/Bun overriding globals like fetch
- Artem's concerns about these frameworks patching global APIs despite good intentions
- Theo notes appreciation for article's tone - not meant as ammunition against frameworks
Background of Global Patching Issues
- Next.js introduced caching API by extending global fetch with next-specific features (next.revalidate, next.cache)
- React patching global fetch for server-side request caching in React Server Components
- Bun adding proxy options to fetch calls
- Third major framework/runtime introducing features via global API modifications
Appeal of Patching Globals
- Intuitive - fetch is known by developers, perfect place to add caching/proxying
- No imports required since it's global
- Checks boxes for API design: intuitive, concise, efficient
- Why invent custom functions when platform should have this built-in
Harm #1: Maintenance
- Modifying something you don't own is inherently difficult
- Should strive for apparent and explicit experiences via imports
- Globals are precisely that - global, unpredictable usage patterns
- Example: Framework deduping fetch conflicts with third-party SDK's own deduping mechanism
- Artem's experience with Mock Service Worker - moved to service worker when possible to avoid global patching
Harm #2: Predictability
- Theo notes console.log isn't even sacred anymore in Next.js/server components
- Same thing behaving differently across environments is highly irritating
- Cross-browser compatibility has improved significantly, but global patching reverses this progress
- Example: Different fetch behaviors between Chrome/Safari vs Node due to content-length handling
- Standards debates between Deno/Bun teams over fetch implementation details
- Next.js specific syntax (next.cache, next.revalidate) won't work in other frameworks
Harm #3: Learning
- Good API must educate - teaches concepts, not just itself
- Theo criticizes shipping server components without good developer tools
- Missing browser-quality debugging tools on server side
- Developer tooling should have been higher priority before shipping these features
- Mock Service Worker example: Old API was library-specific, new API teaches standard fetch/response concepts
- Next.js caching example doesn't teach standard caching concepts, only Next-specific behavior
Harm #4: Lock-in
- More sugar on standard APIs = harder migration
- Bun proxy example - no spec for proxy behavior, creates vendor-specific implementation
- Contrived APIs disguised as globals drive unpredictability
- Counter-argument: Global overrides can make things more standard by avoiding framework-specific SDK implementations
Harm #5: Hurting Progress
- Historical example: MooTools patching JavaScript prototypes
- Prevented ECMAScript from adding new methods due to compatibility issues
- String.prototype.includes instead of contains due to MooTools conflicts
- "Smoosh gate" - Array.prototype.flatten had to become flat due to MooTools
- Language couldn't become more convenient due to developers' addiction to convenience library
Proposed Alternatives
- Explicit imports instead of global patching
- Bun proxy example:
import { proxy } from 'bun'thenproxy(url, new Request()) - Benefits: explicit, predictable, owned by framework, easy to version/maintain
- Doesn't masquerade as native JS behavior
- Enables learning of standard Request API
- No lock-in by design
API vs Behavior Overrides
- Theo distinguishes between API overrides (adding new properties) vs behavior overrides (changing how existing APIs work)
- API overrides more problematic - harder to debug, educate, port
- Behavior overrides within framework context more acceptable
- Example: Deduping identical requests within single server request makes sense
- Caching between requests more problematic without good dev tools
Current Migration Efforts
- React introducing react.cache for explicit caching
- Next.js moving toward unstable_cache API
- Both moving away from fetch patching toward explicit APIs
- Naming collision issue: react.cache vs unstable_cache serve different purposes
Sebastian Markbåge's Perspective
- React.cache is more than just deduping - it's expandable cache system
- Tracks inputs like Haskell tracks IO
- Patching globals enables broader architectural patterns
- Theo's counter: Average developer needs simpler, clearer naming and better education
Counterarguments Addressed
- "Better DX" - Article explains why it's not actually better DX
- "Bun is custom runtime" - Still deviating from standards, introducing same issues
- "Fetch doesn't exist on server" - Has existed in Node since 2022
- Even smart people make mistakes, important to admit and resolve them
Conclusion
- Frameworks are learning and moving toward explicit APIs
- Need better developer tooling for server-side debugging
- Global patching is a foot gun that's already been fired
- Explicit imports and standard-compliant APIs preferred approach
