Unverified Commit 36f50c5a authored by Hendrik Liebau's avatar Hendrik Liebau Committed by GitHub

Send dynamic validation errors to browser via WebSocket (#85818)

By sending the dynamic validation errors to the browser via WebSocket,
instead of rendering a validation outlet into the dynamic dev render
stream, we can avoid artificially delaying the spawned validation to
implicitly wait for the different chunks (static, runtime, and dynamic),
without blocking the dev render stream, and instead wait explicitly for
all chunks to accumulate.

Previously, we were using React's console replaying to get full fidelity
error stacks in the browser (including inspectable virtual server
modules). Now, we're using the same underlying mechanism by sending a
separate RSC stream through the WebSocket that contains only the errors.
In the browser, the received errors are then logged with
`console.error`, which also triggers that they're displayed in a
collapsed Redbox, as was the case before with the replaying.

---------
Co-authored-by: 's avatarJanka Uryga <lolzatu2@gmail.com>
parent bf543597
......@@ -943,5 +943,8 @@
"942": "Unexpected stream chunk while in Before stage",
"943": "getFlightStream should always receive a ReadableStream when using the edge runtime",
"944": "nodeStreamFromReadableStream cannot be used in the edge runtime",
"945": "createNodeStreamFromChunks cannot be used in the edge runtime"
"945": "createNodeStreamFromChunks cannot be used in the edge runtime",
"946": "Failed to deserialize errors.",
"947": "Expected `sendErrorsToBrowser` to be defined in renderOpts.",
"948": "Failed to serialize errors."
}
......@@ -545,6 +545,7 @@ export async function handler(
setCacheStatus: routerServerContext?.setCacheStatus,
setIsrStatus: routerServerContext?.setIsrStatus,
setReactDebugChannel: routerServerContext?.setReactDebugChannel,
sendErrorsToBrowser: routerServerContext?.sendErrorsToBrowser,
dir:
process.env.NEXT_RUNTIME === 'nodejs'
......
......@@ -38,12 +38,19 @@ import {
} from '../../../components/app-router-instance'
import { InvariantError } from '../../../../shared/lib/invariant-error'
import { getOrCreateDebugChannelReadableWriterPair } from '../../debug-channel'
// TODO: Explicitly import from client.browser (doesn't work with Webpack).
// eslint-disable-next-line import/no-extraneous-dependencies
import { createFromReadableStream as createFromReadableStreamBrowser } from 'react-server-dom-webpack/client'
import { findSourceMapURL } from '../../../app-find-source-map-url'
export interface StaticIndicatorState {
pathname: string | null
appIsrManifest: Record<string, boolean> | null
}
const createFromReadableStream =
createFromReadableStreamBrowser as (typeof import('react-server-dom-webpack/client.browser'))['createFromReadableStream']
let mostRecentCompilationHash: any = null
let __nextDevClientId = Math.round(Math.random() * 100 + Date.now())
let reloading = false
......@@ -504,6 +511,29 @@ export function processMessage(
dispatcher.onCacheIndicator(message.state)
return
}
case HMR_MESSAGE_SENT_TO_BROWSER.ERRORS_TO_SHOW_IN_BROWSER: {
createFromReadableStream<Error[]>(
new ReadableStream({
start(controller) {
controller.enqueue(message.serializedErrors)
controller.close()
},
}),
{ findSourceMapURL }
).then(
(errors) => {
for (const error of errors) {
console.error(error)
}
},
(err) => {
console.error(
new Error('Failed to deserialize errors.', { cause: err })
)
}
)
return
}
case HMR_MESSAGE_SENT_TO_BROWSER.MIDDLEWARE_CHANGES:
case HMR_MESSAGE_SENT_TO_BROWSER.CLIENT_CHANGES:
case HMR_MESSAGE_SENT_TO_BROWSER.SERVER_ONLY_CHANGES:
......
......@@ -223,6 +223,14 @@ function parseBinaryMessage(data: ArrayBuffer): HmrMessageSentToBrowser {
const messageType = view.getUint8(0)
switch (messageType) {
case HMR_MESSAGE_SENT_TO_BROWSER.ERRORS_TO_SHOW_IN_BROWSER: {
const serializedErrors = new Uint8Array(data, 1)
return {
type: HMR_MESSAGE_SENT_TO_BROWSER.ERRORS_TO_SHOW_IN_BROWSER,
serializedErrors,
}
}
case HMR_MESSAGE_SENT_TO_BROWSER.REACT_DEBUG_CHUNK: {
assertByteLength(data, 2)
const requestIdLength = view.getUint8(1)
......
......@@ -394,6 +394,7 @@ function processMessage(message: HmrMessageSentToBrowser) {
break
case HMR_MESSAGE_SENT_TO_BROWSER.CACHE_INDICATOR:
case HMR_MESSAGE_SENT_TO_BROWSER.REACT_DEBUG_CHUNK:
case HMR_MESSAGE_SENT_TO_BROWSER.ERRORS_TO_SHOW_IN_BROWSER:
// Only relevant for app router.
break
case HMR_MESSAGE_SENT_TO_BROWSER.MIDDLEWARE_CHANGES:
......
......@@ -124,6 +124,7 @@ export function pageBootstrap(assetPrefix: string) {
case HMR_MESSAGE_SENT_TO_BROWSER.REQUEST_CURRENT_ERROR_STATE:
case HMR_MESSAGE_SENT_TO_BROWSER.REQUEST_PAGE_METADATA:
case HMR_MESSAGE_SENT_TO_BROWSER.CACHE_INDICATOR:
case HMR_MESSAGE_SENT_TO_BROWSER.ERRORS_TO_SHOW_IN_BROWSER:
// Most of these action types are handled in
// src/client/dev/hot-reloader/pages/hot-reloader-pages.ts and
// src/client/dev/hot-reloader/app/hot-reloader-app.tsx
......
......@@ -116,6 +116,10 @@ export interface RenderOptsPartial {
htmlRequestId: string,
requestId: string
) => void
sendErrorsToBrowser?: (
errorsRscStream: ReadableStream<Uint8Array>,
htmlRequestId: string
) => void
nextExport?: boolean
nextConfigOutput?: 'standalone' | 'export'
onInstrumentationRequestError?: ServerOnInstrumentationRequestError
......
......@@ -126,6 +126,11 @@ import { recordMcpTelemetry } from '../mcp/mcp-telemetry-tracker'
import { getFileLogger } from './browser-logs/file-logger'
import type { ServerCacheStatus } from '../../next-devtools/dev-overlay/cache-indicator'
import type { Lockfile } from '../../build/lockfile'
import {
sendSerializedErrorsToClient,
sendSerializedErrorsToClientForHtmlRequest,
setErrorsRscStreamForHtmlRequest,
} from './serialized-errors'
const wsServer = new ws.Server({ noServer: true })
const isTestMode = !!(
......@@ -908,6 +913,16 @@ export async function createHotReloaderTurbopack(
} else {
onUpgrade(client, { isLegacyClient: true })
}
connectReactDebugChannelForHtmlRequest(
htmlRequestId,
sendToClient.bind(null, client)
)
sendSerializedErrorsToClientForHtmlRequest(
htmlRequestId,
sendToClient.bind(null, client)
)
} else {
clientsWithoutHtmlRequestId.add(client)
onUpgrade(client, { isLegacyClient: true })
......@@ -1099,13 +1114,6 @@ export async function createHotReloaderTurbopack(
}
sendToClient(client, syncMessage)
if (htmlRequestId) {
connectReactDebugChannelForHtmlRequest(
htmlRequestId,
sendToClient.bind(null, client)
)
}
})()
})
},
......@@ -1186,6 +1194,22 @@ export async function createHotReloaderTurbopack(
}
},
sendErrorsToBrowser(errorsRscStream, htmlRequestId) {
const client = clientsByHtmlRequestId.get(htmlRequestId)
if (client) {
// If the client is connected, we can send the errors immediately.
sendSerializedErrorsToClient(
errorsRscStream,
sendToClient.bind(null, client)
)
} else {
// Otherwise, store the errors stream so that we can send it when the
// client connects.
setErrorsRscStreamForHtmlRequest(htmlRequestId, errorsRscStream)
}
},
setHmrServerError(_error) {
// Not implemented yet.
},
......
......@@ -40,6 +40,7 @@ export const enum HMR_MESSAGE_SENT_TO_BROWSER {
// Binary messages:
REACT_DEBUG_CHUNK = 0,
ERRORS_TO_SHOW_IN_BROWSER = 1,
}
export const enum HMR_MESSAGE_SENT_TO_SERVER {
......@@ -157,6 +158,11 @@ export interface ReactDebugChunkMessage {
chunk: Uint8Array | null
}
export interface ErrorsToShowInBrowserMessage {
type: HMR_MESSAGE_SENT_TO_BROWSER.ERRORS_TO_SHOW_IN_BROWSER
serializedErrors: Uint8Array
}
export interface RequestCurrentErrorStateMessage {
type: HMR_MESSAGE_SENT_TO_BROWSER.REQUEST_CURRENT_ERROR_STATE
requestId: string
......@@ -189,6 +195,7 @@ export type HmrMessageSentToBrowser =
| ServerErrorMessage
| AppIsrManifestMessage
| DevToolsConfigMessage
| ErrorsToShowInBrowserMessage
| ReactDebugChunkMessage
| RequestCurrentErrorStateMessage
| RequestPageMetadataMessage
......@@ -235,6 +242,10 @@ export interface NextJsHotReloaderInterface {
htmlRequestId: string,
requestId: string
): void
sendErrorsToBrowser(
errorsRscStream: ReadableStream<Uint8Array>,
htmlRequestId: string
): void
getCompilationErrors(page: string): Promise<any[]>
onHMR(
req: IncomingMessage,
......
......@@ -115,6 +115,11 @@ import { recordMcpTelemetry } from '../mcp/mcp-telemetry-tracker'
import { getFileLogger } from './browser-logs/file-logger'
import type { ServerCacheStatus } from '../../next-devtools/dev-overlay/cache-indicator'
import type { Lockfile } from '../../build/lockfile'
import {
sendSerializedErrorsToClient,
sendSerializedErrorsToClientForHtmlRequest,
setErrorsRscStreamForHtmlRequest,
} from './serialized-errors'
const MILLISECONDS_IN_NANOSECOND = BigInt(1_000_000)
......@@ -646,6 +651,12 @@ export default class HotReloaderWebpack implements NextJsHotReloaderInterface {
htmlRequestId,
this.sendToClient.bind(this, client)
)
sendSerializedErrorsToClientForHtmlRequest(
htmlRequestId,
this.sendToClient.bind(this, client)
)
if (enableCacheComponents) {
const status = this.cacheStatusesByRequestId.get(htmlRequestId)
if (status) {
......@@ -1801,6 +1812,25 @@ export default class HotReloaderWebpack implements NextJsHotReloaderInterface {
}
}
public sendErrorsToBrowser(
errorsRscStream: ReadableStream<Uint8Array>,
htmlRequestId: string
): void {
const client = this.webpackHotMiddleware?.getClient(htmlRequestId)
if (client) {
// If the client is connected, we can send the errors immediately.
sendSerializedErrorsToClient(
errorsRscStream,
this.sendToClient.bind(this, client)
)
} else {
// Otherwise, store the errors stream so that we can send it when the
// client connects.
setErrorsRscStreamForHtmlRequest(htmlRequestId, errorsRscStream)
}
}
public async ensurePage({
page,
clientOnly,
......
......@@ -13,6 +13,17 @@ export function createBinaryHmrMessageData(
message: BinaryHmrMessageSentToBrowser
): Uint8Array {
switch (message.type) {
case HMR_MESSAGE_SENT_TO_BROWSER.ERRORS_TO_SHOW_IN_BROWSER: {
const { serializedErrors } = message
const totalLength = 1 + serializedErrors.length
const data = new Uint8Array(totalLength)
const view = new DataView(data.buffer)
view.setUint8(0, HMR_MESSAGE_SENT_TO_BROWSER.ERRORS_TO_SHOW_IN_BROWSER)
data.set(serializedErrors, 1)
return data
}
case HMR_MESSAGE_SENT_TO_BROWSER.REACT_DEBUG_CHUNK: {
const { requestId, chunk } = message
const requestIdBytes = textEncoder.encode(requestId)
......@@ -41,7 +52,7 @@ export function createBinaryHmrMessageData(
}
default: {
throw new InvariantError(
`Invalid binary HMR message of type ${message.type}`
`Invalid binary HMR message of type ${(message as any).type}`
)
}
}
......
......@@ -833,7 +833,7 @@ export default class DevServer extends Server {
// Ideally, we would want to compare the whole objects, but that is too expensive.
result.prerenderedRoutes?.length !== prerenderedRoutes?.length
) {
this.bundlerService.triggerHMR({
this.bundlerService.sendHmrMessage({
type: HMR_MESSAGE_SENT_TO_BROWSER.SERVER_COMPONENT_CHANGES,
hash: `generateStaticParams-${Date.now()}`,
})
......
import { streamToUint8Array } from '../stream-utils/node-web-streams-helper'
import {
HMR_MESSAGE_SENT_TO_BROWSER,
type HmrMessageSentToBrowser,
} from './hot-reloader-types'
const errorsRscStreamsByHtmlRequestId = new Map<
string,
ReadableStream<Uint8Array>
>()
export function sendSerializedErrorsToClient(
errorsRscStream: ReadableStream<Uint8Array>,
sendToClient: (message: HmrMessageSentToBrowser) => void
) {
streamToUint8Array(errorsRscStream).then(
(serializedErrors) => {
sendToClient({
type: HMR_MESSAGE_SENT_TO_BROWSER.ERRORS_TO_SHOW_IN_BROWSER,
serializedErrors,
})
},
(err) => {
console.error(new Error('Failed to serialize errors.', { cause: err }))
}
)
}
export function sendSerializedErrorsToClientForHtmlRequest(
htmlRequestId: string,
sendToClient: (message: HmrMessageSentToBrowser) => void
) {
const errorsRscStream = errorsRscStreamsByHtmlRequestId.get(htmlRequestId)
if (!errorsRscStream) {
return
}
errorsRscStreamsByHtmlRequestId.delete(htmlRequestId)
sendSerializedErrorsToClient(errorsRscStream, sendToClient)
}
export function setErrorsRscStreamForHtmlRequest(
htmlRequestId: string,
errorsRscStream: ReadableStream<Uint8Array>
) {
// TODO: Clean up after a timeout, in case the client never connects, e.g.
// when CURL'ing the page, or loading the page with JavaScript disabled etc.
errorsRscStreamsByHtmlRequestId.set(htmlRequestId, errorsRscStream)
}
export function deleteErrorsRscStreamForHtmlRequest(htmlRequestId: string) {
errorsRscStreamsByHtmlRequestId.delete(htmlRequestId)
}
......@@ -7,9 +7,8 @@ import { createRequestResponseMocks } from './mock-request'
import {
HMR_MESSAGE_SENT_TO_BROWSER,
type HmrMessageSentToBrowser,
type NextJsHotReloaderInterface,
} from '../dev/hot-reloader-types'
import type { ReactDebugChannelForBrowser } from '../dev/debug-channel'
import type { ServerCacheStatus } from '../../next-devtools/dev-overlay/cache-indicator'
/**
* The DevBundlerService provides an interface to perform tasks with the
......@@ -17,6 +16,10 @@ import type { ServerCacheStatus } from '../../next-devtools/dev-overlay/cache-in
*/
export class DevBundlerService {
public appIsrManifestInner: InstanceType<typeof LRUCache<boolean>>
public close: NextJsHotReloaderInterface['close']
public setCacheStatus: NextJsHotReloaderInterface['setCacheStatus']
public setReactDebugChannel: NextJsHotReloaderInterface['setReactDebugChannel']
public sendErrorsToBrowser: NextJsHotReloaderInterface['sendErrorsToBrowser']
constructor(
private readonly bundler: DevBundler,
......@@ -29,6 +32,14 @@ export class DevBundlerService {
return 16
}
)
const { hotReloader } = bundler
this.close = hotReloader.close.bind(hotReloader)
this.setCacheStatus = hotReloader.setCacheStatus.bind(hotReloader)
this.setReactDebugChannel =
hotReloader.setReactDebugChannel.bind(hotReloader)
this.sendErrorsToBrowser = hotReloader.sendErrorsToBrowser.bind(hotReloader)
}
public ensurePage: typeof this.bundler.hotReloader.ensurePage = async (
......@@ -99,13 +110,6 @@ export class DevBundlerService {
return serializableManifest
}
public setCacheStatus(
status: ServerCacheStatus,
htmlRequestId: string
): void {
this.bundler.hotReloader.setCacheStatus(status, htmlRequestId)
}
public setIsrStatus(key: string, value: boolean | undefined) {
if (value === undefined) {
this.appIsrManifestInner.remove(key)
......@@ -125,23 +129,7 @@ export class DevBundlerService {
})
}
public setReactDebugChannel(
debugChannel: ReactDebugChannelForBrowser,
htmlRequestId: string,
requestId: string
): void {
this.bundler.hotReloader.setReactDebugChannel(
debugChannel,
htmlRequestId,
requestId
)
}
public close() {
this.bundler.hotReloader.close()
}
public triggerHMR(message: HmrMessageSentToBrowser) {
public sendHmrMessage(message: HmrMessageSentToBrowser) {
this.bundler.hotReloader.send(message)
}
}
......@@ -730,6 +730,8 @@ export async function initialize(opts: {
setReactDebugChannel: config.experimental.reactDebugChannel
? devBundlerService?.setReactDebugChannel.bind(devBundlerService)
: undefined,
sendErrorsToBrowser:
devBundlerService?.sendErrorsToBrowser.bind(devBundlerService),
}
const logError = async (
......
......@@ -45,6 +45,10 @@ export type RouterServerContext = Record<
requestId: string
) => void
setCacheStatus?: (status: ServerCacheStatus, htmlRequestId: string) => void
sendErrorsToBrowser?: (
errorsRscStream: ReadableStream<Uint8Array>,
htmlRequestId: string
) => void
}
>
......
......@@ -96,11 +96,11 @@ export function streamFromBuffer(chunk: Buffer): ReadableStream<Uint8Array> {
})
}
export async function streamToBuffer(
async function streamToChunks(
stream: ReadableStream<Uint8Array>
): Promise<Buffer> {
): Promise<Array<Uint8Array>> {
const reader = stream.getReader()
const chunks: Uint8Array[] = []
const chunks: Array<Uint8Array> = []
while (true) {
const { done, value } = await reader.read()
......@@ -111,7 +111,30 @@ export async function streamToBuffer(
chunks.push(value)
}
return Buffer.concat(chunks)
return chunks
}
function concatUint8Arrays(chunks: Array<Uint8Array>): Uint8Array {
const totalLength = chunks.reduce((sum, chunk) => sum + chunk.length, 0)
const result = new Uint8Array(totalLength)
let offset = 0
for (const chunk of chunks) {
result.set(chunk, offset)
offset += chunk.length
}
return result
}
export async function streamToUint8Array(
stream: ReadableStream<Uint8Array>
): Promise<Uint8Array> {
return concatUint8Arrays(await streamToChunks(stream))
}
export async function streamToBuffer(
stream: ReadableStream<Uint8Array>
): Promise<Buffer> {
return Buffer.concat(await streamToChunks(stream))
}
export async function streamToString(
......
......@@ -30,7 +30,6 @@ describe('Cache Components Dev Errors', () => {
"stack": [
"Page app/error/page.tsx (2:23)",
"Page <anonymous>",
"ReportValidation <anonymous>",
],
}
`)
......@@ -60,7 +59,6 @@ describe('Cache Components Dev Errors', () => {
"stack": [
"Page app/error/page.tsx (2:23)",
"Page <anonymous>",
"ReportValidation <anonymous>",
],
}
`)
......@@ -118,7 +116,6 @@ describe('Cache Components Dev Errors', () => {
| ^",
"stack": [
"Page app/no-accessed-data/page.js (2:9)",
"ReportValidation <anonymous>",
],
}
`)
......
......@@ -74,7 +74,6 @@ describe('Cache Components Fallback Validation', () => {
| ^",
"stack": [
"Page app/partial/[top]/unwrapped/[bottom]/page.tsx (6:26)",
"ReportValidation <anonymous>",
],
}
`)
......@@ -103,7 +102,6 @@ describe('Cache Components Fallback Validation', () => {
| ^",
"stack": [
"Page app/partial/[top]/unwrapped/[bottom]/page.tsx (6:26)",
"ReportValidation <anonymous>",
],
}
`)
......@@ -135,7 +133,6 @@ describe('Cache Components Fallback Validation', () => {
| ^",
"stack": [
"Page app/partial/[top]/unwrapped/[bottom]/page.tsx (6:26)",
"ReportValidation <anonymous>",
],
}
`)
......@@ -164,7 +161,6 @@ describe('Cache Components Fallback Validation', () => {
| ^",
"stack": [
"Page app/partial/[top]/unwrapped/[bottom]/page.tsx (6:26)",
"ReportValidation <anonymous>",
],
}
`)
......@@ -196,7 +192,6 @@ describe('Cache Components Fallback Validation', () => {
| ^",
"stack": [
"Page app/partial/[top]/unwrapped/[bottom]/page.tsx (6:26)",
"ReportValidation <anonymous>",
],
}
`)
......@@ -225,7 +220,6 @@ describe('Cache Components Fallback Validation', () => {
| ^",
"stack": [
"Page app/partial/[top]/unwrapped/[bottom]/page.tsx (6:26)",
"ReportValidation <anonymous>",
],
}
`)
......@@ -261,7 +255,6 @@ describe('Cache Components Fallback Validation', () => {
| ^",
"stack": [
"Layout app/none/[top]/wrapped/layout.tsx (10:3)",
"ReportValidation <anonymous>",
],
}
`)
......@@ -290,7 +283,6 @@ describe('Cache Components Fallback Validation', () => {
| ^",
"stack": [
"Layout app/none/[top]/wrapped/layout.tsx (10:3)",
"ReportValidation <anonymous>",
],
}
`)
......@@ -322,7 +314,6 @@ describe('Cache Components Fallback Validation', () => {
| ^",
"stack": [
"Layout app/none/[top]/wrapped/layout.tsx (10:3)",
"ReportValidation <anonymous>",
],
}
`)
......@@ -351,7 +342,6 @@ describe('Cache Components Fallback Validation', () => {
| ^",
"stack": [
"Layout app/none/[top]/wrapped/layout.tsx (10:3)",
"ReportValidation <anonymous>",
],
}
`)
......@@ -383,7 +373,6 @@ describe('Cache Components Fallback Validation', () => {
| ^",
"stack": [
"Layout app/none/[top]/wrapped/layout.tsx (10:3)",
"ReportValidation <anonymous>",
],
}
`)
......@@ -412,7 +401,6 @@ describe('Cache Components Fallback Validation', () => {
| ^",
"stack": [
"Layout app/none/[top]/wrapped/layout.tsx (10:3)",
"ReportValidation <anonymous>",
],
}
`)
......@@ -444,7 +432,6 @@ describe('Cache Components Fallback Validation', () => {
| ^",
"stack": [
"Layout app/none/[top]/unwrapped/layout.tsx (8:3)",
"ReportValidation <anonymous>",
],
}
`)
......@@ -473,7 +460,6 @@ describe('Cache Components Fallback Validation', () => {
| ^",
"stack": [
"Layout app/none/[top]/unwrapped/layout.tsx (8:3)",
"ReportValidation <anonymous>",
],
}
`)
......@@ -505,7 +491,6 @@ describe('Cache Components Fallback Validation', () => {
| ^",
"stack": [
"Layout app/none/[top]/unwrapped/layout.tsx (8:3)",
"ReportValidation <anonymous>",
],
}
`)
......@@ -534,7 +519,6 @@ describe('Cache Components Fallback Validation', () => {
| ^",
"stack": [
"Layout app/none/[top]/unwrapped/layout.tsx (8:3)",
"ReportValidation <anonymous>",
],
}
`)
......@@ -566,7 +550,6 @@ describe('Cache Components Fallback Validation', () => {
| ^",
"stack": [
"Layout app/none/[top]/unwrapped/layout.tsx (8:3)",
"ReportValidation <anonymous>",
],
}
`)
......@@ -595,7 +578,6 @@ describe('Cache Components Fallback Validation', () => {
| ^",
"stack": [
"Layout app/none/[top]/unwrapped/layout.tsx (8:3)",
"ReportValidation <anonymous>",
],
}
`)
......
......@@ -112,12 +112,7 @@ describe('react-performance-track', () => {
const track = await browser.eval('window.reactServerRequests.getSnapshot()')
// TODO: Should include "draftMode [Prefetchable]".
expect(track).toEqual([
{
name: '\u200b',
properties: [],
},
])
expect(track).toEqual([])
})
it('should show headers', async () => {
......
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