Skip to content

Commit 3504889

Browse files
iamkdmischnic
authored andcommitted
Pass only required props to NonIndex (#77685)
<!-- Thanks for opening a PR! Your contribution is much appreciated. To make sure your PR is handled as smoothly as possible we request that you follow the checklist sections below. Choose the right checklist for the change(s) that you're making: ## For Contributors ### Improving Documentation - Run `pnpm prettier-fix` to fix formatting issues before opening the PR. - Read the Docs Contribution Guide to ensure your contribution follows the docs guidelines: https://nextjs.org/docs/community/contribution-guide ### Adding or Updating Examples - The "examples guidelines" are followed from our contributing doc https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md - Make sure the linting passes by running `pnpm build && pnpm lint`. See https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md ### Fixing a bug - Related issues linked using `fixes #number` - Tests added. See: https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs - Errors have a helpful link attached, see https://github.com/vercel/next.js/blob/canary/contributing.md ### Adding a feature - Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. (A discussion must be opened, see https://github.com/vercel/next.js/discussions/new?category=ideas) - Related issues/discussions are linked using `fixes #number` - e2e tests added (https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs) - Documentation added - Telemetry added. In case of a feature if it's used or not. - Errors have a helpful link attached, see https://github.com/vercel/next.js/blob/canary/contributing.md ## For Maintainers - Minimal description (aim for explaining to someone not on the team to understand the PR) - When linking to a Slack thread, you might want to share details of the conclusion - Link both the Linear (Fixes NEXT-xxx) and the GitHub issues - Add review comments if necessary to explain to the reviewer the logic behind a change --> ### What? Pass only the required props to the `NonIndex` component to reduce the response size. The numbers below are from a starter project with some of the starter HTML removed: On navigation (clicking a `<Link href="/route" />`): | | Before | After | | ------ | ------ | ----- | | Transferred over network | 9.2 kB | **4.4 kB** | | Resource size | 43.6 kB | **18.0 kB** | On page load/refresh: | | Before | After | | ------ | ------ | ----- | | Transferred over network | 12.5 kB | **8.9 kB** | | Resource size | 52.2 kB | **33.8 kB** | ### Why? I have noticed seemingly empty pages having a pretty sizeable RSC response. It turned out that because the whole context was passed to `NonIndex`, it got serialized into the RSC response on each navigation.
1 parent 6da2963 commit 3504889

File tree

2 files changed

+29
-14
lines changed

2 files changed

+29
-14
lines changed

packages/next/src/server/app-render/app-render.tsx

+27-8
Original file line numberDiff line numberDiff line change
@@ -433,13 +433,20 @@ function makeGetDynamicParamFromSegment(
433433
}
434434
}
435435

436-
function NonIndex({ ctx }: { ctx: AppRenderContext }) {
437-
const is404Page = ctx.pagePath === '/404'
438-
const isInvalidStatusCode =
439-
typeof ctx.res.statusCode === 'number' && ctx.res.statusCode > 400
436+
function NonIndex({
437+
pagePath,
438+
statusCode,
439+
isAction,
440+
}: {
441+
pagePath: string
442+
statusCode: number | undefined
443+
isAction: boolean
444+
}) {
445+
const is404Page = pagePath === '/404'
446+
const isInvalidStatusCode = typeof statusCode === 'number' && statusCode > 400
440447

441448
// Only render noindex for page request, skip for server actions
442-
if (!ctx.isAction && (is404Page || isInvalidStatusCode)) {
449+
if (!isAction && (is404Page || isInvalidStatusCode)) {
443450
return <meta name="robots" content="noindex" />
444451
}
445452
return null
@@ -527,7 +534,11 @@ async function generateDynamicRSCPayload(
527534
rscHead: (
528535
<React.Fragment key={flightDataPathHeadKey}>
529536
{/* noindex needs to be blocking */}
530-
<NonIndex ctx={ctx} />
537+
<NonIndex
538+
pagePath={ctx.pagePath}
539+
statusCode={ctx.res.statusCode}
540+
isAction={ctx.isAction}
541+
/>
531542
{/* Adding requestId as react key to make metadata remount for each render */}
532543
<ViewportTree key={requestId} />
533544
{StreamingMetadata ? <StreamingMetadata /> : null}
@@ -859,7 +870,11 @@ async function getRSCPayload(
859870

860871
const initialHead = (
861872
<React.Fragment key={flightDataPathHeadKey}>
862-
<NonIndex ctx={ctx} />
873+
<NonIndex
874+
pagePath={ctx.pagePath}
875+
statusCode={ctx.res.statusCode}
876+
isAction={ctx.isAction}
877+
/>
863878
<ViewportTree key={ctx.requestId} />
864879
<StaticMetadata />
865880
</React.Fragment>
@@ -961,7 +976,11 @@ async function getErrorRSCPayload(
961976

962977
const initialHead = (
963978
<React.Fragment key={flightDataPathHeadKey}>
964-
<NonIndex ctx={ctx} />
979+
<NonIndex
980+
pagePath={ctx.pagePath}
981+
statusCode={ctx.res.statusCode}
982+
isAction={ctx.isAction}
983+
/>
965984
{/* Adding requestId as react key to make metadata remount for each render */}
966985
<ViewportTree key={requestId} />
967986
{process.env.NODE_ENV === 'development' && (

test/e2e/opentelemetry/instrumentation/opentelemetry.test.ts

+2-6
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const EXTERNAL = {
1212
const COLLECTOR_PORT = 9001
1313

1414
describe('opentelemetry', () => {
15-
const { next, skipped, isNextDev } = nextTestSetup({
15+
const { next, skipped } = nextTestSetup({
1616
files: __dirname,
1717
skipDeployment: true,
1818
dependencies: require('./package.json').dependencies,
@@ -169,11 +169,7 @@ describe('opentelemetry', () => {
169169
},
170170
{
171171
attributes: {
172-
'next.clientComponentLoadCount': isNextDev
173-
? // In dev, additional client components are being loaded
174-
// due to RSC props being deserialized.
175-
11
176-
: 8,
172+
'next.clientComponentLoadCount': 8,
177173
'next.span_type':
178174
'NextNodeServer.clientComponentLoading',
179175
},

0 commit comments

Comments
 (0)