forked from w3c/largest-contentful-paint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.bs
252 lines (198 loc) · 15.3 KB
/
index.bs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
<pre class=metadata>
Title: Largest Contentful Paint
Status: CG-DRAFT
Shortname: largest-contentful-paint
Group: WICG
Level: 1
Editor: Yoav Weiss, Google https://google.com, [email protected]
Editor: Nicolás Peña Moreno, Google https://google.com, [email protected]
URL: https://wicg.github.io/largest-contentful-paint
Repository: https://github.com/WICG/largest-contentful-paint
Test Suite: https://github.com/web-platform-tests/wpt/tree/master/largest-contentful-paint
Abstract: This document defines an API that enables monitoring the largest paint an element triggered on screen.
Default Highlight: js
</pre>
<pre class=anchors>
urlPrefix: https://w3c.github.io/performance-timeline/; spec: PERFORMANCE-TIMELINE-2;
type: interface; url: #the-performanceentry-interface; text: PerformanceEntry;
type: attribute; for: PerformanceEntry;
text: name; url: #dom-performanceentry-name;
text: entryType; url: #dom-performanceentry-entrytype;
text: startTime; url: #dom-performanceentry-starttime;
text: duration; url: #dom-performanceentry-duration;
type: dfn; url: #dfn-queue-a-performanceentry; text: queue the PerformanceEntry;
type: attribute; for: PerformanceObserver;
text: supportedEntryTypes; url: #supportedentrytypes-attribute;
urlPrefix: https://wicg.github.io/element-timing/; spec: ELEMENT-TIMING;
type: dfn; url: #sec-elements-exposed; text: exposed;
type: dfn; url: #get-an-element; text: get an element;
urlPrefix: https://w3c.github.io/hr-time; spec: HR-TIME;
type: dfn; url: #dfn-current-high-resolution-time; text: current high resolution time;
type: interface; url: #dom-domhighrestimestamp; text: DOMHighResTimeStamp;
urlPrefix: https://dom.spec.whatwg.org; spec: DOM;
type: dfn; url: #concept-document; text: Document;
type: dfn; url: #concept-element; text: Element;
type: attribute; for: Element;
text: element id; url: #dom-element-id;
type: dfn; url: #concept-event-dispatch; text: event dispatch algorithm;
urlPrefix: https://wicg.github.io/event-timing; spec: EVENT-TIMING;
type: dfn; url: #has-dispatched-input-event; text: has dispatched input event;
urlPrefix: https://fetch.spec.whatwg.org/; spec: FETCH;
type: dfn; url: #dom-request-url; text: request URL
urlPrefix: https://html.spec.whatwg.org/multipage/webappapis.html; spec: html;
type: dfn; text: relevant global object; url: concept-relevant-global;
type: attribute; for: img;
text: naturalWidth; url: #dom-img-naturalwidth;
text: naturalHeight; url: #dom-img-naturalheight;
text: width; url: #dom-img-width;
text: height; url: #dom-img-height;
</pre>
Introduction {#sec-intro}
=====================
<em>This section is non-normative.</em>
The LargestContentfulPaint API enables developers to gain visibility into the loading and rendering process of the web pages, in order for them to be able to optimize it.
Developers today don't have a reliable metric that correlates with their user's visual rendering experience. Existing metrics such as First Paint and First Contentful Paint focus on initial rendering, but don't take into account the importance of the painted content, and therefore may indicate times in which the user still does not consider the page useful.
Largest Contentful Paint (LCP) aims to be a new page-load metric that:
* better correlates with user experience than the existing page-load metrics
* is easy to understand and reason about
* reduces the chance of gaming
The largest paint during the loading process of the page is likely to signify a meaningful event from the user's perspective, and is therefore something we want to expose by default to developers, enabling performance teams, analytics providers and lab-based measurement tools to collect those metrics without requiring extra annotation work by the folks creating the content itself.
The API relies heavily on [[ELEMENT-TIMING]], which can be thought of as the low-level primitive that this high-level feature is built on top of. For cases where the content creators are willing to annotate their content and indicate the important points in the page's loading cycle, Element Timing is the API that will provide them more control over the elements that get reported.
Elements exposed {#sec-elements-exposed}
------------------------
The Largest Contentful Paint API will only expose element types that are already <a>exposed</a> by the Element Timing API. In this case, there is no need to annotate them with the <code>elementtiming</code> attribute.
Largest content {#sec-largest-content}
------------------------
The algorithm used for this API keeps track of the content seen so far. Whenever a new largest content is found, a new entry is created for it. Content that is removed is still considered by the algorithm. In particular, if the content removed was the largest, then a new entry is created only if larger content is ever added. The algorithm terminates whenever scroll or input events occur, since those are likely to introduce new content into the website.
Usage example {#sec-example}
------------------------
The following example shows an image and a large body of text. The developer then registers an observer that gets candidate entries for the largest paint while the page is loading.
<xmp class="example highlight" highlight=html>
<img src="large_image.jpg">
<p id='large-paragraph'>This is large body of text.</p>
...
<script>
const observer = new PerformanceObserver((list) => {
let perfEntries = list.getEntries();
let lastEntry = perfEntries[perfEntries.length - 1];
// Process the latest candidate for largest contentful paint
});
observer.observe({entryTypes: ['largest-contentful-paint']});
</script>
</xmp>
Limitations {#limitations}
------------------------
The LargestContentfulPaint API is based on heuristics. As such, it is error prone. It has the following problems:
* The algorithm halts when it detects certain types of user inputs. However, this means that the algorithm will not capture the main content if the user input occurs before the main content is displayed. In fact, the algorithm may produce meaningless results or no results at all if user input occurs very early.
* To account for image carousels, content is still considered as the largest even if it's removed. This presents problems for websites with splash screens that use large content as placeholders.
Largest Contentful Paint {#sec-largest-contentful-paint}
=======================================
Largest Contentful Paint involves the following new interface:
{{LargestContentfulPaint}} interface {#sec-largest-contentful-paint-interface}
------------------------------------------------------------------------
<pre class="idl">
[Exposed=Window]
interface LargestContentfulPaint : PerformanceEntry {
readonly attribute DOMHighResTimeStamp renderTime;
readonly attribute DOMHighResTimeStamp loadTime;
readonly attribute unsigned long size;
readonly attribute DOMString id;
readonly attribute DOMString url;
readonly attribute Element? element;
[Default] object toJSON();
};
</pre>
Each {{LargestContentfulPaint}} object has these associated concepts:
* A <dfn>renderTime</dfn>, initially set to 0.
* A <dfn>size</dfn>, initially set to 0.
* A <dfn>loadTime</dfn>, initially set to 0.
* An <dfn>id</dfn>, initially set to the empty string.
* A <dfn>url</dfn>, initially set to the empty string.
* An <dfn>element</dfn> containing the associated {{Element}}, initially set to <code>null</code>.
The {{PerformanceEntry/entryType}} attribute's getter must return the {{DOMString}} <code>"largest-contentful-paint"</code>.
The {{PerformanceEntry/name}} attribute's getter must return the empty string.
The {{PerformanceEntry/startTime}} attribute's getter must return the value of the <a>context object</a>'s <a>renderTime</a> if it is not 0, and the value of the <a>context object</a>'s <a>loadTime</a> otherwise.
The {{PerformanceEntry/duration}} attribute's getter must return 0.
The {{LargestContentfulPaint/renderTime}} attribute must return the value of the <a>context object</a>'s <a>renderTime</a>.
The {{LargestContentfulPaint/loadTime}} attribute must return the value of the <a>context object</a>'s <a>loadTime</a>.
The {{LargestContentfulPaint/size}} attribute must return the value of the <a>context object</a>'s <a>size</a>.
The {{LargestContentfulPaint/id}} attribute must return the value of the <a>context object</a>'s <a>id</a>.
The {{LargestContentfulPaint/url}} attribute must return the value of the <a>context object</a>'s <a>url</a>.
The {{LargestContentfulPaint/element}} attribute's getter must return the value returned by running the <a>get an element</a> algorithm with <a>element</a> and null as inputs.
Note: The above algorithm defines that an element that is no longer <a>descendant</a> of the {{Document}} will no longer be returned by {{LargestContentfulPaint/element}}'s attribute getter, including elements that are inside a shadow DOM.
This specification also extends {{Document}} by adding to it a <dfn>largest contentful paint size</dfn> concept, initially set to 0.
It also adds an associated <dfn>content set</dfn>, which is initially an empty <a spec=infra for=/>set</a>. The [=content set=] will be filled with <a>pairs</a> with an {{Element}} as the first item and a {{Request}} as the second item. This is used for performance, to enable the algorithm to only consider each content once.
Note: The user agent needs to maintain the [=content set=] so that removed content does not introduce memory leaks. In particular, it can tie the lifetime of the <a>pairs</a> to weak pointers to the {{Element|Elements}} so that it can be cleaned up sometime after the {{Element|Elements}} are removed. Since the <a spec=infra for=/>set</a> is not exposed to web developers, this does not expose garbage collection timing.
Processing model {#sec-processing-model}
========================================
Potentially add LargestContentfulPaint entry {#sec-add-lcp-entry}
--------------------------------------------------------
Note: A user agent implementing the Largest Contentful Paint API would need to include <code>"largest-contentful-paint"</code> in {{PerformanceObserver/supportedEntryTypes}} for {{Window}} contexts.
This allows developers to detect support for the API.
In order to <dfn export>potentially add a {{LargestContentfulPaint}} entry</dfn>, the user agent must run the following steps:
<div algorithm="LargestContentfulPaint potentially-add-entry">
: Input
:: |intersectionRect|, a {{DOMRectReadOnly}}
:: |imageRequest|, a {{Request}}
:: |renderTime|, a DOMHighResTimestamp
:: |loadTime|, a DOMHighResTimestamp
:: |element|, an <a>Element</a>
:: |document|, a <a>Document</a>
: Output
:: None
1. Let |contentIdentifier| be the <a>pair</a> (|element|, |imageRequest|).
1. If |document|'s [=content set=] <a for=set>contains</a> |contentIdentifier|, return.
1. <a for=set>Append</a> |contentIdentifier| to |document|'s [=content set=]
1. Let |window| be |document|’s [=relevant global object=].
1. If either of |window|'s [=has dispatched scroll event=] or [=has dispatched input event=] is true, return.
1. Let |url| be the empty string.
1. If |imageRequest| is not null, set |url| to be |imageRequest|'s [=request URL=].
1. Let |id| be |element|'s <a attribute for=Element>element id</a>.
1. Let |width| be |intersectionRect|'s {{DOMRectReadOnly/width}}.
1. Let |height| be |intersectionRect|'s {{DOMRectReadOnly/height}}.
1. Let |size| be <code>|width| * |height|</code>.
1. If |imageRequest| is not null, run the following steps:
1. Let |naturalWidth| and |naturalHeight| be the outputs of running the same steps for an <{img}>'s {{img/naturalWidth}} and {{img/naturalHeight}} attribute getters, but using |imageRequest| as the image.
1. Let |naturalSize| be <code>|naturalWidth| * |naturalHeight|</code>.
1. Let |displayWidth| and |displayHeight| be the outputs of running the same steps for an <{img}>'s {{img/width}} and {{img/height}} attribute getters, but using |imageRequest| as the image.
1. Let |displaySize| be <code>|displayWidth| * |displayHeight|</code>.
1. Let |penaltyFactor| be <code>min(|displaySize|, |naturalSize|) / |displaySize|</code>.
1. Multiply |size| by |penaltyFactor|.
1. If |size| is less than or equal to |document|'s [=largest contentful paint size=], return.
1. Let |contentInfo| be a <a>map</a> with |contentInfo|["size"] = |size|, |contentInfo|["url"] = |url|, |contentInfo|["id"] = |id|, |contentInfo|["renderTime"] = |renderTime|, |contentInfo|["loadTime"] = |loadTime|, and contentInfo["element"] = |element|.
1. <a>Create a LargestContentfulPaint entry</a> with |contentInfo|, and |document| as inputs.
</div>
Create a LargestContentfulPaint entry {#sec-create-entry}
--------------------------------------------------------
In order to <dfn>create a {{LargestContentfulPaint}} entry</dfn>, the user agent must run the following steps:
<div algorithm="LargestContentfulPaint create-entry">
: Input
:: |contentInfo|, a <a>map</a>
:: |document|, a {{Document}}
: Output
:: None
1. Set |document|'s [=largest contentful paint size=] to |contentInfo|["size"].
1. Let |entry| be a new {{LargestContentfulPaint}} entry with |document|'s [=relevant realm=], with its
* {{LargestContentfulPaint/size}} set to |contentInfo|["size"],
* {{LargestContentfulPaint/url}} set to |contentInfo|["url"],
* {{LargestContentfulPaint/id}} set to |contentInfo|["id"],
* {{LargestContentfulPaint/renderTime}} set to |contentInfo|["renderTime"],
* {{LargestContentfulPaint/loadTime}} set to |contentInfo|["loadTime"],
* and {{LargestContentfulPaint/element}} set to |contentInfo|["element"].
1. [=Queue the PerformanceEntry=] |entry|.
</div>
Modifications to the DOM specification {#sec-modifications-DOM}
--------------------------------------------------------
<em>This section will be removed once the [[DOM]] specification has been modified.</em>
<div algorithm="additions to event dispatch">
We modify the <a>event dispatch algorithm</a> as follows.
Right after step 1, we add the following step:
* If |target|'s [=relevant global object=] is a {{Window}} object, <var ignore>event</var>'s {{Event/type}} is {{scroll}} and its {{Event/isTrusted}} is false, set |target|'s [=relevant global object=]'s [=has dispatched scroll event=] to true.
</div>
Modifications to the HTML specification {#sec-modifications-HTML}
----------------------------------------
<em>This section will be removed once the [[HTML]] specification has been modified.</em>
Each {{Window}} has <dfn>has dispatched scroll event</dfn>, a boolean which is initially set to false.
Security & privacy considerations {#sec-security}
===============================================
This API relies on Element Timing for its underlying primitives. LCP may expose some element not exposed by Element Timing in case that they are smaller than Element Timing's limits, but are still the largest elements to be painted up until that point in the page's loading. That does not seem to expose any sensitive information beyond what Element Timing already enables.