Unverified Commit f3ff9430 authored by Andrew Clark's avatar Andrew Clark Committed by GitHub

[lts-active] Don't load the app-page runtime in proxy.ts (#100423)

Backports [Don't load the app-page runtime in proxy.ts
(#100384)](https://github.com/vercel/next.js/pull/100384) to
`releases/lts/active` for the next 16.4.x patch. The cherry-pick applied
cleanly.

<!-- NEXT_JS_LLM -->

🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-authored-by: 's avatarTobias Lins <me@tobi.sh>
Co-authored-by: 's avatarJanka Uryga <lolzatu2@gmail.com>
parent f697cf5d
......@@ -30,9 +30,6 @@ import type {
PrerenderStoreModernServer,
} from './work-unit-async-storage.external'
// Once postpone is in stable we should switch to importing the postpone export directly
import React from 'react'
import { DynamicServerError } from '../../client/components/hooks-server-context'
import { StaticGenBailoutError } from '../../client/components/static-generation-bailout'
import { getStagedRenderingController } from './work-unit-async-storage.external'
......@@ -1250,10 +1247,14 @@ function addErrorContext(
componentStack: string,
createInstantStack: (() => Error) | null
) {
const ownerStack =
process.env.NODE_ENV !== 'production' && React.captureOwnerStack
? React.captureOwnerStack()
: null
let ownerStack: string | null = null
if (process.env.NODE_ENV !== 'production') {
// Required lazily so production builds don't include React. This module
// is reachable from proxy.ts via `connection()` in `next/server`, and
// importing React there pulls in the whole app-page runtime.
const React = require('react') as typeof import('react')
ownerStack = React.captureOwnerStack ? React.captureOwnerStack() : null
}
if (createInstantStack !== null) {
error.cause = createInstantStack()
......
import type { ReactNode } from 'react'
export default function Root({ children }: { children: ReactNode }) {
return (
<html>
<body>{children}</body>
</html>
)
}
export default function Page() {
return <p>hello world</p>
}
import fs from 'fs'
// @ts-check
/** @type {import('next').NextAdapter} */
const adapter = {
name: 'proxy-no-page-runtime-adapter',
async onBuildComplete({ outputs }) {
await fs.promises.writeFile(
'build-complete.json',
JSON.stringify({ outputs }, null, 2)
)
},
}
export default adapter
import type { NextConfig } from 'next'
const nextConfig: NextConfig = {
adapterPath: require.resolve('./my-adapter.mjs'),
}
export default nextConfig
import { nextTestSetup } from 'e2e-utils'
import type { NextAdapter } from 'next'
type Outputs = Parameters<NextAdapter['onBuildComplete']>[0]['outputs']
const isPageRuntime = (file: string) =>
/app-page.*\.runtime\.prod\.js$/.test(file)
// A proxy that only imports `next/server` must not pull in the app-page runtime.
// It's expensive to load on a cold start and was previously reachable through
// `connection()` -> `dynamic-rendering`, which imported React.
// This test accesses the filesystem, which is not available in deploy mode
// @force-gate !deploy
describe('proxy-no-page-runtime', () => {
const { next } = nextTestSetup({
files: __dirname,
})
it('should run the proxy', async () => {
const res = await next.fetch('/')
expect(res.status).toBe(200)
expect(res.headers.get('x-from-proxy')).toBe('1')
})
it('should not include the app-page runtime in the proxy output', async () => {
const { outputs }: { outputs: Outputs } = await next.readJSON(
'build-complete.json'
)
// The app-page runtime as the page outputs trace it. If it is renamed or
// stops being traced, this fails instead of the check below passing.
const pageRuntimeFiles = [
...new Set(
outputs.appPages.flatMap((output) =>
Object.keys(output.assets).filter(isPageRuntime)
)
),
]
expect(pageRuntimeFiles).not.toEqual([])
expect(outputs.middleware).toBeDefined()
const proxyFiles = Object.keys(outputs.middleware!.assets)
expect(proxyFiles).not.toEqual([])
expect(
proxyFiles.filter((file) => pageRuntimeFiles.includes(file))
).toEqual([])
})
})
import { NextResponse } from 'next/server'
export function proxy() {
const response = NextResponse.next()
response.headers.set('x-from-proxy', '1')
return response
}
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