Skip to content

Commit 7ca0d8f

Browse files
author
Joan León
authored
Merge pull request #37 from nucliweb/feat/lazy-fetchpriority
feat/lazy fetchpriority
2 parents 1e4aeed + 022faad commit 7ca0d8f

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Find Images With Loading Lazy and Fetchpriority
2+
3+
List all images that have `loading="lazy"` and `fetchpriority=high`.
4+
5+
> In reality you probably wouldn't really want `loading=lazy` and `fetchpriority=high` at the same time. That would mean don't load it initially, but when you start to load it, load it with high priority. Which is kind of contradictory! - [Barry Pollard](https://twitter.com/tunetheweb/status/1714750391890444582)
6+
7+
### Attribution
8+
9+
This snippet code it's based in the [script](https://twitter.com/csswizardry/status/1714697877245632832) shared by [Harry Roberts](https://csswizardry.com/).
10+
11+
#### Snippet
12+
13+
```js copy
14+
const MESSAGES = {
15+
good: `The current rendered code, don't have elements with loading="lazy" fetchpriority="high"`,
16+
bad: "In reality you probably wouldnt really want `loading=lazy` and `fetchpriority=high` at the same time. That would mean don't load it initially, but when you start to load it, load it with high priority. Which is kind of contradictory!"
17+
}
18+
const elementsLazyFetchpriority = document.querySelectorAll("[loading=lazy][fetchpriority=high]")
19+
const numLazyFetchpriority = elementsLazyFetchpriority.length
20+
const hasLazyFetchpriority = numLazyFetchpriority > 0
21+
22+
23+
if (hasLazyFetchpriority) {
24+
console.log(`%c The page has ${numLazyFetchpriority} image(s) with loading="lazy" fetchpriority="high"`, 'background: #222; color: lightcoral; padding: 0.5ch; font-size: 1.28em')
25+
elementsLazyFetchpriority.forEach((el) => console.log(el))
26+
27+
console.log(`%c ${MESSAGES.bad}`, 'background: #222; color: lightcoral; padding: 0.5ch; margin-top: 1em')
28+
} else {
29+
console.log(`%c ${MESSAGES.good}`, 'background: #222; color: lightgreen; padding: 0.5ch')
30+
}
31+
32+
33+
```

pages/index.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ A curated list of snippets to get Web Performance metrics to use in the browser
88
<img
99
style={{ aspectRatio: 342 / 214 }}
1010
sizes="85vw"
11-
srcset="https://res.cloudinary.com/nucliweb/image/upload/c_scale,f_auto,q_auto,w_600/v1685735993/webperf-snippets/DevTools-Snippets.png 600w,
11+
srcSet="https://res.cloudinary.com/nucliweb/image/upload/c_scale,f_auto,q_auto,w_600/v1685735993/webperf-snippets/DevTools-Snippets.png 600w,
1212
https://res.cloudinary.com/nucliweb/image/upload/c_scale,f_auto,q_auto,w_1200/v1685735993/webperf-snippets/DevTools-Snippets.png 1200w"
1313
src="https://res.cloudinary.com/nucliweb/image/upload/c_scale,f_auto,q_auto,w_342/v1685735993/webperf-snippets/DevTools-Snippets.png"
1414
loading="eager"

0 commit comments

Comments
 (0)