Unverified Commit bc51d9bb authored by Josh Story's avatar Josh Story Committed by GitHub

[Cache Components] Allow hiding logs after abort (#84579)

When Next.js prerenders with Cache Components enabled it will abort
prerenders before they have completed. It is common for user code to log
in a catch block and with this new prerendering behavior these logs will
trigger frequently for cases where the rejection only happened because
we are abandoning the work. This is analagous to treating a fetch
rejection as an error even when you pass an abortSignal into the fetch
and then abort it.

Currently when logs occur after an aborted prerender we dim the log
output to signify that the log line is not particuarly important. This
is useful and for folks who want to see everything output by their
program this is potentially the best behavior. However many log
collectors don't support colors and have no way of contextualizing the
dimmed log which can lead to them appearing to be more significant than
they are.

To combat this we expose a new experimental flag that allows you to
completely disable logs after aborting. This means the log lines you see
are much higher signal.

The implementation is somewhat complex because we install the
environment extensions as early as possible before any user code can run
but we can't install the hiding behavior until we have a config in
scope. I've added the hiding installation to next-server and the worker
exportPages entrypoints. I believe this is sufficient to cover all
contexts in which we will prerender.
parent a89f854f
......@@ -871,5 +871,6 @@
"870": "refresh can only be called from within a Server Action. See more info here: https://nextjs.org/docs/app/api-reference/functions/refresh",
"871": "Image with src \"%s\" is using a query string which is not configured in images.localPatterns.\\nRead more: https://nextjs.org/docs/messages/next-image-unconfigured-localpatterns",
"872": "updateTag can only be called from within a Server Action. To invalidate cache tags in Route Handlers or other contexts, use revalidateTag instead. See more info here: https://nextjs.org/docs/app/api-reference/functions/updateTag",
"873": "Invalid profile provided \"%s\" must be configured under cacheLife in next.config or be \"max\""
"873": "Invalid profile provided \"%s\" must be configured under cacheLife in next.config or be \"max\"",
"874": "Expected not to install Node.js global behaviors in the edge runtime."
}
......@@ -50,6 +50,7 @@ import type { PagesRenderContext, PagesSharedContext } from '../server/render'
import type { AppSharedContext } from '../server/app-render/app-render'
import { MultiFileWriter } from '../lib/multi-file-writer'
import { createRenderResumeDataCache } from '../server/resume-data-cache/resume-data-cache'
import { installGlobalBehaviors } from '../server/node-environment-extensions/global-behaviors'
;(globalThis as any).__NEXT_DATA__ = {
nextExport: true,
}
......@@ -336,6 +337,8 @@ export async function exportPages(
renderResumeDataCachesByPage = {},
} = input
installGlobalBehaviors(nextConfig)
if (nextConfig.experimental.enablePrerenderSourceMaps) {
try {
// Same as `next dev`
......
......@@ -346,6 +346,7 @@ export const experimentalSchema = {
])
.optional(),
lockDistDir: z.boolean().optional(),
hideLogsAfterAbort: z.boolean().optional(),
}
export const configSchema: zod.ZodType<NextConfig> = z.lazy(() =>
......
......@@ -817,6 +817,14 @@ export interface ExperimentalConfig {
* @default true
*/
lockDistDir?: boolean
/**
* Hide logs that occur after a render has already aborted.
* This can help reduce noise in the console when dealing with aborted renders.
*
* @default false
*/
hideLogsAfterAbort?: boolean
}
export type ExportPathMap = {
......@@ -1499,6 +1507,7 @@ export const defaultConfig = Object.freeze({
lockDistDir: true,
isolatedDevBuild: true,
middlewareClientMaxBodySize: 10_485_760, // 10MB
hideLogsAfterAbort: false,
},
htmlLimitedBots: undefined,
bundlePagesRouterDependencies: false,
......
......@@ -123,6 +123,7 @@ import {
RouterServerContextSymbol,
routerServerGlobal,
} from './lib/router-utils/router-server-context'
import { installGlobalBehaviors } from './node-environment-extensions/global-behaviors'
export * from './base-server'
......@@ -273,6 +274,8 @@ export default class NextNodeServer extends BaseServer<
// Initialize super class
super(options)
installGlobalBehaviors(this.nextConfig)
const isDev = options.dev ?? false
this.isDev = isDev
this.sriEnabled = Boolean(options.conf.experimental?.sri?.algorithm)
......
......@@ -11,6 +11,17 @@ export function registerGetCacheSignal(getSignal: GetCacheSignal): void {
cacheSignals.push(getSignal)
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars -- we may use later and want parity with the HIDDEN_STYLE value
const DIMMED_STYLE = 'dimmed'
const HIDDEN_STYLE = 'hidden'
type LogStyle = typeof DIMMED_STYLE | typeof HIDDEN_STYLE
let currentAbortedLogsStyle: LogStyle = 'dimmed'
export function setAbortedLogsStyle(style: LogStyle) {
currentAbortedLogsStyle = style
}
type InterceptableConsoleMethod =
| 'error'
| 'assert'
......@@ -175,75 +186,89 @@ function patchConsoleMethod(methodName: InterceptableConsoleMethod): void {
const wrapperMethod = function (this: typeof console, ...args: any[]) {
const consoleStore = consoleAsyncStorage.getStore()
if (consoleStore?.dim === true) {
return applyWithDimming.call(
this,
consoleStore,
originalMethod,
methodName,
args
)
} else {
// First we see if there is a cache signal for our current scope. If we're in a client render it'll
// come from the client React cacheSignal implementation. If we are in a server render it'll come from
// the server React cacheSignal implementation. Any particular console call will be in one, the other, or neither
// scope and these signals return null if you are out of scope so this can be called from a single global patch
// and still work properly.
for (let i = 0; i < cacheSignals.length; i++) {
const signal = cacheSignals[i]() // try to get a signal from registered functions
if (signal) {
// We are in a React Server render and can consult the React cache signal to determine if logs
// are now dimmable.
if (signal.aborted) {
return applyWithDimming.call(
this,
consoleStore,
originalMethod,
methodName,
args
)
} else {
return originalMethod.apply(this, args)
// First we see if there is a cache signal for our current scope. If we're in a client render it'll
// come from the client React cacheSignal implementation. If we are in a server render it'll come from
// the server React cacheSignal implementation. Any particular console call will be in one, the other, or neither
// scope and these signals return null if you are out of scope so this can be called from a single global patch
// and still work properly.
for (let i = 0; i < cacheSignals.length; i++) {
const signal = cacheSignals[i]() // try to get a signal from registered functions
if (signal) {
// We are in a React Server render and can consult the React cache signal to determine if logs
// are now dimmable.
if (signal.aborted) {
if (currentAbortedLogsStyle === HIDDEN_STYLE) {
return
}
return applyWithDimming.call(
this,
consoleStore,
originalMethod,
methodName,
args
)
} else if (consoleStore?.dim === true) {
return applyWithDimming.call(
this,
consoleStore,
originalMethod,
methodName,
args
)
} else {
return originalMethod.apply(this, args)
}
}
// We need to fall back to checking the work unit store for two reasons.
// 1. Client React does not yet implement cacheSignal (it always returns null)
// 2. route.ts files aren't rendered with React but do have prerender semantics
// TODO in the future we should be able to remove this once there is a runnable cache
// scope independent of actual React rendering.
const workUnitStore = workUnitAsyncStorage.getStore()
switch (workUnitStore?.type) {
case 'prerender':
case 'prerender-runtime':
// These can be hit in a route handler. In the future we can use potential React.createCache API
// to create a cache scope for arbitrary computation and can move over to cacheSignal exclusively.
// fallthrough
case 'prerender-client':
// This is a react-dom/server render and won't have a cacheSignal until React adds this for the client world.
const renderSignal = workUnitStore.renderSignal
if (renderSignal.aborted) {
return applyWithDimming.call(
this,
consoleStore,
originalMethod,
methodName,
args
)
} else {
return originalMethod.apply(this, args)
}
// We need to fall back to checking the work unit store for two reasons.
// 1. Client React does not yet implement cacheSignal (it always returns null)
// 2. route.ts files aren't rendered with React but do have prerender semantics
// TODO in the future we should be able to remove this once there is a runnable cache
// scope independent of actual React rendering.
const workUnitStore = workUnitAsyncStorage.getStore()
switch (workUnitStore?.type) {
case 'prerender':
case 'prerender-runtime':
// These can be hit in a route handler. In the future we can use potential React.createCache API
// to create a cache scope for arbitrary computation and can move over to cacheSignal exclusively.
// fallthrough
case 'prerender-client':
// This is a react-dom/server render and won't have a cacheSignal until React adds this for the client world.
const renderSignal = workUnitStore.renderSignal
if (renderSignal.aborted) {
if (currentAbortedLogsStyle === HIDDEN_STYLE) {
return
}
case 'prerender-legacy':
case 'prerender-ppr':
case 'cache':
case 'unstable-cache':
case 'private-cache':
case 'request':
case undefined:
return applyWithDimming.call(
this,
consoleStore,
originalMethod,
methodName,
args
)
}
// intentional fallthrough
case 'prerender-legacy':
case 'prerender-ppr':
case 'cache':
case 'unstable-cache':
case 'private-cache':
case 'request':
case undefined:
if (consoleStore?.dim === true) {
return applyWithDimming.call(
this,
consoleStore,
originalMethod,
methodName,
args
)
} else {
return originalMethod.apply(this, args)
default:
workUnitStore satisfies never
}
}
default:
workUnitStore satisfies never
}
}
if (originalName) {
......
/**
* Unlike most files in the node-environment-extensions folder this one is not
* an extension itself but it exposes a function to install config based global
* behaviors that should be loaded whenever a Node Server or Node Worker are created.
*/
import type { NextConfigComplete } from '../config-shared'
import { InvariantError } from '../../shared/lib/invariant-error'
import { setAbortedLogsStyle } from './console-dim.external'
export function installGlobalBehaviors(config: NextConfigComplete) {
if (process.env.NEXT_RUNTIME === 'edge') {
throw new InvariantError(
'Expected not to install Node.js global behaviors in the edge runtime.'
)
}
if (config.experimental?.hideLogsAfterAbort === true) {
setAbortedLogsStyle('hidden')
} else {
setAbortedLogsStyle('dimmed')
}
}
......@@ -3,23 +3,27 @@
import { use } from 'react'
function log(outBadge: string, errBadge: string) {
console.info(`${outBadge} /console: template(one: %s, two: %s)`, 'one', 'two')
console.info(
`${outBadge} /console-after-abort/client: template(one: %s, two: %s)`,
'one',
'two'
)
console.log(
// eslint-disable-next-line no-useless-concat
`${outBadge} /console: This is a console page. Don't match the codeframe.`
`${outBadge} /console-after-abort/client: This is a console page. Don't match the codeframe.`
)
console.warn(`${errBadge} /console: not a template`, {
console.warn(`${errBadge} /console-after-abort/client: not a template`, {
foo: 'just-some-object',
})
console.error(new Error(`${errBadge} /console: test`))
console.error(new Error(`${errBadge} /console-after-abort/client: test`))
console.assert(
false,
`${errBadge} /console: This is an assert message with a %s`,
`${errBadge} /console-after-abort/client: This is an assert message with a %s`,
'template'
)
console.assert(
true,
`${errBadge} /console: This is an assert message without a template`
`${errBadge} /console-after-abort/client: This is an assert message without a template`
)
}
......@@ -32,7 +36,9 @@ export default function ClientConsolePage({
outBadge: string
errBadge: string
}) {
console.log(`${outBadge} logging before prerender aborts in client component`)
console.log(
`${outBadge} /console-after-abort/client: logging before prerender aborts in client component`
)
setTimeout(log.bind(null, outBadge, errBadge), 1)
use(data)
......
......@@ -9,7 +9,9 @@ export default async function ConsolePage() {
const outBadge = `:::${i}:out:::`
const errBadge = `:::${i++}:err:::`
console.log(`${outBadge} logging before prerender abort`)
console.log(
`${outBadge} /console-after-abort/client: logging before prerender abort`
)
return (
<ClientConsolePage data={data} outBadge={outBadge} errBadge={errBadge} />
......
import { headers } from 'next/headers'
async function cachedConsoleCalls(outBadge: string, errBadge: string) {
'use cache'
console.info(
`${outBadge} /console-after-abort/server: template(one: %s, two: %s)`,
'one',
'two'
)
console.log(
// eslint-disable-next-line no-useless-concat
`${outBadge} /console-after-abort/server: This is a console page` +
". Don't match the codeframe."
)
console.warn(`${errBadge} /console-after-abort/server: not a template`, {
foo: 'just-some-object',
})
// TODO(veil): Assert on inspected errors once we sourcemap errors replayed from Cache environment.
// console.error(new Error('/console-after-abort/server: test'))
console.assert(
false,
`${errBadge} /console-after-abort/server: This is an assert message with a %s`,
'template'
)
console.assert(
true,
`${errBadge} /console-after-abort/server: This is an assert message without a template`
)
}
let i = 0
export default async function ConsolePage() {
const outBadge = `:::${i}:out:::`
const errBadge = `:::${i++}:err:::`
console.log(
`${outBadge} /console-after-abort/server: logging before trying await headers()`
)
try {
await headers()
} catch (error) {
console.error(
`${errBadge} /console-after-abort/server: caught error trying await headers()`
)
}
// We add some delay because in tests this sometimes runs after the second render pass
// has already started and that can lead to out-of-order console logs.
console.info(
`${outBadge} /console-after-abort/server: template(one: %s, two: %s)`,
'one',
'two'
)
console.log(
// eslint-disable-next-line no-useless-concat
`${outBadge} /console-after-abort/server: This is a console page` +
". Don't match the codeframe."
)
console.warn(`${errBadge} /console-after-abort/server: not a template`, {
foo: 'just-some-object',
})
console.error(new Error(`${errBadge} /console-after-abort/server: test`))
console.assert(
false,
`${errBadge} /console-after-abort/server: This is an assert message with a %s`,
'template'
)
console.assert(
true,
`${errBadge} /console-after-abort/server: This is an assert message without a template`
)
await cachedConsoleCalls(outBadge, errBadge)
return null
}
export default function Root({ children }: { children: React.ReactNode }) {
return (
<html>
<body>
<main>{children}</main>
</body>
</html>
)
}
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {
experimental: {
cacheComponents: true,
},
}
module.exports = nextConfig
'use client'
import { use } from 'react'
function log(outBadge: string, errBadge: string) {
console.info(
`${outBadge} /console-after-abort/client: template(one: %s, two: %s)`,
'one',
'two'
)
console.log(
// eslint-disable-next-line no-useless-concat
`${outBadge} /console-after-abort/client: This is a console page. Don't match the codeframe.`
)
console.warn(`${errBadge} /console-after-abort/client: not a template`, {
foo: 'just-some-object',
})
console.error(new Error(`${errBadge} /console-after-abort/client: test`))
console.assert(
false,
`${errBadge} /console-after-abort/client: This is an assert message with a %s`,
'template'
)
console.assert(
true,
`${errBadge} /console-after-abort/client: This is an assert message without a template`
)
}
export default function ClientConsolePage({
data,
outBadge,
errBadge,
}: {
data: Promise<any>
outBadge: string
errBadge: string
}) {
console.log(
`${outBadge} /console-after-abort/client: logging before prerender aborts in client component`
)
setTimeout(log.bind(null, outBadge, errBadge), 1)
use(data)
return null
}
import { headers } from 'next/headers'
import ClientConsolePage from './client'
let i = 0
export default async function ConsolePage() {
const data = headers().then(() => null)
const outBadge = `:::${i}:out:::`
const errBadge = `:::${i++}:err:::`
console.log(
`${outBadge} /console-after-abort/client: logging before prerender abort`
)
return (
<ClientConsolePage data={data} outBadge={outBadge} errBadge={errBadge} />
)
}
import { Suspense } from 'react'
export default async function layout({ children }) {
return <Suspense>{children}</Suspense>
}
import { headers } from 'next/headers'
async function cachedConsoleCalls(outBadge: string, errBadge: string) {
'use cache'
console.info(
`${outBadge} /console-after-abort/server: template(one: %s, two: %s)`,
'one',
'two'
)
console.log(
// eslint-disable-next-line no-useless-concat
`${outBadge} /console-after-abort/server: This is a console page` +
". Don't match the codeframe."
)
console.warn(`${errBadge} /console-after-abort/server: not a template`, {
foo: 'just-some-object',
})
// TODO(veil): Assert on inspected errors once we sourcemap errors replayed from Cache environment.
// console.error(new Error('/console-after-abort/server: test'))
console.assert(
false,
`${errBadge} /console-after-abort/server: This is an assert message with a %s`,
'template'
)
console.assert(
true,
`${errBadge} /console-after-abort/server: This is an assert message without a template`
)
}
let i = 0
export default async function ConsolePage() {
const outBadge = `:::${i}:out:::`
const errBadge = `:::${i++}:err:::`
console.log(
`${outBadge} /console-after-abort/server: logging before trying await headers()`
)
try {
await headers()
} catch (error) {
console.error(
`${errBadge} /console-after-abort/server: caught error trying await headers()`
)
}
// We add some delay because in tests this sometimes runs after the second render pass
// has already started and that can lead to out-of-order console logs.
console.info(
`${outBadge} /console-after-abort/server: template(one: %s, two: %s)`,
'one',
'two'
)
console.log(
// eslint-disable-next-line no-useless-concat
`${outBadge} /console-after-abort/server: This is a console page` +
". Don't match the codeframe."
)
console.warn(`${errBadge} /console-after-abort/server: not a template`, {
foo: 'just-some-object',
})
console.error(new Error(`${errBadge} /console-after-abort/server: test`))
console.assert(
false,
`${errBadge} /console-after-abort/server: This is an assert message with a %s`,
'template'
)
console.assert(
true,
`${errBadge} /console-after-abort/server: This is an assert message without a template`
)
await cachedConsoleCalls(outBadge, errBadge)
return null
}
import { headers } from 'next/headers'
async function cachedConsoleCalls(outBadge: string, errBadge: string) {
'use cache'
console.info(`${outBadge} /console: template(one: %s, two: %s)`, 'one', 'two')
await 1
console.log(
// eslint-disable-next-line no-useless-concat
`${outBadge} /console: This is a console page` +
". Don't match the codeframe."
)
await 1
console.warn(`${errBadge} /console: not a template`, {
foo: 'just-some-object',
})
await 1
// TODO(veil): Assert on inspected errors once we sourcemap errors replayed from Cache environment.
// console.error(new Error('/console: test'))
console.assert(
......@@ -28,25 +29,20 @@ let i = 0
export default async function ConsolePage() {
const outBadge = `:::${i}:out:::`
const errBadge = `:::${i++}:err:::`
console.log(`${outBadge} logging before trying await headers()`)
try {
await headers()
} catch (error) {
console.error(`${errBadge} caught error trying await headers()`)
}
// We add some delay because in tests this sometimes runs after the second render pass
// has already started and that can lead to out-of-order console logs.
console.info(`${outBadge} /console: template(one: %s, two: %s)`, 'one', 'two')
await 1
console.log(
// eslint-disable-next-line no-useless-concat
`${outBadge} /console: This is a console page` +
". Don't match the codeframe."
)
await 1
console.warn(`${errBadge} /console: not a template`, {
foo: 'just-some-object',
})
await 1
console.error(new Error(`${errBadge} /console: test`))
await 1
console.assert(
false,
`${errBadge} /console: This is an assert message with a %s`,
......@@ -57,6 +53,7 @@ export default async function ConsolePage() {
`${errBadge} /console: This is an assert message without a template`
)
await 1
await cachedConsoleCalls(outBadge, errBadge)
return null
......
export default function Root({ children }: { children: React.ReactNode }) {
return (
<html>
<body>
<main>{children}</main>
</body>
</html>
)
}
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {
experimental: {
cacheComponents: true,
hideLogsAfterAbort: true,
},
}
module.exports = nextConfig
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