Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Track page counts during builds #42766

Merged
merged 2 commits into from Nov 11, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
44 changes: 38 additions & 6 deletions packages/next/build/index.ts
Expand Up @@ -597,6 +597,7 @@ export default async function build(
appPageKeys.push(normalizedAppPageKey)
}
}
const totalAppPagesCount = appPageKeys.length

const pageKeys = {
pages: pagesPageKeys,
Expand Down Expand Up @@ -1097,6 +1098,7 @@ export default async function build(
telemetry.record(
eventBuildCompleted(pagesPaths, {
durationInSeconds: webpackBuildEnd[0],
totalAppPagesCount,
})
)

Expand All @@ -1121,6 +1123,10 @@ export default async function build(
const buildManifestPath = path.join(distDir, BUILD_MANIFEST)
const appBuildManifestPath = path.join(distDir, APP_BUILD_MANIFEST)

let staticAppPagesCount = 0
let serverAppPagesCount = 0
let edgeRuntimeAppCount = 0
let edgeRuntimePagesCount = 0
const ssgPages = new Set<string>()
const ssgStaticFallbackPages = new Set<string>()
const ssgBlockingFallbackPages = new Set<string>()
Expand Down Expand Up @@ -1300,6 +1306,18 @@ export default async function build(
config.experimental.gzipSize
)

const middlewareManifest: MiddlewareManifest = require(join(
distDir,
SERVER_DIRECTORY,
MIDDLEWARE_MANIFEST
))

for (const key of Object.keys(middlewareManifest?.functions)) {
if (key.startsWith('/api')) {
edgeRuntimePagesCount++
}
}

await Promise.all(
Object.entries(pageKeys)
.reduce<Array<{ pageType: keyof typeof pageKeys; page: string }>>(
Expand Down Expand Up @@ -1388,15 +1406,16 @@ export default async function build(
let edgeInfo: any

if (pageRuntime === SERVER_RUNTIME.edge) {
const manifest = require(join(
distDir,
SERVER_DIRECTORY,
MIDDLEWARE_MANIFEST
))
if (pageType === 'app') {
edgeRuntimeAppCount++
} else {
edgeRuntimePagesCount++
}

const manifestKey =
pageType === 'pages' ? page : originalAppPath || ''

edgeInfo = manifest.functions[manifestKey]
edgeInfo = middlewareManifest.functions[manifestKey]
}

let isPageStaticSpan =
Expand Down Expand Up @@ -1581,6 +1600,14 @@ export default async function build(
}
}

if (pageType === 'app') {
if (isSsg || isStatic) {
staticAppPagesCount++
} else {
serverAppPagesCount++
}
}

pageInfos.set(page, {
size: selfSize,
totalSize: allSize,
Expand Down Expand Up @@ -2589,6 +2616,11 @@ export default async function build(
.length,
redirectsWithHasCount: redirects.filter((r: any) => !!r.has).length,
middlewareCount: Object.keys(rootPaths).length > 0 ? 1 : 0,
totalAppPagesCount,
staticAppPagesCount,
serverAppPagesCount,
edgeRuntimeAppCount,
edgeRuntimePagesCount,
})
)

Expand Down
12 changes: 12 additions & 0 deletions packages/next/telemetry/events/build.ts
Expand Up @@ -56,6 +56,7 @@ type EventBuildCompleted = {
totalPageCount: number
hasDunderPages: boolean
hasTestPages: boolean
totalAppPagesCount?: number
}

export function eventBuildCompleted(
Expand All @@ -77,6 +78,7 @@ export function eventBuildCompleted(
(path) =>
REGEXP_DIRECTORY_TESTS.test(path) || REGEXP_FILE_TEST.test(path)
),
totalAppPagesCount: event.totalAppPagesCount,
},
}
}
Expand All @@ -100,6 +102,11 @@ type EventBuildOptimized = {
rewritesWithHasCount: number
redirectsWithHasCount: number
middlewareCount: number
totalAppPagesCount?: number
staticAppPagesCount?: number
serverAppPagesCount?: number
edgeRuntimeAppCount?: number
edgeRuntimePagesCount?: number
}

export function eventBuildOptimize(
Expand All @@ -121,6 +128,11 @@ export function eventBuildOptimize(
(path) =>
REGEXP_DIRECTORY_TESTS.test(path) || REGEXP_FILE_TEST.test(path)
),
totalAppPagesCount: event.totalAppPagesCount,
staticAppPagesCount: event.staticAppPagesCount,
serverAppPagesCount: event.serverAppPagesCount,
edgeRuntimeAppCount: event.edgeRuntimeAppCount,
edgeRuntimePagesCount: event.edgeRuntimePagesCount,
},
}
}
Expand Down
23 changes: 23 additions & 0 deletions test/integration/telemetry/pages/edge.js
@@ -0,0 +1,23 @@
import Image from 'next/image'
import LegacyImage from 'next/legacy/image'
import profilePic from '../public/small.jpg'

export const config = {
runtime: 'experimental-edge',
}

function About() {
return (
<>
<h1>My Homepage</h1>
<Image src={profilePic} alt="Picture of the author" />
<p>Welcome to my homepage!</p>
</>
)
}

export default About

export function AboutFutureImage() {
return <LegacyImage src={profilePic} alt="Picture of the author" />
}
90 changes: 87 additions & 3 deletions test/integration/telemetry/test/index.test.js
Expand Up @@ -331,9 +331,14 @@ describe('Telemetry CLI', () => {
const event1 = /NEXT_BUILD_OPTIMIZED[\s\S]+?{([\s\S]+?)}/.exec(stderr).pop()
expect(event1).toMatch(/"staticPropsPageCount": 2/)
expect(event1).toMatch(/"serverPropsPageCount": 2/)
expect(event1).toMatch(/"ssrPageCount": 2/)
expect(event1).toMatch(/"ssrPageCount": 3/)
expect(event1).toMatch(/"staticPageCount": 4/)
expect(event1).toMatch(/"totalPageCount": 10/)
expect(event1).toMatch(/"totalPageCount": 11/)
expect(event1).toMatch(/"totalAppPagesCount": 0/)
expect(event1).toMatch(/"staticAppPagesCount": 0/)
expect(event1).toMatch(/"serverAppPagesCount": 0/)
expect(event1).toMatch(/"edgeRuntimeAppCount": 0/)
expect(event1).toMatch(/"edgeRuntimePagesCount": 2/)
})

it('detects isSrcDir dir correctly for `next dev`', async () => {
Expand Down Expand Up @@ -377,7 +382,19 @@ describe('Telemetry CLI', () => {
)
await fs.mkdir(path.join(__dirname, '../app'))
await fs.writeFile(
path.join(__dirname, '../app/page.js'),
path.join(__dirname, '../app/layout.js'),
`
export default function RootLayout({ children }) {
return <html>
<head/>
<body>{children}</body>
</html>
}
`
)
await fs.ensureFile(path.join(__dirname, '../app/hello/page.js'))
await fs.writeFile(
path.join(__dirname, '../app/hello/page.js'),
'export default function Page() { return "hello world" }'
)

Expand Down Expand Up @@ -490,6 +507,73 @@ describe('Telemetry CLI', () => {
}
})

it('should detect app page counts', async () => {
const teardown = await setupAppDir()

try {
await fs.ensureFile(path.join(__dirname, '../app/ssr/page.js'))
await fs.writeFile(
path.join(__dirname, '../app/ssr/page.js'),
`
export const revalidate = 0
export default function Page() {
return <p>ssr page</p>
}
`
)
await fs.ensureFile(path.join(__dirname, '../app/edge-ssr/page.js'))
await fs.writeFile(
path.join(__dirname, '../app/edge-ssr/page.js'),
`
export const runtime = 'experimental-edge'
export default function Page() {
return <p>edge-ssr page</p>
}
`
)
await fs.ensureFile(path.join(__dirname, '../app/app-ssg/[slug]/page.js'))
await fs.writeFile(
path.join(__dirname, '../app/app-ssg/[slug]/page.js'),
`
export function generateStaticParams() {
return [
{ slug: 'post-1' },
{ slug: 'post-2' },
]
}
export default function Page() {
return <p>ssg page</p>
}
`
)
const { stderr } = await nextBuild(appDir, [], {
stderr: true,
env: { NEXT_TELEMETRY_DEBUG: 1 },
})

const event1 = /NEXT_BUILD_OPTIMIZED[\s\S]+?{([\s\S]+?)}/
.exec(stderr)
.pop()
expect(event1).toMatch(/"staticPropsPageCount": 2/)
expect(event1).toMatch(/"serverPropsPageCount": 2/)
expect(event1).toMatch(/"ssrPageCount": 3/)
expect(event1).toMatch(/"staticPageCount": 4/)
expect(event1).toMatch(/"totalPageCount": 11/)
expect(event1).toMatch(/"totalAppPagesCount": 4/)
expect(event1).toMatch(/"serverAppPagesCount": 2/)
expect(event1).toMatch(/"edgeRuntimeAppCount": 1/)
expect(event1).toMatch(/"edgeRuntimePagesCount": 2/)

const event2 = /NEXT_BUILD_COMPLETED[\s\S]+?{([\s\S]+?)}/
.exec(stderr)
.pop()

expect(event2).toMatch(/"totalAppPagesCount": 4/)
} finally {
await teardown()
}
})

it('detect reportWebVitals correctly for `next build`', async () => {
// Case 1: When _app.js does not exist.
let build = await nextBuild(appDir, [], {
Expand Down