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 @@ ...@@ -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", "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", "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", "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' ...@@ -50,6 +50,7 @@ import type { PagesRenderContext, PagesSharedContext } from '../server/render'
import type { AppSharedContext } from '../server/app-render/app-render' import type { AppSharedContext } from '../server/app-render/app-render'
import { MultiFileWriter } from '../lib/multi-file-writer' import { MultiFileWriter } from '../lib/multi-file-writer'
import { createRenderResumeDataCache } from '../server/resume-data-cache/resume-data-cache' import { createRenderResumeDataCache } from '../server/resume-data-cache/resume-data-cache'
import { installGlobalBehaviors } from '../server/node-environment-extensions/global-behaviors'
;(globalThis as any).__NEXT_DATA__ = { ;(globalThis as any).__NEXT_DATA__ = {
nextExport: true, nextExport: true,
} }
...@@ -336,6 +337,8 @@ export async function exportPages( ...@@ -336,6 +337,8 @@ export async function exportPages(
renderResumeDataCachesByPage = {}, renderResumeDataCachesByPage = {},
} = input } = input
installGlobalBehaviors(nextConfig)
if (nextConfig.experimental.enablePrerenderSourceMaps) { if (nextConfig.experimental.enablePrerenderSourceMaps) {
try { try {
// Same as `next dev` // Same as `next dev`
......
...@@ -346,6 +346,7 @@ export const experimentalSchema = { ...@@ -346,6 +346,7 @@ export const experimentalSchema = {
]) ])
.optional(), .optional(),
lockDistDir: z.boolean().optional(), lockDistDir: z.boolean().optional(),
hideLogsAfterAbort: z.boolean().optional(),
} }
export const configSchema: zod.ZodType<NextConfig> = z.lazy(() => export const configSchema: zod.ZodType<NextConfig> = z.lazy(() =>
......
...@@ -817,6 +817,14 @@ export interface ExperimentalConfig { ...@@ -817,6 +817,14 @@ export interface ExperimentalConfig {
* @default true * @default true
*/ */
lockDistDir?: boolean 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 = { export type ExportPathMap = {
...@@ -1499,6 +1507,7 @@ export const defaultConfig = Object.freeze({ ...@@ -1499,6 +1507,7 @@ export const defaultConfig = Object.freeze({
lockDistDir: true, lockDistDir: true,
isolatedDevBuild: true, isolatedDevBuild: true,
middlewareClientMaxBodySize: 10_485_760, // 10MB middlewareClientMaxBodySize: 10_485_760, // 10MB
hideLogsAfterAbort: false,
}, },
htmlLimitedBots: undefined, htmlLimitedBots: undefined,
bundlePagesRouterDependencies: false, bundlePagesRouterDependencies: false,
......
...@@ -123,6 +123,7 @@ import { ...@@ -123,6 +123,7 @@ import {
RouterServerContextSymbol, RouterServerContextSymbol,
routerServerGlobal, routerServerGlobal,
} from './lib/router-utils/router-server-context' } from './lib/router-utils/router-server-context'
import { installGlobalBehaviors } from './node-environment-extensions/global-behaviors'
export * from './base-server' export * from './base-server'
...@@ -273,6 +274,8 @@ export default class NextNodeServer extends BaseServer< ...@@ -273,6 +274,8 @@ export default class NextNodeServer extends BaseServer<
// Initialize super class // Initialize super class
super(options) super(options)
installGlobalBehaviors(this.nextConfig)
const isDev = options.dev ?? false const isDev = options.dev ?? false
this.isDev = isDev this.isDev = isDev
this.sriEnabled = Boolean(options.conf.experimental?.sri?.algorithm) this.sriEnabled = Boolean(options.conf.experimental?.sri?.algorithm)
......
...@@ -11,6 +11,17 @@ export function registerGetCacheSignal(getSignal: GetCacheSignal): void { ...@@ -11,6 +11,17 @@ export function registerGetCacheSignal(getSignal: GetCacheSignal): void {
cacheSignals.push(getSignal) 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 = type InterceptableConsoleMethod =
| 'error' | 'error'
| 'assert' | 'assert'
...@@ -175,75 +186,89 @@ function patchConsoleMethod(methodName: InterceptableConsoleMethod): void { ...@@ -175,75 +186,89 @@ function patchConsoleMethod(methodName: InterceptableConsoleMethod): void {
const wrapperMethod = function (this: typeof console, ...args: any[]) { const wrapperMethod = function (this: typeof console, ...args: any[]) {
const consoleStore = consoleAsyncStorage.getStore() const consoleStore = consoleAsyncStorage.getStore()
if (consoleStore?.dim === true) { // First we see if there is a cache signal for our current scope. If we're in a client render it'll
return applyWithDimming.call( // come from the client React cacheSignal implementation. If we are in a server render it'll come from
this, // the server React cacheSignal implementation. Any particular console call will be in one, the other, or neither
consoleStore, // scope and these signals return null if you are out of scope so this can be called from a single global patch
originalMethod, // and still work properly.
methodName, for (let i = 0; i < cacheSignals.length; i++) {
args const signal = cacheSignals[i]() // try to get a signal from registered functions
) if (signal) {
} else { // We are in a React Server render and can consult the React cache signal to determine if logs
// First we see if there is a cache signal for our current scope. If we're in a client render it'll // are now dimmable.
// come from the client React cacheSignal implementation. If we are in a server render it'll come from if (signal.aborted) {
// the server React cacheSignal implementation. Any particular console call will be in one, the other, or neither if (currentAbortedLogsStyle === HIDDEN_STYLE) {
// scope and these signals return null if you are out of scope so this can be called from a single global patch return
// 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)
} }
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 // We need to fall back to checking the work unit store for two reasons.
// TODO in the future we should be able to remove this once there is a runnable cache // 1. Client React does not yet implement cacheSignal (it always returns null)
// scope independent of actual React rendering. // 2. route.ts files aren't rendered with React but do have prerender semantics
const workUnitStore = workUnitAsyncStorage.getStore() // TODO in the future we should be able to remove this once there is a runnable cache
switch (workUnitStore?.type) { // scope independent of actual React rendering.
case 'prerender': const workUnitStore = workUnitAsyncStorage.getStore()
case 'prerender-runtime': switch (workUnitStore?.type) {
// These can be hit in a route handler. In the future we can use potential React.createCache API case 'prerender':
// to create a cache scope for arbitrary computation and can move over to cacheSignal exclusively. case 'prerender-runtime':
// fallthrough // These can be hit in a route handler. In the future we can use potential React.createCache API
case 'prerender-client': // to create a cache scope for arbitrary computation and can move over to cacheSignal exclusively.
// This is a react-dom/server render and won't have a cacheSignal until React adds this for the client world. // fallthrough
const renderSignal = workUnitStore.renderSignal case 'prerender-client':
if (renderSignal.aborted) { // This is a react-dom/server render and won't have a cacheSignal until React adds this for the client world.
return applyWithDimming.call( const renderSignal = workUnitStore.renderSignal
this, if (renderSignal.aborted) {
consoleStore, if (currentAbortedLogsStyle === HIDDEN_STYLE) {
originalMethod, return
methodName,
args
)
} else {
return originalMethod.apply(this, args)
} }
case 'prerender-legacy': return applyWithDimming.call(
case 'prerender-ppr': this,
case 'cache': consoleStore,
case 'unstable-cache': originalMethod,
case 'private-cache': methodName,
case 'request': args
case undefined: )
}
// 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) return originalMethod.apply(this, args)
default: }
workUnitStore satisfies never default:
} workUnitStore satisfies never
} }
} }
if (originalName) { 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 @@ ...@@ -3,23 +3,27 @@
import { use } from 'react' import { use } from 'react'
function log(outBadge: string, errBadge: string) { 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( console.log(
// eslint-disable-next-line no-useless-concat // 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', foo: 'just-some-object',
}) })
console.error(new Error(`${errBadge} /console: test`)) console.error(new Error(`${errBadge} /console-after-abort/client: test`))
console.assert( console.assert(
false, 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' 'template'
) )
console.assert( console.assert(
true, 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({ ...@@ -32,7 +36,9 @@ export default function ClientConsolePage({
outBadge: string outBadge: string
errBadge: 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) setTimeout(log.bind(null, outBadge, errBadge), 1)
use(data) use(data)
......
...@@ -9,7 +9,9 @@ export default async function ConsolePage() { ...@@ -9,7 +9,9 @@ export default async function ConsolePage() {
const outBadge = `:::${i}:out:::` const outBadge = `:::${i}:out:::`
const errBadge = `:::${i++}:err:::` const errBadge = `:::${i++}:err:::`
console.log(`${outBadge} logging before prerender abort`) console.log(
`${outBadge} /console-after-abort/client: logging before prerender abort`
)
return ( return (
<ClientConsolePage data={data} outBadge={outBadge} errBadge={errBadge} /> <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) { async function cachedConsoleCalls(outBadge: string, errBadge: string) {
'use cache' 'use cache'
console.info(`${outBadge} /console: template(one: %s, two: %s)`, 'one', 'two') console.info(`${outBadge} /console: template(one: %s, two: %s)`, 'one', 'two')
await 1
console.log( console.log(
// eslint-disable-next-line no-useless-concat // eslint-disable-next-line no-useless-concat
`${outBadge} /console: This is a console page` + `${outBadge} /console: This is a console page` +
". Don't match the codeframe." ". Don't match the codeframe."
) )
await 1
console.warn(`${errBadge} /console: not a template`, { console.warn(`${errBadge} /console: not a template`, {
foo: 'just-some-object', foo: 'just-some-object',
}) })
await 1
// TODO(veil): Assert on inspected errors once we sourcemap errors replayed from Cache environment. // TODO(veil): Assert on inspected errors once we sourcemap errors replayed from Cache environment.
// console.error(new Error('/console: test')) // console.error(new Error('/console: test'))
console.assert( console.assert(
...@@ -28,25 +29,20 @@ let i = 0 ...@@ -28,25 +29,20 @@ let i = 0
export default async function ConsolePage() { export default async function ConsolePage() {
const outBadge = `:::${i}:out:::` const outBadge = `:::${i}:out:::`
const errBadge = `:::${i++}:err:::` 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') console.info(`${outBadge} /console: template(one: %s, two: %s)`, 'one', 'two')
await 1
console.log( console.log(
// eslint-disable-next-line no-useless-concat // eslint-disable-next-line no-useless-concat
`${outBadge} /console: This is a console page` + `${outBadge} /console: This is a console page` +
". Don't match the codeframe." ". Don't match the codeframe."
) )
await 1
console.warn(`${errBadge} /console: not a template`, { console.warn(`${errBadge} /console: not a template`, {
foo: 'just-some-object', foo: 'just-some-object',
}) })
await 1
console.error(new Error(`${errBadge} /console: test`)) console.error(new Error(`${errBadge} /console: test`))
await 1
console.assert( console.assert(
false, false,
`${errBadge} /console: This is an assert message with a %s`, `${errBadge} /console: This is an assert message with a %s`,
...@@ -57,6 +53,7 @@ export default async function ConsolePage() { ...@@ -57,6 +53,7 @@ export default async function ConsolePage() {
`${errBadge} /console: This is an assert message without a template` `${errBadge} /console: This is an assert message without a template`
) )
await 1
await cachedConsoleCalls(outBadge, errBadge) await cachedConsoleCalls(outBadge, errBadge)
return null 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