Comments (3)
I'm concerned that the CLS will not trigger by default until a user has navigated a few times in a SPA... wouldn't that throw off the value significantly?
As currently defined, CLS doesn't treat SPAs differently from any other web page. CLS measures layout shifts that occur throughout the entire lifecycle of a page, regardless of whether that page updates the URL.
So to answer your question, no, navigating in an SPA does not throw off the value of CLS; though (depending on how the SPA is loading new content) it could mean that a page that a user keeps open for a long time may end up with higher CLS than it would have if it had used traditional page loads. On the other hand (again, depending on how the site is built), an SPA may also end up with less CLS in cases where most of the layout shifts happen during page load.
The main reason for incorporating this into our application is to track user experience on first load. So in that sense I feel like I'd prefer CLS to have a way of returning the current shift value when called (after DOM load in our case) which obviously hopefully would be a value of 0
I understand why you'd want this, but I want to stress that what you're describing is not CLS. If you do want to track this data, I'd recommend calling it something else to avoid confusion (e.g. "pre-load layout shift" or "CLS before load", etc.).
Also, while it's possible to track CLS before load using this library, it gets a bit tricky because PerformanceObserver
callbacks are not always called right away (to avoid them affecting performance), so you can't necessarily guarantee that all callback have run by the time the load event fires.
For example, this (using the reportAllChanges
param) will be close but might not be 100% accurate:
import {getCLS} from 'web-vitals';
let clsBeforeLoad = 0;
getCLS((metric) => {
clsBeforeLoad = metric.value;
}, true);
addEventListener('load', () => {
// `clsBeforeLoad` here might not have the latest entries.
console.log(clsBeforeLoad);
});
If you need precision, you're better off not using this library and just using the underlying Layout Instability API directly:
let clsBeforeLoad = 0;
function onLayoutShiftEntry(entry) {
// Only count layout shifts without recent user input.
if (!entry.hadRecentInput) {
clsBeforeLoad += entry.value;
}
}
const po = new PerformanceObserver((list) => {
list.getEntries().map(onLayoutShiftEntry);
});
po.observe({'layout-shift', buffered: true});
addEventListener('load', () => {
// Calling `takeRecords()` gives you access to any undispatched entries in the load event.
po.takeRecords().map(onLayoutShiftEntry);
console.log(clsBeforeLoad);
});
from web-vitals.
I'm going to close this issue as I don't think there are any changes that need to be made to the library to address this.
from web-vitals.
Maybe interesting thread about Web Vitals in combination with ranking factors (coming in May 2021): https://twitter.com/simonhearne/status/1316775506222682112
from web-vitals.
Related Issues (20)
- can't find ReportCallback or ReportHandler type HOT 3
- Are there any updates for soft-navs? HOT 2
- LoAF missed for some INP events HOT 9
- Facing issue while exporting `web-vitals/attribution` using `browserify` HOT 2
- LoAF Performance entry script sourceFunctionName and sourceURL logged values HOT 4
- Using `performance.eventCounts` instead of `performance.interactionCount` HOT 2
- Heads Up on Changes to INP Measurement When Interfered with JS Modal Dialogs HOT 3
- TypeError: Cannot read properties of undefined (reading 'processingEnd') HOT 13
- TypeScript 5.5 compatibility
- Speculation rules can impact on increase values for INP ?
- Missing Cumulative Layout Shift (CLS) metric when batching multiple reports together in a single network request HOT 5
- Add clarification regarding event listener in event batching section
- LCP and CLS fired on interaction HOT 5
- Tgt
- Update LCP attribution to handle the first frames of animated content HOT 3
- INP attribution helper latestProcessingEnd never initialized HOT 1
- Missing LoAF attributions for INP due to violating internal invariants
- Any way to simulate clicks for INP testing? HOT 3
- Identical abstract selectors can be returned as distinct strings HOT 6
- onINP trigger always when browser tab change ? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from web-vitals.