Commit d190f2e1 authored by Houssein Djirdeh's avatar Houssein Djirdeh Committed by Joe Haddad

Adds user timing marks and measures for hydration/render (#8069)

* adds user timing marks for hydration step

* changes measure names and removes window namespaces by adding performance as a global to standard

* separates hydrate and render marks

* Add performance mark to router change instead of link click

* Changes performance mark name at router change

* adds checks for performance

* creates constants utils for performance and user timings
parent 57ac8a09
......@@ -53,7 +53,8 @@
"examples/with-kea/**",
"examples/with-mobx/**",
"test/integration/client-navigation/pages/throw-undefined.js"
]
],
"globals": [ "performance" ]
},
"devDependencies": {
"@babel/plugin-proposal-object-rest-spread": "7.4.4",
......
......@@ -11,6 +11,7 @@ import {
getURL,
loadGetInitialProps,
NextPageContext,
SUPPORTS_PERFORMANCE_USER_TIMING,
} from '../utils'
import { rewriteUrlForNextExport } from './rewrite-url-for-export'
import { getRouteMatcher } from './utils/route-matcher'
......@@ -246,6 +247,11 @@ export default class Router implements BaseRouter {
change(method: string, _url: Url, _as: Url, options: any): Promise<boolean> {
return new Promise((resolve, reject) => {
// marking route changes as a navigation start entry
if (SUPPORTS_PERFORMANCE_USER_TIMING) {
performance.mark('routeChange')
}
// If url and as provided as an object representation,
// we'll format them into the string version here.
const url = typeof _url === 'object' ? formatWithValidation(_url) : _url
......
......@@ -282,3 +282,9 @@ export function formatWithValidation(
return format(url as any, options)
}
export const SUPPORTS_PERFORMANCE = typeof performance !== 'undefined'
export const SUPPORTS_PERFORMANCE_USER_TIMING =
SUPPORTS_PERFORMANCE &&
typeof performance.mark === 'function' &&
typeof performance.measure === 'function'
......@@ -4,7 +4,11 @@ import ReactDOM from 'react-dom'
import HeadManager from './head-manager'
import { createRouter, makePublicRouterInstance } from 'next/router'
import mitt from 'next-server/dist/lib/mitt'
import { loadGetInitialProps, getURL } from 'next-server/dist/lib/utils'
import {
loadGetInitialProps,
getURL,
SUPPORTS_PERFORMANCE_USER_TIMING
} from 'next-server/dist/lib/utils'
import PageLoader from './page-loader'
import * as envConfig from 'next-server/config'
import { HeadManagerContext } from 'next-server/dist/lib/head-manager-context'
......@@ -240,13 +244,54 @@ export async function renderError (props) {
// If hydrate does not exist, eg in preact.
let isInitialRender = typeof ReactDOM.hydrate === 'function'
function renderReactElement (reactEl, domEl) {
// mark start of hydrate/render
if (SUPPORTS_PERFORMANCE_USER_TIMING) {
performance.mark('beforeRender')
}
// The check for `.hydrate` is there to support React alternatives like preact
if (isInitialRender) {
ReactDOM.hydrate(reactEl, domEl)
ReactDOM.hydrate(reactEl, domEl, markHydrateComplete)
isInitialRender = false
} else {
ReactDOM.render(reactEl, domEl)
ReactDOM.render(reactEl, domEl, markRenderComplete)
}
}
function markHydrateComplete () {
if (!SUPPORTS_PERFORMANCE_USER_TIMING) return
performance.mark('afterHydrate') // mark end of hydration
performance.measure('Next.js-before-hydration', null, 'beforeRender')
performance.measure('Next.js-hydration', 'beforeRender', 'afterHydrate')
clearMarks()
}
function markRenderComplete () {
if (!SUPPORTS_PERFORMANCE_USER_TIMING) return
performance.mark('afterRender') // mark end of render
const navStartEntries = performance.getEntriesByName('routeChange', 'mark')
if (!navStartEntries.length) {
return
}
performance.measure(
'Next.js-route-change-to-render',
navStartEntries[0].name,
'beforeRender'
)
performance.measure('Next.js-render', 'beforeRender', 'afterRender')
clearMarks()
}
function clearMarks () {
performance.clearMarks()
performance.clearMeasures()
}
function AppContainer ({ children }) {
......
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