From fc32c5e6deb256e81636b7ef591cdb9e647904ec Mon Sep 17 00:00:00 2001 From: Luca Forstner Date: Tue, 9 Apr 2024 15:35:19 +0200 Subject: [PATCH 01/13] Add `clientTraceMetadata` experimental option to propagate tracing data to the client --- .../crates/next-core/src/next_config.rs | 1 + packages/next/src/build/webpack-config.ts | 1 + packages/next/src/export/index.ts | 1 + .../next/src/server/app-render/app-render.tsx | 6 ++++ .../make-get-server-inserted-html.tsx | 9 +++++- packages/next/src/server/app-render/types.ts | 1 + packages/next/src/server/base-server.ts | 2 ++ packages/next/src/server/config-schema.ts | 1 + packages/next/src/server/config-shared.ts | 7 +++++ packages/next/src/server/lib/trace/tracer.ts | 28 +++++++++++++++++++ 10 files changed, 56 insertions(+), 1 deletion(-) diff --git a/packages/next-swc/crates/next-core/src/next_config.rs b/packages/next-swc/crates/next-core/src/next_config.rs index 93cf4bda13ae7..cde9514d27b97 100644 --- a/packages/next-swc/crates/next-core/src/next_config.rs +++ b/packages/next-swc/crates/next-core/src/next_config.rs @@ -514,6 +514,7 @@ pub struct ExperimentalConfig { gzip_size: Option, instrumentation_hook: Option, + client_trace_metadata: Option, large_page_data_bytes: Option, logging: Option, memory_based_workers_count: Option, diff --git a/packages/next/src/build/webpack-config.ts b/packages/next/src/build/webpack-config.ts index d0a5f322bc504..262b0c718f9bb 100644 --- a/packages/next/src/build/webpack-config.ts +++ b/packages/next/src/build/webpack-config.ts @@ -2057,6 +2057,7 @@ export default async function getBaseWebpackConfig( emotion: config.compiler?.emotion, modularizeImports: config.modularizeImports, imageLoaderFile: config.images.loaderFile, + clientTraceMetadata: config.experimental.clientTraceMetadata, }) const cache: any = { diff --git a/packages/next/src/export/index.ts b/packages/next/src/export/index.ts index f428fcfcf8ec6..b667808c6c8ec 100644 --- a/packages/next/src/export/index.ts +++ b/packages/next/src/export/index.ts @@ -421,6 +421,7 @@ export async function exportAppImpl( experimental: { isAppPPREnabled: checkIsAppPPREnabled(nextConfig.experimental.ppr), swrDelta: nextConfig.experimental.swrDelta, + clientTraceMetadata: nextConfig.experimental.clientTraceMetadata === true, }, } diff --git a/packages/next/src/server/app-render/app-render.tsx b/packages/next/src/server/app-render/app-render.tsx index 5d9b7c7d2e0d2..b5a0a1478edf8 100644 --- a/packages/next/src/server/app-render/app-render.tsx +++ b/packages/next/src/server/app-render/app-render.tsx @@ -995,6 +995,9 @@ async function renderToHTMLOrFlightImpl( renderServerInsertedHTML, serverCapturedErrors: allCapturedErrors, basePath: renderOpts.basePath, + traceData: renderOpts.experimental.clientTraceMetadata + ? getTracer().getTracePropagationData() + : [], }) const renderer = createStaticRenderer({ @@ -1319,6 +1322,9 @@ async function renderToHTMLOrFlightImpl( renderServerInsertedHTML, serverCapturedErrors: [], basePath: renderOpts.basePath, + traceData: renderOpts.experimental.clientTraceMetadata + ? getTracer().getTracePropagationData() + : [], }), serverInsertedHTMLToHead: true, validateRootLayout, diff --git a/packages/next/src/server/app-render/make-get-server-inserted-html.tsx b/packages/next/src/server/app-render/make-get-server-inserted-html.tsx index 976733f19644f..e9d52e7342650 100644 --- a/packages/next/src/server/app-render/make-get-server-inserted-html.tsx +++ b/packages/next/src/server/app-render/make-get-server-inserted-html.tsx @@ -9,15 +9,18 @@ import { renderToReadableStream } from 'react-dom/server.edge' import { streamToString } from '../stream-utils/node-web-streams-helper' import { RedirectStatusCode } from '../../client/components/redirect-status-code' import { addPathPrefix } from '../../shared/lib/router/utils/add-path-prefix' +import type { ClientTraceDataEntry } from '../lib/trace/tracer' export function makeGetServerInsertedHTML({ polyfills, renderServerInsertedHTML, serverCapturedErrors, + traceData, basePath, }: { polyfills: JSX.IntrinsicElements['script'][] renderServerInsertedHTML: () => React.ReactNode + traceData: ClientTraceDataEntry[] serverCapturedErrors: Error[] basePath: string }) { @@ -81,7 +84,11 @@ export function makeGetServerInsertedHTML({ return