Unverified Commit 417f8c0d authored by Jan Kassens's avatar Jan Kassens Committed by GitHub

Defer React loading until fetch dedupe construction (#100401)

## Summary

Avoid initializing React when the router server and fetch helpers are
imported. Load React when constructing the fetch deduplication helper,
and share the fetch-patched symbol through a standalone module so the
router server can access it without loading the fetch patcher.

Removed the lazy module evaluation unit test following review feedback.

## Verification

- Previously validated fetch deduplication and fetch patching unit
tests, plus
`test/e2e/app-dir/app-fetch-deduping/app-fetch-deduping.test.ts` in
development with Turbopack and production with webpack.
- Rebased onto `canary` (`3f51f6e6`); confirmed the implementation
patches are unchanged. Tests were not rerun for the test deletion.

<!-- NEXT_JS_LLM -->
parent 331a4692
/** /**
* Based on https://github.com/facebook/react/blob/d4e78c42a94be027b4dc7ed2659a5fddfbf9bd4e/packages/react/src/ReactFetch.js * Based on https://github.com/facebook/react/blob/d4e78c42a94be027b4dc7ed2659a5fddfbf9bd4e/packages/react/src/ReactFetch.js
*/ */
import * as React from 'react'
import { cloneResponse } from './clone-response' import { cloneResponse } from './clone-response'
import { InvariantError } from '../../shared/lib/invariant-error' import { InvariantError } from '../../shared/lib/invariant-error'
...@@ -42,6 +41,9 @@ type CacheEntry = [ ...@@ -42,6 +41,9 @@ type CacheEntry = [
] ]
export function createDedupeFetch(originalFetch: typeof fetch) { export function createDedupeFetch(originalFetch: typeof fetch) {
// Importing the helper alone must not load the app-page runtime through
// the App Router's vendored React. Keep the same React instance when used.
const React = require('react') as typeof import('react')
const getCacheEntries = React.cache( const getCacheEntries = React.cache(
// eslint-disable-next-line @typescript-eslint/no-unused-vars -- url is the cache key // eslint-disable-next-line @typescript-eslint/no-unused-vars -- url is the cache key
(url: string): CacheEntry[] => [] (url: string): CacheEntry[] => []
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment