Skip to content

Commit

Permalink
better logging
Browse files Browse the repository at this point in the history
  • Loading branch information
balazsbajorics committed Oct 4, 2024
1 parent e15522e commit 3a79a98
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 107 deletions.
2 changes: 2 additions & 0 deletions app/components/Layout.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
PredictiveSearchForm,
PredictiveSearchResults,
} from '~/components/Search'
import { useLogRerender } from '~/helpers/logging'

/**
* @param {LayoutProps}
Expand All @@ -20,6 +21,7 @@ export function Layout({
header,
isLoggedIn,
}) {
useLogRerender('Layout')
return (
<div>
<CartAside cart={cart} />
Expand Down
40 changes: 40 additions & 0 deletions app/helpers/logging.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import * as React from 'react'

function getID() {
// return randnom alphanumeric string
return Math.random().toString(36).substring(2, 6)
}

function logRerender(
name,
renderCount,
ID,
mountOrUnmount,
) {
const message = `${name} ${ID} ${mountOrUnmount} ${renderCount}`
console.log(message)
if (
typeof performance !== 'undefined' &&
typeof performance.measure !== 'undefined'
) {
performance.measure(message, {
start: performance.now(),
duration: 20,
})
}
}

export function useLogRerender(name) {
const [ID] = React.useState(getID())
const renderCount = React.useRef(0)
renderCount.current = renderCount.current + 1
logRerender(name, renderCount.current, ID, 'render')
React.useEffect(() => {
logRerender(name, renderCount.current, ID, 'mount')
return () => {
logRerender(name, renderCount.current, ID, 'unmount')
}
}, [])

return renderCount
}
37 changes: 2 additions & 35 deletions app/routes/_index/route.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,10 @@ import React from 'react'

import { Column } from '@h2/new/Layout'
import Hero from './sections/hero'
import { useLogRerender } from '~/helpers/logging'

export default function Homepage() {
const renderCount = React.useRef(Math.random())
renderCount.current = renderCount.current + 1
console.log('Homepage render', renderCount.current)

if (
typeof performance !== 'undefined' &&
typeof performance.measure !== 'undefined'
) {
performance.measure(
`Homepage route render ${renderCount.current}`,
{
start: performance.now(),
duration: 50,
},
)
}

React.useEffect(() => {
return function cleanup() {
// we are about to unmount!
console.log('Homepage unmount', renderCount.current)
if (
typeof performance !== 'undefined' &&
typeof performance.measure !== 'undefined'
) {
performance.measure(
`Homepage route unmount ${renderCount.current}`,
{
start: performance.now(),
duration: 50,
},
)
}
}
}, [])
useLogRerender('Homepage')

return (
<Column>
Expand Down
75 changes: 3 additions & 72 deletions app/routes/_index/sections/hero.jsx
Original file line number Diff line number Diff line change
@@ -1,41 +1,8 @@
import * as React from 'react'
import { useLogRerender } from '~/helpers/logging'

export default function Hero() {
const renderCount = React.useRef(Math.random())
renderCount.current = renderCount.current + 1
console.log('Hero render', renderCount.current)

if (
typeof performance !== 'undefined' &&
typeof performance.measure !== 'undefined'
) {
performance.measure(
`Hero render ${renderCount.current}`,
{
start: performance.now(),
duration: 50,
},
)
}

React.useEffect(() => {
return function cleanup() {
// we are about to unmount!
console.log('Hero unmount', renderCount.current)
if (
typeof performance !== 'undefined' &&
typeof performance.measure !== 'undefined'
) {
performance.measure(
`Hero unmount ${renderCount.current}`,
{
start: performance.now(),
duration: 50,
},
)
}
}
}, [])
useLogRerender('Hero')

return (
<section className='w-full relative min-h-8 h-svh max-h-[64rem] bg-darkGray text-white'>
Expand All @@ -58,43 +25,7 @@ export default function Hero() {

function Counter() {
const [clickCount, setClickCount] = React.useState(0)
const renderCount = React.useRef(0)
renderCount.current += 1

console.log('Counter render', renderCount.current)

if (
typeof performance !== 'undefined' &&
typeof performance.measure !== 'undefined'
) {
performance.measure(
`Counter render ${renderCount.current}`,
{
start: performance.now(),
duration: 50,
},
)
}

React.useEffect(() => {
return function cleanup() {
// we are about to unmount!
console.log('Counter unmount', renderCount.current)

if (
typeof performance !== 'undefined' &&
typeof performance.measure !== 'undefined'
) {
performance.measure(
`Counter unmount ${renderCount.current}`,
{
start: performance.now(),
duration: 50,
},
)
}
}
}, [])
const renderCount = useLogRerender('Counter')

return (
<div style={{ contain: 'layout' }}>
Expand Down

0 comments on commit 3a79a98

Please sign in to comment.