Skip to content

Commit

Permalink
update feature detection markers to match new standard
Browse files Browse the repository at this point in the history
Feature detection was added in #747 to allow performance
analysis for nuxt/image uses in aggregate. Since that time,
Javascript-based feature markers have been standardized
in the W3C group around the User Timing API:
https://www.w3.org/TR/user-timing/#dfn-mark_feature_usage

This commit updates the feature tracking to use the new
standard, which is designed to be performance-neutral.
The previous mechanism (the data- attributes) will be
removed in a future PR, once we can confirm it works
as expected end-to-end.
  • Loading branch information
kara committed Jan 31, 2024
1 parent 35a57cc commit db7a0b1
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/runtime/components/nuxt-img.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { parseSize } from '../utils'
import { prerenderStaticImages } from '../utils/prerender'
import { baseImageProps, useBaseImage } from './_base'
import { useHead, useNuxtApp } from '#imports'
import { markFeatureUsage } from '../utils/performance'

Check failure on line 7 in src/runtime/components/nuxt-img.ts

View workflow job for this annotation

GitHub Actions / ci

`../utils/performance` import should occur before import of `./_base`

export const imgProps = {
...baseImageProps,
Expand Down Expand Up @@ -112,6 +113,8 @@ export default defineComponent({
placeholderLoaded.value = true
ctx.emit('load', event)
}

markFeatureUsage('nuxt-image')
return
}

Expand Down
3 changes: 3 additions & 0 deletions src/runtime/components/nuxt-picture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { prerenderStaticImages } from '../utils/prerender'
import { useBaseImage, baseImageProps } from './_base'
import { useImage, useHead, useNuxtApp } from '#imports'
import { getFileExtension } from '#image'
import { markFeatureUsage } from '../utils/performance'

Check failure on line 7 in src/runtime/components/nuxt-picture.ts

View workflow job for this annotation

GitHub Actions / ci

`../utils/performance` import should occur before import of `./_base`

export const pictureProps = {
...baseImageProps,
Expand Down Expand Up @@ -95,6 +96,8 @@ export default defineComponent({
imgEl.value.onload = (event) => {
ctx.emit('load', event)
}

markFeatureUsage('nuxt-picture')
})

return () =>
Expand Down
15 changes: 15 additions & 0 deletions src/runtime/utils/performance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* Feature detection enables us to analyze the performance of nuxt/image
* usages and validate performance outcomes (in aggregate only).
*

Check failure on line 4 in src/runtime/utils/performance.ts

View workflow job for this annotation

GitHub Actions / ci

Trailing spaces not allowed
* Javascript-based feature markers have been standardized in the W3C group
* around the User Timing API. See more detail here:
* https://www.w3.org/TR/user-timing/#dfn-mark_feature_usage
*/
export function markFeatureUsage(featureName: string) {

Check failure on line 9 in src/runtime/utils/performance.ts

View workflow job for this annotation

GitHub Actions / ci

Missing space before function parentheses
performance?.mark?.('mark_feature_usage', {
detail: {
feature: featureName
}
})
}

Check failure on line 15 in src/runtime/utils/performance.ts

View workflow job for this annotation

GitHub Actions / ci

Newline required at end of file but not found

0 comments on commit db7a0b1

Please sign in to comment.