Skip to content

Commit

Permalink
performance-impl: cant check ampdoc vis hidden while null (#36197)
Browse files Browse the repository at this point in the history
* performance-impl: cant check ampdoc vis hidden before initted

* Add unit test
  • Loading branch information
samouri authored Sep 29, 2021
1 parent 3e9f5db commit 39162ab
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/service/performance-impl.js
Original file line number Diff line number Diff line change
Expand Up @@ -556,9 +556,14 @@ export class Performance {
* @param {!LayoutShift} entry
*/
tickLayoutShiftScore_(entry) {
if (!this.ampdoc_) {
return;
}

if (this.isVisibilityHidden_()) {
return;
}

const entries = this.layoutShiftEntries_;
if (entries.length > 0) {
const first = entries[0];
Expand Down
16 changes: 16 additions & 0 deletions test/unit/test-performance.js
Original file line number Diff line number Diff line change
Expand Up @@ -1167,6 +1167,22 @@ describes.realWin('PeformanceObserver metrics', {amp: true}, (env) => {
return Services.performanceFor(env.win);
}

it('should not throw when layout-shift occurs before core services available', () => {
// Fake the Performance API.
env.win.PerformanceObserver.supportedEntryTypes = ['layout-shift'];
const perf = getPerformance();

// Fake layout-shift that occured before core services registered
performanceObserver.triggerCallback({
getEntries() {
return [
{entryType: 'layout-shift', value: 0.3, hadRecentInput: false},
];
},
});
perf.coreServicesAvailable();
});

it('when the viewer visibility changes to inactive', () => {
// Specify an Android Chrome user agent.
env.sandbox
Expand Down

0 comments on commit 39162ab

Please sign in to comment.