Skip to content

Commit 02f2975

Browse files
committed
Add new snippet Lazy Fetchpriority
1 parent 1e4aeed commit 02f2975

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
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 shared by [Harry Roberts](https://twitter.com/csswizardry/status/1714697877245632832).
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+
```

0 commit comments

Comments
 (0)