Unverified Commit 6d4f2631 authored by Shu Ding's avatar Shu Ding Committed by GitHub

Improved bundling strategy for the server graph (#40739)

This PR changes the external module resolution to eagerly bundle
node_modules, and some specific Next.js internal modules, if on the
`WEBPACK_LAYERS.server` layer. While resolving corresponding packages,
we use the `react-server` export condition (fallbacks to default).

A follow-up PR will be adding a Next.js option to opt-out specific
packages from being bundled on the server layer.

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have a helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the
feature request has been accepted for implementation before opening a
PR.
- [ ] Related issues linked using `fixes #number`
- [x] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have a helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing
doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
parent c5f1e2f7
......@@ -395,8 +395,14 @@ pub fn server_components<C: Comments>(
JsWord::from("client-only"),
JsWord::from("react-dom/client"),
JsWord::from("react-dom/server"),
// TODO-APP: JsWord::from("next/router"),
// TODO-APP: Rule out client hooks.
],
invalid_client_imports: vec![
JsWord::from("server-only"),
// TODO-APP: Rule out server hooks such as `useCookies`, `useHeaders`,
// `usePreviewData`.
],
invalid_client_imports: vec![JsWord::from("server-only")],
invalid_server_react_dom_apis: vec![
JsWord::from("findDOMNode"),
JsWord::from("flushSync"),
......
......@@ -831,6 +831,14 @@ export default async function getBaseWebpackConfig(
[COMPILER_NAMES.edgeServer]: ['browser', 'module', 'main'],
}
const reactAliases = {
react: reactDir,
'react-dom$': reactDomDir,
'react-dom/server$': `${reactDomDir}/server`,
'react-dom/server.browser$': `${reactDomDir}/server.browser`,
'react-dom/client$': `${reactDomDir}/client`,
}
const resolveConfig = {
// Disable .mjs for node_modules bundling
extensions: isNodeServer
......@@ -843,11 +851,8 @@ export default async function getBaseWebpackConfig(
alias: {
next: NEXT_PROJECT_ROOT,
react: `${reactDir}`,
'react-dom$': `${reactDomDir}`,
'react-dom/server$': `${reactDomDir}/server`,
'react-dom/server.browser$': `${reactDomDir}/server.browser`,
'react-dom/client$': `${reactDomDir}/client`,
...reactAliases,
'styled-jsx/style$': require.resolve(`styled-jsx/style`),
'styled-jsx$': require.resolve(`styled-jsx`),
......@@ -977,6 +982,7 @@ export default async function getBaseWebpackConfig(
context: string,
request: string,
dependencyType: string,
layer: string | null,
getResolve: (
options: any
) => (
......@@ -1001,14 +1007,47 @@ export default async function getBaseWebpackConfig(
return `commonjs next/dist/lib/import-next-warning`
}
const resolveWithReactServerCondition =
layer === WEBPACK_LAYERS.server
? getResolve({
// If React is aliased to another channel during Next.js' local development,
// we need to provide that alias to webpack's resolver.
alias: process.env.__NEXT_REACT_CHANNEL
? {
...reactAliases,
'react/package.json': `${reactDir}/package.json`,
'react/jsx-runtime': `${reactDir}/jsx-runtime`,
'react/jsx-dev-runtime': `${reactDir}/jsx-dev-runtime`,
'react-dom/package.json': `${reactDomDir}/package.json`,
}
: false,
conditionNames: ['react-server'],
})
: null
// Special internal modules that must be bundled for Server Components.
if (layer === WEBPACK_LAYERS.server) {
if (!isLocal && /^react(?:$|\/)/.test(request)) {
const [resolved] = await resolveWithReactServerCondition!(
context,
request
)
return resolved
}
if (
request ===
'next/dist/compiled/react-server-dom-webpack/writer.browser.server'
) {
return
}
}
// Relative requires don't need custom resolution, because they
// are relative to requests we've already resolved here.
// Absolute requires (require('/foo')) are extremely uncommon, but
// also have no need for customization as they're already resolved.
if (!isLocal) {
// styled-jsx is also marked as externals here to avoid being
// bundled in client components for RSC.
if (/^(?:next$|styled-jsx$|react(?:$|\/))/.test(request)) {
if (/^(?:next$|react(?:$|\/))/.test(request)) {
return `commonjs ${request}`
}
......@@ -1122,9 +1161,22 @@ export default async function getBaseWebpackConfig(
return
}
// Anything else that is standard JavaScript within `node_modules`
// can be externalized.
if (/node_modules[/\\].*\.[mc]?js$/.test(res)) {
if (layer === WEBPACK_LAYERS.server) {
try {
const [resolved] = await resolveWithReactServerCondition!(
context,
request
)
return resolved
} catch (err) {
// The `react-server` condition is not matched, fallback.
return
}
}
// Anything else that is standard JavaScript within `node_modules`
// can be externalized.
return `${externalType} ${request}`
}
......@@ -1176,11 +1228,17 @@ export default async function getBaseWebpackConfig(
context,
request,
dependencyType,
contextInfo,
getResolve,
}: {
context: string
request: string
dependencyType: string
contextInfo: {
issuer: string
issuerLayer: string | null
compiler: string
}
getResolve: (
options: any
) => (
......@@ -1193,24 +1251,31 @@ export default async function getBaseWebpackConfig(
) => void
) => void
}) =>
handleExternals(context, request, dependencyType, (options) => {
const resolveFunction = getResolve(options)
return (resolveContext: string, requestToResolve: string) =>
new Promise((resolve, reject) => {
resolveFunction(
resolveContext,
requestToResolve,
(err, result, resolveData) => {
if (err) return reject(err)
if (!result) return resolve([null, false])
const isEsm = /\.js$/i.test(result)
? resolveData?.descriptionFileData?.type === 'module'
: /\.mjs$/i.test(result)
resolve([result, isEsm])
}
)
})
}),
handleExternals(
context,
request,
dependencyType,
contextInfo.issuerLayer,
(options) => {
const resolveFunction = getResolve(options)
return (resolveContext: string, requestToResolve: string) =>
new Promise((resolve, reject) => {
resolveFunction(
resolveContext,
requestToResolve,
(err, result, resolveData) => {
if (err) return reject(err)
if (!result) return resolve([null, false])
const isEsm = /\.js$/i.test(result)
? resolveData?.descriptionFileData?.type ===
'module'
: /\.mjs$/i.test(result)
resolve([result, isEsm])
}
)
})
}
),
]
: [
// When the 'serverless' target is used all node_modules will be compiled into the output bundles
......
......@@ -191,6 +191,9 @@ const nextAppLoader: webpack.LoaderDefinitionFunction<{
: 'null'
}
export const serverHooks = require('next/dist/client/components/hooks-server-context.js')
export const renderToReadableStream = require('next/dist/compiled/react-server-dom-webpack/writer.browser.server').renderToReadableStream
export const __next_app_webpack_require__ = __webpack_require__
`
......
......@@ -59,11 +59,11 @@ export class FlightClientEntryPlugin {
)
compiler.hooks.finishMake.tapPromise(PLUGIN_NAME, (compilation) => {
return this.createClientEndpoints(compiler, compilation)
return this.createClientEntries(compiler, compilation)
})
}
async createClientEndpoints(compiler: any, compilation: any) {
async createClientEntries(compiler: any, compilation: any) {
const promises: Array<
ReturnType<typeof this.injectClientEntryAndSSRModules>
> = []
......
// @ts-expect-error createServerContext exists on experimental channel
import { createServerContext } from 'react'
// createServerContext exists in react@experimental + react-dom@experimental
if (typeof createServerContext === 'undefined') {
throw new Error(
'"app" directory requires React.createServerContext which is not available in the version of React you are using. Please update to react@experimental and react-dom@experimental.'
)
}
export class DynamicServerError extends Error {
constructor(type: string) {
super(`Dynamic server usage: ${type}`)
......
import type { AsyncLocalStorage } from 'async_hooks'
import { useContext } from 'react'
import {
HeadersContext,
......@@ -6,23 +5,7 @@ import {
CookiesContext,
DynamicServerError,
} from './hooks-server-context'
export interface StaticGenerationStore {
inUse?: boolean
pathname?: string
revalidate?: number
fetchRevalidate?: number
isStaticGeneration?: boolean
}
export let staticGenerationAsyncStorage:
| AsyncLocalStorage<StaticGenerationStore>
| StaticGenerationStore = {}
if (process.env.NEXT_RUNTIME !== 'edge' && typeof window === 'undefined') {
staticGenerationAsyncStorage =
new (require('async_hooks').AsyncLocalStorage)()
}
import { staticGenerationAsyncStorage } from './static-generation-async-storage'
function useStaticGenerationBailout(reason: string) {
const staticGenerationStore =
......
import type { AsyncLocalStorage } from 'async_hooks'
export interface StaticGenerationStore {
inUse?: boolean
pathname?: string
revalidate?: number
fetchRevalidate?: number
isStaticGeneration?: boolean
}
export let staticGenerationAsyncStorage:
| AsyncLocalStorage<StaticGenerationStore>
| StaticGenerationStore = {}
if (process.env.NEXT_RUNTIME !== 'edge' && typeof window === 'undefined') {
staticGenerationAsyncStorage =
new (require('async_hooks').AsyncLocalStorage)()
}
......@@ -388,9 +388,7 @@ export default async function exportPage({
// and bail when dynamic dependencies are detected
// only fully static paths are fully generated here
if (isAppDir) {
const {
DynamicServerError,
} = require('../client/components/hooks-server-context')
const { DynamicServerError } = components.ComponentMod.serverHooks
const { renderToHTMLOrFlight } =
require('../server/app-render') as typeof import('../server/app-render')
......
......@@ -3,10 +3,11 @@ import type { LoadComponentsReturnType } from './load-components'
import type { ServerRuntime } from '../types'
// TODO-APP: change to React.use once it becomes stable
// @ts-ignore
import React, { experimental_use as use } from 'react'
import { ParsedUrlQuery, stringify as stringifyQuery } from 'querystring'
import { createFromReadableStream } from 'next/dist/compiled/react-server-dom-webpack'
import { renderToReadableStream } from 'next/dist/compiled/react-server-dom-webpack/writer.browser.server'
import { NextParsedUrlQuery } from './request-meta'
import RenderResult from './render-result'
import {
......@@ -103,15 +104,15 @@ let isFetchPatched = false
// we patch fetch to collect cache information used for
// determining if a page is static or not
function patchFetch() {
function patchFetch(ComponentMod: any) {
if (isFetchPatched) return
isFetchPatched = true
const { DynamicServerError } =
require('../client/components/hooks-server-context') as typeof import('../client/components/hooks-server-context')
ComponentMod.serverHooks as typeof import('../client/components/hooks-server-context')
const { staticGenerationAsyncStorage } =
require('../client/components/hooks-server') as typeof import('../client/components/hooks-server')
require('../client/components/static-generation-async-storage') as typeof import('../client/components/static-generation-async-storage')
const origFetch = (global as any).fetch
......@@ -224,6 +225,7 @@ function useFlightResponse(
function createServerComponentRenderer(
ComponentToRender: React.ComponentType,
ComponentMod: {
renderToReadableStream: any
__next_app_webpack_require__?: any
__next_rsc__?: {
__webpack_require__?: any
......@@ -259,7 +261,7 @@ function createServerComponentRenderer(
let RSCStream: ReadableStream<Uint8Array>
const createRSCStream = () => {
if (!RSCStream) {
RSCStream = renderToReadableStream(
RSCStream = ComponentMod.renderToReadableStream(
<ComponentToRender />,
serverComponentManifest,
{
......@@ -516,10 +518,19 @@ export async function renderToHTMLOrFlight(
isPagesDir: boolean,
isStaticGeneration: boolean = false
): Promise<RenderResult | null> {
patchFetch()
const {
buildManifest,
subresourceIntegrityManifest,
serverComponentManifest,
serverCSSManifest = {},
supportsDynamicHTML,
ComponentMod,
} = renderOpts
patchFetch(ComponentMod)
const { staticGenerationAsyncStorage } =
require('../client/components/hooks-server') as typeof import('../client/components/hooks-server')
require('../client/components/static-generation-async-storage') as typeof import('../client/components/static-generation-async-storage')
if (
!('getStore' in staticGenerationAsyncStorage) &&
......@@ -538,27 +549,11 @@ export async function renderToHTMLOrFlight(
: staticGenerationAsyncStorage
const { CONTEXT_NAMES } =
require('../client/components/hooks-server-context') as typeof import('../client/components/hooks-server-context')
// @ts-expect-error createServerContext exists in react@experimental + react-dom@experimental
if (typeof React.createServerContext === 'undefined') {
throw new Error(
'"app" directory requires React.createServerContext which is not available in the version of React you are using. Please update to react@experimental and react-dom@experimental.'
)
}
ComponentMod.serverHooks as typeof import('../client/components/hooks-server-context')
// don't modify original query object
query = Object.assign({}, query)
const {
buildManifest,
subresourceIntegrityManifest,
serverComponentManifest,
serverCSSManifest = {},
supportsDynamicHTML,
ComponentMod,
} = renderOpts
const isFlight = req.headers.__flight__ !== undefined
const isPrefetch = req.headers.__flight_prefetch__ !== undefined
......@@ -570,9 +565,13 @@ export async function renderToHTMLOrFlight(
// Empty so that the client-side router will do a full page navigation.
const flightData: FlightData = pathname + (search ? `?${search}` : '')
return new FlightRenderResult(
renderToReadableStream(flightData, serverComponentManifest, {
onError: flightDataRendererErrorHandler,
}).pipeThrough(createBufferedTransformStream())
ComponentMod.renderToReadableStream(
flightData,
serverComponentManifest,
{
onError: flightDataRendererErrorHandler,
}
).pipeThrough(createBufferedTransformStream())
)
}
......@@ -1057,7 +1056,7 @@ export async function renderToHTMLOrFlight(
).slice(1),
]
const readable = renderToReadableStream(
const readable = ComponentMod.renderToReadableStream(
flightData,
serverComponentManifest,
{
......
import { usePathname } from 'next/dist/client/components/hooks-client'
// import { usePathname } from 'next/dist/client/components/hooks-client'
export default function Page() {
// This should throw an error.
usePathname()
// usePathname()
return null
}
import { useRouter } from 'next/dist/client/components/hooks-client'
// import { useRouter } from 'next/dist/client/components/hooks-client'
export default function Page() {
// This should throw an error.
useRouter()
// useRouter()
return null
}
import { useSearchParams } from 'next/dist/client/components/hooks-client'
// import { useSearchParams } from 'next/dist/client/components/hooks-client'
export default function Page() {
// This should throw an error.
useSearchParams()
// useSearchParams()
return null
}
body {
font-size: xx-large;
font-size: large;
}
......@@ -780,7 +780,8 @@ describe('app dir', () => {
})
describe('next/router', () => {
it('should always return null when accessed from /app', async () => {
// `useRouter` should not be accessible in server components.
it.skip('should always return null when accessed from /app', async () => {
const browser = await webdriver(next.url, '/old-router')
try {
......
......@@ -44,7 +44,7 @@ describe('app dir - react server components', () => {
},
packageJson: {
scripts: {
setup: `cp -r ./node_modules_bak/non-isomorphic-text ./node_modules; cp -r ./node_modules_bak/random-module-instance ./node_modules`,
setup: `cp -r ./node_modules_bak/* ./node_modules`,
build: 'yarn setup && next build',
dev: 'yarn setup && next dev',
start: 'next start',
......@@ -398,6 +398,26 @@ describe('app dir - react server components', () => {
)
})
it('should resolve the subset react in server components based on the react-server condition', async () => {
await fetchViaHTTP(next.url, '/react-server').then(async (response) => {
const result = await resolveStreamResponse(response)
expect(result).toContain('Server: <!-- -->subset')
expect(result).toContain('Client: <!-- -->full')
})
})
it('should resolve 3rd party package exports based on the react-server condition', async () => {
await fetchViaHTTP(next.url, '/react-server/3rd-party-package').then(
async (response) => {
const result = await resolveStreamResponse(response)
expect(result).toContain('Server: index.react-server')
expect(result).toContain('Server subpath: subpath.react-server')
expect(result).toContain('Client: index.default')
expect(result).toContain('Client subpath: subpath.default')
}
)
})
if (!isNextDev) {
it('should generate edge SSR manifests for Node.js', async () => {
const distServerDir = path.join(distDir, 'server')
......
'client'
import v from 'conditional-exports'
import v1 from 'conditional-exports/subpath'
export default function Client() {
return (
<>
{`Client: ${v}`}
<br />
{`Client subpath: ${v1}`}
<br />
</>
)
}
import v from 'conditional-exports'
import v1 from 'conditional-exports/subpath'
import Client from './client'
export default function Page() {
return (
<div>
{`Server: ${v}`}
<br />
{`Server subpath: ${v1}`}
<br />
<Client />
</div>
)
}
'client'
export { default } from './detector'
import React from 'react'
export default function Detector() {
return 'useState' in React ? 'full' : 'subset'
}
import Detector from './detector'
import ClientDetector from './client-detector'
export default function Page() {
return (
<div>
Server: <Detector />
<br />
Client: <ClientDetector />
<br />
</div>
)
}
{
"name": "conditional-exports",
"main": "index.js",
"exports": {
".": {
"react-server": "./index.server.js",
"default": "./index.js"
},
"./subpath": {
"react-server": "./subpath.server.js",
"default": "./subpath.js"
},
"./package.json": "./package.json"
}
}
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