refactor(tracker): cleanup img module & remove longtasks
This commit is contained in:
parent
48f23c4d55
commit
9fad7577a9
2 changed files with 29 additions and 80 deletions
|
|
@ -32,7 +32,24 @@ export default function (app: App): void {
|
|||
}
|
||||
}
|
||||
|
||||
const sendImgSrc = app.safe(function (this: HTMLImageElement): void {
|
||||
const sendSrcset = function (id: number, img: HTMLImageElement): void {
|
||||
const { srcset } = img
|
||||
if (!srcset) {
|
||||
return
|
||||
}
|
||||
const resolvedSrcset = srcset
|
||||
.split(',')
|
||||
.map((str) => resolveURL(str))
|
||||
.join(',')
|
||||
app.send(SetNodeAttribute(id, 'srcset', resolvedSrcset))
|
||||
}
|
||||
|
||||
const sendSrc = function (id: number, img: HTMLImageElement): void {
|
||||
const src = img.src
|
||||
app.send(SetNodeAttributeURLBased(id, 'src', src, app.getBaseHref()))
|
||||
}
|
||||
|
||||
const sendImgAttrs = app.safe(function (this: HTMLImageElement): void {
|
||||
const id = app.nodes.getID(this)
|
||||
if (id === undefined) {
|
||||
return
|
||||
|
|
@ -49,14 +66,8 @@ export default function (app: App): void {
|
|||
} else if (resolvedSrc.length >= 1e5 || app.sanitizer.isMasked(id)) {
|
||||
sendPlaceholder(id, this)
|
||||
} else {
|
||||
app.send(SetNodeAttribute(id, 'src', resolvedSrc))
|
||||
if (srcset) {
|
||||
const resolvedSrcset = srcset
|
||||
.split(',')
|
||||
.map((str) => resolveURL(str))
|
||||
.join(',')
|
||||
app.send(SetNodeAttribute(id, 'srcset', resolvedSrcset))
|
||||
}
|
||||
sendSrc(id, this)
|
||||
sendSrcset(id, this)
|
||||
}
|
||||
})
|
||||
|
||||
|
|
@ -69,24 +80,26 @@ export default function (app: App): void {
|
|||
return
|
||||
}
|
||||
if (mutation.attributeName === 'src') {
|
||||
const src = target.src
|
||||
app.send(SetNodeAttributeURLBased(id, 'src', src, app.getBaseHref()))
|
||||
sendSrc(id, target)
|
||||
}
|
||||
if (mutation.attributeName === 'srcset') {
|
||||
const srcset = target.srcset
|
||||
app.send(SetNodeAttribute(id, 'srcset', srcset))
|
||||
sendSrcset(id, target)
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
app.attachStopCallback(() => {
|
||||
observer.disconnect()
|
||||
})
|
||||
|
||||
app.nodes.attachNodeCallback((node: Node): void => {
|
||||
if (!hasTag(node, 'IMG')) {
|
||||
return
|
||||
}
|
||||
app.nodes.attachElementListener('error', node, sendImgSrc)
|
||||
app.nodes.attachElementListener('load', node, sendImgSrc)
|
||||
sendImgSrc.call(node)
|
||||
app.nodes.attachElementListener('error', node, sendImgAttrs.bind(node))
|
||||
app.nodes.attachElementListener('load', node, sendImgAttrs.bind(node))
|
||||
sendImgAttrs.call(node)
|
||||
observer.observe(node, { attributes: true, attributeFilter: ['src', 'srcset'] })
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,64 +0,0 @@
|
|||
import type App from '../app/index.js'
|
||||
import { LongTask } from '../app/messages.gen.js'
|
||||
|
||||
// https://w3c.github.io/performance-timeline/#the-performanceentry-interface
|
||||
interface TaskAttributionTiming extends PerformanceEntry {
|
||||
readonly containerType: string
|
||||
readonly containerSrc: string
|
||||
readonly containerId: string
|
||||
readonly containerName: string
|
||||
}
|
||||
|
||||
// https://www.w3.org/TR/longtasks/#performancelongtasktiming
|
||||
interface PerformanceLongTaskTiming extends PerformanceEntry {
|
||||
readonly attribution: ReadonlyArray<TaskAttributionTiming>
|
||||
}
|
||||
|
||||
export default function (app: App): void {
|
||||
if (!('PerformanceObserver' in window) || !('PerformanceLongTaskTiming' in window)) {
|
||||
return
|
||||
}
|
||||
|
||||
const contexts: string[] = [
|
||||
'unknown',
|
||||
'self',
|
||||
'same-origin-ancestor',
|
||||
'same-origin-descendant',
|
||||
'same-origin',
|
||||
'cross-origin-ancestor',
|
||||
'cross-origin-descendant',
|
||||
'cross-origin-unreachable',
|
||||
'multiple-contexts',
|
||||
]
|
||||
const containerTypes: string[] = ['window', 'iframe', 'embed', 'object']
|
||||
function longTask(entry: PerformanceLongTaskTiming): void {
|
||||
let type = '',
|
||||
src = '',
|
||||
id = '',
|
||||
name = ''
|
||||
const container = entry.attribution[0]
|
||||
if (container != null) {
|
||||
type = container.containerType
|
||||
name = container.containerName
|
||||
id = container.containerId
|
||||
src = container.containerSrc
|
||||
}
|
||||
|
||||
app.send(
|
||||
LongTask(
|
||||
entry.startTime + performance.timing.navigationStart,
|
||||
entry.duration,
|
||||
Math.max(contexts.indexOf(entry.name), 0),
|
||||
Math.max(containerTypes.indexOf(type), 0),
|
||||
name,
|
||||
id,
|
||||
src,
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
const observer: PerformanceObserver = new PerformanceObserver((list) =>
|
||||
list.getEntries().forEach(longTask),
|
||||
)
|
||||
observer.observe({ entryTypes: ['longtask'] })
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue