feat(nuxt): print nuxt hook timings in browser devtools (#29922)

This commit is contained in:
Daniel Roe 2024-12-03 10:12:07 +00:00 committed by GitHub
parent 23ebfc0c28
commit fad53aa9ec
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 57 additions and 0 deletions

View File

@ -0,0 +1,42 @@
import { defineNuxtPlugin } from '../nuxt'
export default defineNuxtPlugin({
name: 'nuxt:browser-devtools-timing',
enforce: 'pre',
setup (nuxtApp) {
nuxtApp.hooks.beforeEach((event) => {
// @ts-expect-error __startTime is not a public API
event.__startTime = performance.now()
})
// After each
nuxtApp.hooks.afterEach((event) => {
performance.measure(event.name, {
// @ts-expect-error __startTime is not a public API
start: event.__startTime,
detail: {
devtools: {
dataType: 'track-entry',
track: 'nuxt',
color: 'tertiary-dark',
} satisfies ExtensionTrackEntryPayload,
},
})
})
},
})
type DevToolsColor =
'primary' | 'primary-light' | 'primary-dark' |
'secondary' | 'secondary-light' | 'secondary-dark' |
'tertiary' | 'tertiary-light' | 'tertiary-dark' |
'error'
interface ExtensionTrackEntryPayload {
dataType?: 'track-entry' // Defaults to "track-entry"
color?: DevToolsColor // Defaults to "primary"
track: string // Required: Name of the custom track
trackGroup?: string // Optional: Group for organizing tracks
properties?: [string, string][] // Key-value pairs for detailed view
tooltipText?: string // Short description for tooltip
}

View File

@ -548,6 +548,12 @@ async function initNuxt (nuxt: Nuxt) {
addPlugin(resolve(nuxt.options.appDir, 'plugins/debug'))
}
// Add experimental Chrome devtools timings support
// https://developer.chrome.com/docs/devtools/performance/extension
if (nuxt.options.experimental.browserDevtoolsTiming) {
addPlugin(resolve(nuxt.options.appDir, 'plugins/browser-devtools-timing.client'))
}
for (const [key, options] of modulesToInstall) {
await installModule(key, options)
}

View File

@ -417,5 +417,14 @@ export default defineUntypedSchema({
return val ?? ((await get('future') as Record<string, unknown>).compatibilityVersion === 4)
},
},
/**
* Enable timings for Nuxt application hooks in the performance panel of Chromium-based browsers.
*
* @see [the Chrome DevTools extensibility API](https://developer.chrome.com/docs/devtools/performance/extension#tracks)
*/
browserDevtoolsTiming: {
$resolve: async (val, get) => val ?? await get('dev'),
},
},
})