Skip to content

Commit 39162ab

Browse files
authored
performance-impl: cant check ampdoc vis hidden while null (#36197)
* performance-impl: cant check ampdoc vis hidden before initted * Add unit test
1 parent 3e9f5db commit 39162ab

2 files changed

Lines changed: 21 additions & 0 deletions

File tree

src/service/performance-impl.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,9 +556,14 @@ export class Performance {
556556
* @param {!LayoutShift} entry
557557
*/
558558
tickLayoutShiftScore_(entry) {
559+
if (!this.ampdoc_) {
560+
return;
561+
}
562+
559563
if (this.isVisibilityHidden_()) {
560564
return;
561565
}
566+
562567
const entries = this.layoutShiftEntries_;
563568
if (entries.length > 0) {
564569
const first = entries[0];

test/unit/test-performance.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1167,6 +1167,22 @@ describes.realWin('PeformanceObserver metrics', {amp: true}, (env) => {
11671167
return Services.performanceFor(env.win);
11681168
}
11691169

1170+
it('should not throw when layout-shift occurs before core services available', () => {
1171+
// Fake the Performance API.
1172+
env.win.PerformanceObserver.supportedEntryTypes = ['layout-shift'];
1173+
const perf = getPerformance();
1174+
1175+
// Fake layout-shift that occured before core services registered
1176+
performanceObserver.triggerCallback({
1177+
getEntries() {
1178+
return [
1179+
{entryType: 'layout-shift', value: 0.3, hadRecentInput: false},
1180+
];
1181+
},
1182+
});
1183+
perf.coreServicesAvailable();
1184+
});
1185+
11701186
it('when the viewer visibility changes to inactive', () => {
11711187
// Specify an Android Chrome user agent.
11721188
env.sandbox

0 commit comments

Comments
 (0)