Unverified Commit f3c36c33 authored by vercel-fleet-prod[bot]'s avatar vercel-fleet-prod[bot] Committed by GitHub

Add browser initial load optimizer eval

Co-authored-by: 's avatarWill Binns-Smith <755844+wbinnssmith@users.noreply.github.com>
parent a8ec4d11
...@@ -37,5 +37,9 @@ ...@@ -37,5 +37,9 @@
"agentFeedback": true, "agentFeedback": true,
"publish": false, "publish": false,
"timeout": 900 "timeout": 900
},
"agent-059-browser-initial-load-optimizer": {
"skills": ["next-browser-initial-load-optimizer"],
"timeout": 1800
} }
} }
import { expect, test } from 'vitest'
import { transcriptPath } from '@vercel/agent-eval/eval'
import { readFileSync, readdirSync } from 'node:fs'
import { join } from 'node:path'
function sourceFiles(directory = process.cwd()): string[] {
const files: string[] = []
for (const entry of readdirSync(directory, { withFileTypes: true })) {
if (['.next', 'node_modules'].includes(entry.name)) continue
const path = join(directory, entry.name)
if (entry.isDirectory()) files.push(...sourceFiles(path))
else if (/\.[cm]?[jt]sx?$/.test(entry.name)) files.push(path)
}
return files
}
function sourceContents(): string {
return ['app', 'components']
.flatMap((directory) => sourceFiles(join(process.cwd(), directory)))
.map((path) => readFileSync(path, 'utf8'))
.join('\n')
}
test('loads the CodeMirror editor through an async boundary', () => {
const sources = sourceContents()
expect(sources).toContain('Open formula editor')
expect(sources).toMatch(/@uiw\/react-codemirror/)
expect(sources).not.toMatch(
/import\s+(?!type\b)[^'";]+from\s*['"].*heavy-editor['"]/
)
expect(sources).toMatch(/dynamic\s*\(/)
expect(sources).toMatch(/import\s*\(\s*['"].*heavy-editor['"]\s*\)/)
})
test('preloads the editor on pointer and keyboard intent', () => {
const sources = sourceContents()
expect(sources).toMatch(
/on(?:Mouse|Pointer)Enter\s*=\s*\{[\s\S]{0,160}(?:preload|load|import)/
)
expect(sources).toMatch(
/onFocus\s*=\s*\{[\s\S]{0,160}(?:preload|load|import)/
)
})
test('preserves the click-gated JavaScript editor', () => {
const sources = sourceContents()
expect(sources).toContain('Formula Workspace')
expect(sources).toContain('Open formula editor')
expect(sources).toContain('revenue - costs')
expect(sources).toMatch(/@uiw\/react-codemirror/)
expect(sources).toMatch(/@codemirror\/lang-javascript/)
expect(sources).toMatch(/useState\s*\(\s*false\s*\)/)
expect(sources).toMatch(/\{\s*\w+\s*&&\s*<\w+/)
})
test('uses analyzer evidence and verifies the optimization', () => {
const transcript = readFileSync(transcriptPath(), 'utf8')
const analyzerRuns = transcript.match(/experimental-analyze/g) ?? []
expect(transcript.trim()).not.toBe('')
expect(analyzerRuns.length).toBeGreaterThanOrEqual(2)
expect(transcript).toMatch(/(?:query|initial[_ -]?load|scope)/i)
expect(transcript).toMatch(/(?:CodeMirror|@uiw\/react-codemirror)/i)
expect(transcript).toMatch(/(?:package|source|importer)/i)
expect(transcript).toMatch(
/(?:next\s+build|pnpm\s+(?:run\s+)?build|npm\s+run\s+build)/i
)
})
The `/` route in this Next.js app has become slow on first load. Optimize its browser initial-load performance without changing the visible content or removing any features.
The formula editor must still open from the button, support JavaScript editing, and feel responsive when the user signals that they are about to open it with either a pointer or keyboard.
Use the tools and documentation available in the project, verify the optimization, and leave the app in a production-buildable state.
import type { ReactNode } from 'react'
export default function RootLayout({ children }: { children: ReactNode }) {
return (
<html lang="en">
<body>{children}</body>
</html>
)
}
import { Workspace } from '../components/workspace'
export default function Page() {
return (
<main>
<h1>Formula Workspace</h1>
<Workspace />
</main>
)
}
'use client'
import CodeMirror from '@uiw/react-codemirror'
import { javascript } from '@codemirror/lang-javascript'
export function HeavyEditor({
value,
onChange,
}: {
value: string
onChange: (value: string) => void
}) {
return (
<CodeMirror
value={value}
height="240px"
extensions={[javascript({ jsx: true })]}
onChange={onChange}
/>
)
}
'use client'
import { useState } from 'react'
import { HeavyEditor } from './heavy-editor'
export function Workspace() {
const [showEditor, setShowEditor] = useState(false)
const [formula, setFormula] = useState('revenue - costs')
return (
<section>
<p>Build and test a formula before adding it to the dashboard.</p>
<button onClick={() => setShowEditor(true)}>Open formula editor</button>
{showEditor ? (
<HeavyEditor value={formula} onChange={setFormula} />
) : null}
</section>
)
}
import type { NextConfig } from 'next'
const nextConfig: NextConfig = {}
export default nextConfig
{
"private": true,
"type": "module",
"scripts": {
"build": "next build",
"dev": "next dev",
"start": "next start"
},
"dependencies": {
"@codemirror/lang-javascript": "^6.2.4",
"@uiw/react-codemirror": "^4.25.4",
"next": "^16",
"react": "19.1.0",
"react-dom": "19.1.0"
},
"devDependencies": {
"@types/node": "^20",
"@types/react": "^19",
"@types/react-dom": "^19",
"typescript": "^5",
"vitest": "^3.1.3"
}
}
{
"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"]
}
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