Unverified Commit 2685731b authored by Will Binns-Smith's avatar Will Binns-Smith Committed by GitHub

Bundle Analyzer Agent Skill: Add server Markdown eval (#99179)

## Summary

Add a Markdown/highlighting eval that preserves rendered release notes
while reducing cold browser JavaScript below 150,000 bytes.

Use the shared bundle-optimizer measurement hooks and require invocation
of `next-bundle-optimizer` in the skills treatment.

## Verification

- Root and eval TypeScript checks pass on the rebased stack.
- Formatting, focused ESLint, and local setup/transcript checks pass.
- Remote agent evals not rerun after these updates.
- Bootstrap build blocked by a registry 401 while resolving
`@pnpm/exe@10.33.0`.

<!-- NEXT_JS_LLM -->

<!-- fleet d7bdbc2b-b0bc-4591-99eb-dccb0fa49c73 -->

<!-- fleet ea768377-d71c-4a15-90b2-f4d50d4f6a1c -->
Co-authored-by: 's avatarvercel-fleet-prod[bot] <318278635+vercel-fleet-prod[bot]@users.noreply.github.com>
Co-authored-by: 's avatarWill Binns-Smith <755844+wbinnssmith@users.noreply.github.com>
parent 7ca8d66d
...@@ -110,15 +110,16 @@ Fixtures opt into before/after JavaScript measurements with `"browserJs": true` ...@@ -110,15 +110,16 @@ Fixtures opt into before/after JavaScript measurements with `"browserJs": true`
in `eval.config.json`. The runner adds measurement hooks only to experiments in `eval.config.json`. The runner adds measurement hooks only to experiments
containing those fixtures. containing those fixtures.
The dynamic-editor bundle-optimizer fixture measures cold Turbopack production The dynamic-editor and Markdown bundle-optimizer fixtures measure cold Turbopack
loads in headless Chromium before and after the agent runs. It uses production loads in headless Chromium before and after the agent runs. Each uses
plain `next build`, disabled browser caching, and fresh browser contexts. Passing plain `next build`, disabled browser caching, and fresh browser contexts. Passing
requires fewer compressed JavaScript response-body bytes than the baseline and a requires fewer compressed JavaScript response-body bytes than the baseline and a
rounded byte budget of 200,000 bytes. rounded byte budget: 200,000 bytes for the editor and 150,000 bytes for Markdown.
The editor must stay unloaded until interaction, preload on pointer hover, and The editor must stay unloaded until interaction, preload on pointer hover, and
stay editable. CodeMirror responses are identified by its stay editable. CodeMirror responses are identified by its
`cm-content` runtime class. `cm-content` runtime class. Markdown must preserve the rendered headings, bulleted
list, and highlighted JavaScript code block.
`lib/next-test-utils.mjs` owns production builds and server cleanup. `lib/next-test-utils.mjs` owns production builds and server cleanup.
Shared utilities in `lib/bundle-optimizer/browser-js.ts` handle browser cleanup, Shared utilities in `lib/bundle-optimizer/browser-js.ts` handle browser cleanup,
......
...@@ -71,5 +71,11 @@ ...@@ -71,5 +71,11 @@
"browserJs": true, "browserJs": true,
"publish": false, "publish": false,
"timeout": 1800 "timeout": 1800
},
"agent-060-bundle-optimizer-rsc-markdown": {
"skills": ["next-bundle-optimizer"],
"browserJs": true,
"publish": false,
"timeout": 1800
} }
} }
import { beforeAll, expect, test } from 'vitest'
import { runBrowserJsEval } from '../../lib/bundle-optimizer/browser-js.js'
import { measureBrowserJs } from './__eval__/measure-browser-js.js'
import type { BrowserMeasurement as Measurement } from '../../lib/bundle-optimizer/browser-js.js'
type BrowserDetails = {
content: { formattedReleaseNotes: boolean }
markdownSources: string[]
}
type BrowserMeasurement = Measurement<BrowserDetails>
let before: BrowserMeasurement
let after: BrowserMeasurement
const budgetBytes = 150_000
beforeAll(async () => {
const measured = await runBrowserJsEval<BrowserDetails>(
budgetBytes,
measureBrowserJs
)
before = measured.before
after = measured.after
}, 360_000)
test('builds and preserves the rendered release notes', () => {
expect(after.content.formattedReleaseNotes).toBe(true)
})
test('keeps Markdown rendering modules out of browser chunks', () => {
expect(before.markdownSources).toEqual(
expect.arrayContaining([expect.stringMatching(/\/react-markdown\//)])
)
expect(after.markdownSources).toEqual([])
})
test('keeps initial browser JavaScript under 150,000 bytes', () => {
expect(after.initial.encodedBytes).toBeGreaterThan(0)
expect(after.initial.encodedBytes).toBeLessThanOrEqual(budgetBytes)
})
test('reduces initial browser JavaScript', () => {
expect(after.initial.encodedBytes).toBeLessThan(before.initial.encodedBytes)
})
The `/` route in this Next.js app has become slow on first load. Optimize its browser initial-load performance without changing the visible release notes or their formatting.
The Markdown heading, bulleted list, and highlighted JavaScript code block must render exactly as useful display content; users do not edit this content in the browser.
Use the tools and documentation available in the project, verify the optimization, and leave the app in a production-buildable state.
import assert from 'node:assert/strict'
import { readdir, readFile } from 'node:fs/promises'
import { join } from 'node:path'
import { expect } from 'playwright/test'
import {
withProductionBrowser,
withMeasuredPage,
} from '../../../lib/bundle-optimizer/browser-js.js'
type SourceMap = {
sources?: string[]
sections?: { map: SourceMap }[]
}
function sourceMapSources(map: SourceMap): string[] {
assert(Array.isArray(map.sources) || Array.isArray(map.sections))
return [
...(map.sources ?? []),
...(map.sections ?? []).flatMap(({ map }) => sourceMapSources(map)),
]
}
export async function measureBrowserJs() {
return withProductionBrowser(async (browser, url) => {
const staticDir = join('.next', 'static')
const maps = (await readdir(staticDir, { recursive: true })).filter(
(file) => file.endsWith('.js.map')
)
assert(maps.length > 0, 'Missing browser JavaScript source maps')
const sources = (
await Promise.all(
maps.map(async (file) =>
sourceMapSources(
JSON.parse(await readFile(join(staticDir, file), 'utf8'))
)
)
)
).flat()
assert(sources.length > 0, 'Missing browser JavaScript source-map sources')
const markdownSources = [
...new Set(sources.map((source) => source.replaceAll('\\', '/'))),
].filter((source) =>
/\/node_modules\/(?:.*\/)?(?:react-markdown|remark-gfm|rehype-highlight|micromark(?:-[^/]+)?|mdast-util-[^/]+|lowlight|highlight\.js)\//.test(
source
)
)
if (process.env.NEXT_EVAL_BROWSER_JS_PHASE === 'before') {
assert(
markdownSources.some((source) => /\/react-markdown\//.test(source)),
'Baseline browser source maps must contain the Markdown renderer'
)
}
return withMeasuredPage(browser, url, async (page, snapshot) => {
await expect(
page.getByRole('heading', { name: 'Release Notes', level: 1 })
).toBeVisible()
await expect(
page.getByRole('heading', { name: 'Quarterly plan', level: 2 })
).toBeVisible()
await expect(page.locator('article ul')).toBeVisible()
await expect(page.locator('article ul > li')).toHaveText([
'Ship the browser performance work',
'Keep the release notes readable',
])
const code = page.locator('article pre > code')
await expect(code).toBeVisible()
await expect(code).toHaveText("const target = 'fast initial load'\n")
await expect(code).toHaveClass(/language-js/)
await expect(code.locator('.hljs-keyword')).toHaveText('const')
await expect(code.locator('.hljs-string')).toHaveText(
"'fast initial load'"
)
return {
initial: await snapshot(),
content: { formattedReleaseNotes: true },
markdownSources,
}
})
})
}
import type { ReactNode } from 'react'
export default function RootLayout({ children }: { children: ReactNode }) {
return (
<html lang="en">
<body>{children}</body>
</html>
)
}
import { ReleaseNotes } from '../components/release-notes'
export default function Page() {
return (
<main>
<h1>Release Notes</h1>
<ReleaseNotes />
</main>
)
}
'use client'
import ReactMarkdown from 'react-markdown'
import rehypeHighlight from 'rehype-highlight'
import remarkGfm from 'remark-gfm'
const notes = `## Quarterly plan
- Ship the browser performance work
- Keep the release notes readable
\`\`\`js
const target = 'fast initial load'
\`\`\`
`
export function ReleaseNotes() {
return (
<article>
<ReactMarkdown
remarkPlugins={[remarkGfm]}
rehypePlugins={[rehypeHighlight]}
>
{notes}
</ReactMarkdown>
</article>
)
}
import type { NextConfig } from 'next'
const nextConfig: NextConfig = {
productionBrowserSourceMaps: true,
}
export default nextConfig
{
"name": "agent-060-bundle-optimizer-rsc-markdown",
"private": true,
"type": "module",
"scripts": {
"build": "next build",
"dev": "next dev",
"start": "next start"
},
"dependencies": {
"next": "^16",
"react": "19.1.0",
"react-dom": "19.1.0",
"react-markdown": "^10.1.0",
"rehype-highlight": "^7.0.2",
"remark-gfm": "^4.0.1"
},
"devDependencies": {
"@types/node": "^20",
"@types/react": "^19",
"@types/react-dom": "^19",
"typescript": "^5",
"vitest": "^3.1.3",
"@playwright/test": "1.61.0"
}
}
{
"compilerOptions": {
"target": "ES2017",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": true,
"noEmit": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "bundler",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "react-jsx",
"incremental": true,
"plugins": [
{
"name": "next"
}
]
},
"include": [
"next-env.d.ts",
".next/types/**/*.ts",
"**/*.ts",
"**/*.tsx",
".next/dev/types/**/*.ts"
],
"exclude": ["node_modules", "EVAL.ts", "__eval__"]
}
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