Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
9a66bc67
Commit
9a66bc67
authored
Jul 09, 2020
by
Tim Zallmann
Committed by
Phil Hughes
Jul 09, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Adds native lazy image loading
So no need for IntersectionObservers if supported
parent
0382457b
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
36 additions
and
12 deletions
+36
-12
app/assets/javascripts/lazy_loader.js
app/assets/javascripts/lazy_loader.js
+17
-12
app/views/layouts/_img_loader.html.haml
app/views/layouts/_img_loader.html.haml
+17
-0
app/views/layouts/application.html.haml
app/views/layouts/application.html.haml
+2
-0
No files found.
app/assets/javascripts/lazy_loader.js
View file @
9a66bc67
...
...
@@ -14,6 +14,10 @@ export default class LazyLoader {
scrollContainer
.
addEventListener
(
'
load
'
,
()
=>
this
.
register
());
}
static
supportsNativeLazyLoading
()
{
return
'
loading
'
in
HTMLImageElement
.
prototype
;
}
static
supportsIntersectionObserver
()
{
return
Boolean
(
window
.
IntersectionObserver
);
}
...
...
@@ -23,7 +27,9 @@ export default class LazyLoader {
()
=>
{
const
lazyImages
=
[].
slice
.
call
(
document
.
querySelectorAll
(
'
.lazy
'
));
if
(
LazyLoader
.
supportsIntersectionObserver
())
{
if
(
LazyLoader
.
supportsNativeLazyLoading
())
{
lazyImages
.
forEach
(
img
=>
LazyLoader
.
loadImage
(
img
));
}
else
if
(
LazyLoader
.
supportsIntersectionObserver
())
{
if
(
this
.
intersectionObserver
)
{
lazyImages
.
forEach
(
img
=>
this
.
intersectionObserver
.
observe
(
img
));
}
...
...
@@ -72,11 +78,14 @@ export default class LazyLoader {
}
register
()
{
if
(
LazyLoader
.
supportsIntersectionObserver
())
{
this
.
startIntersectionObserver
();
}
else
{
this
.
startLegacyObserver
();
if
(
!
LazyLoader
.
supportsNativeLazyLoading
())
{
if
(
LazyLoader
.
supportsIntersectionObserver
())
{
this
.
startIntersectionObserver
();
}
else
{
this
.
startLegacyObserver
();
}
}
this
.
startContentObserver
();
this
.
searchLazyImages
();
}
...
...
@@ -148,16 +157,12 @@ export default class LazyLoader {
static
loadImage
(
img
)
{
if
(
img
.
getAttribute
(
'
data-src
'
))
{
img
.
setAttribute
(
'
loading
'
,
'
lazy
'
);
let
imgUrl
=
img
.
getAttribute
(
'
data-src
'
);
// Only adding width + height for avatars for now
if
(
imgUrl
.
indexOf
(
'
/avatar/
'
)
>
-
1
&&
imgUrl
.
indexOf
(
'
?
'
)
===
-
1
)
{
let
targetWidth
=
null
;
if
(
img
.
getAttribute
(
'
width
'
))
{
targetWidth
=
img
.
getAttribute
(
'
width
'
);
}
else
{
targetWidth
=
img
.
width
;
}
if
(
targetWidth
)
imgUrl
+=
`?width=
${
targetWidth
}
`
;
const
targetWidth
=
img
.
getAttribute
(
'
width
'
)
||
img
.
width
;
imgUrl
+=
`?width=
${
targetWidth
}
`
;
}
img
.
setAttribute
(
'
src
'
,
imgUrl
);
img
.
removeAttribute
(
'
data-src
'
);
...
...
app/views/layouts/_img_loader.html.haml
0 → 100644
View file @
9a66bc67
=
javascript_tag
nonce:
true
do
:plain
if ('loading' in HTMLImageElement.prototype) {
document.querySelectorAll('img.lazy').forEach(img => {
img.loading = 'lazy';
let imgUrl = img.dataset.src;
// Only adding width + height for avatars for now
if (imgUrl.indexOf('/avatar/') > -1 && imgUrl.indexOf('?') === -1) {
const targetWidth = img.getAttribute('width') || img.width;
imgUrl += `?width=${targetWidth}`;
}
img.src = imgUrl;
img.removeAttribute('data-src');
img.classList.remove('lazy');
img.classList.add('js-lazy-loaded', 'qa-js-lazy-loaded');
});
}
app/views/layouts/application.html.haml
View file @
9a66bc67
...
...
@@ -13,4 +13,6 @@
=
render
'layouts/page'
,
sidebar:
sidebar
,
nav:
nav
=
footer_message
=
render
'layouts/img_loader'
=
yield
:scripts_body
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment