Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add intersection observer for lazy loading of images #291

Merged
merged 2 commits into from
Apr 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions locales/es.po
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ msgstr ""
"Project-Id-Version: simple-icons-website\n"
"Report-Msgid-Bugs-To: https://github.com/simple-icons/simple-icons-website/issues\n"
"POT-Creation-Date: 2022-09-25T01:25:24.834Z\n"
"PO-Revision-Date: 2022-10-02 16:42+0800\n"
"PO-Revision-Date: 2023-04-05T20:12:22.666Z\n"
"Last-Translator: @mondeja <https://github.com/mondeja>\n"
"Language-Team: es <https://github.com/simple-icons>\n"
"Language: es\n"
Expand Down Expand Up @@ -67,9 +67,6 @@ msgstr "Envía una petición"
msgid "$iconTitle SVG"
msgstr "SVG de $iconTitle"

msgid "$iconTitle icon"
msgstr "Icono de $iconTitle"

msgid "$iconTitle guidelines"
msgstr "Directrices de $iconTitle"

Expand Down
5 changes: 1 addition & 4 deletions locales/fr.po
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ msgstr ""
"Project-Id-Version: simple-icons-website\n"
"Report-Msgid-Bugs-To: https://github.com/simple-icons/simple-icons-website/issues\n"
"POT-Creation-Date: 2022-09-25T01:25:24.834Z\n"
"PO-Revision-Date: 2022-10-02 16:42+0800\n"
"PO-Revision-Date: 2023-04-05T20:12:22.666Z\n"
"Last-Translator: @mondeja <https://github.com/mondeja>\n"
"Language-Team: fr <https://github.com/simple-icons>\n"
"Language: fr\n"
Expand Down Expand Up @@ -79,9 +79,6 @@ msgstr "Soumettre une demande"
msgid "$iconTitle SVG"
msgstr "$iconTitle SVG"

msgid "$iconTitle icon"
msgstr "Icône $iconTitle"

msgid "$iconTitle guidelines"
msgstr "Directives $iconTitle"

Expand Down
5 changes: 1 addition & 4 deletions locales/it.po
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ msgstr ""
"Project-Id-Version: simple-icons-website\n"
"Report-Msgid-Bugs-To: https://github.com/simple-icons/simple-icons-website/issues\n"
"POT-Creation-Date: 2022-09-26T15:39:06.158Z\n"
"PO-Revision-Date: 2022-10-02 16:42+0800\n"
"PO-Revision-Date: 2023-04-05T20:12:22.666Z\n"
"Last-Translator: @mondeja <https://github.com/mondeja>\n"
"Language-Team: it <https://github.com/simple-icons>\n"
"Language: it\n"
Expand Down Expand Up @@ -85,9 +85,6 @@ msgstr "Invia una richiesta"
msgid "$iconTitle SVG"
msgstr "$iconTitle SVG"

msgid "$iconTitle icon"
msgstr "$iconTitle icona"

msgid "$iconTitle guidelines"
msgstr "$iconTitle linee guida"

Expand Down
5 changes: 1 addition & 4 deletions locales/ja-JP.po
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ msgstr ""
"Project-Id-Version: simple-icons-website\n"
"Report-Msgid-Bugs-To: https://github.com/simple-icons/simple-icons-website/issues\n"
"POT-Creation-Date: 2022-09-26T15:39:06.158Z\n"
"PO-Revision-Date: 2023-01-16 14:15+0000\n"
"PO-Revision-Date: 2023-04-05T20:12:22.666Z\n"
"Last-Translator: @adamrusted <https://github.com/adamrusted>\n"
"Language-Team: ja-JP <https://github.com/simple-icons>\n"
"Language: ja-JP\n"
Expand Down Expand Up @@ -85,9 +85,6 @@ msgstr "リクエストを提出する"
msgid "$iconTitle SVG"
msgstr "$iconTitle SVG"

msgid "$iconTitle icon"
msgstr "$iconTitle アイコン"

msgid "$iconTitle guidelines"
msgstr "$iconTitle 鑑札"

Expand Down
5 changes: 1 addition & 4 deletions locales/pt.po
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ msgstr ""
"Project-Id-Version: simple-icons-website\n"
"Report-Msgid-Bugs-To: https://github.com/simple-icons/simple-icons-website/issues\n"
"POT-Creation-Date: 2022-10-07T19:52:50.250Z\n"
"PO-Revision-Date: 2022-10-07 22:18+0200\n"
"PO-Revision-Date: 2023-04-05T20:12:22.666Z\n"
"Last-Translator: @mondeja <https://github.com/mondeja>\n"
"Language-Team: pt <https://github.com/simple-icons>\n"
"Language: pt\n"
Expand Down Expand Up @@ -115,9 +115,6 @@ msgstr "Compactar"
msgid "$iconTitle SVG"
msgstr "$iconTitle SVG"

msgid "$iconTitle icon"
msgstr "ícone $iconTitle"

msgid "Removal at $version"
msgstr "Remoção na $version"

Expand Down
5 changes: 1 addition & 4 deletions locales/zh-CN.po
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ msgstr ""
"Project-Id-Version: simple-icons-website\n"
"Report-Msgid-Bugs-To: https://github.com/simple-icons/simple-icons-website/issues\n"
"POT-Creation-Date: 2022-09-27T13:42:46.869Z\n"
"PO-Revision-Date: 2022-10-02 16:42+0800\n"
"PO-Revision-Date: 2023-04-05T20:12:22.666Z\n"
"Last-Translator: @LitoMore <https://github.com/LitoMore>\n"
"Language-Team: zh-CN <https://github.com/simple-icons>\n"
"Language: zh-CN\n"
Expand Down Expand Up @@ -88,9 +88,6 @@ msgstr "提交请求"
msgid "$iconTitle SVG"
msgstr "$iconTitle SVG"

msgid "$iconTitle icon"
msgstr "$iconTitle 图标"

msgid "Deprecated"
msgstr "已废弃"

Expand Down
5 changes: 1 addition & 4 deletions locales/zh-HK.po
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ msgstr ""
"Project-Id-Version: simple-icons-website\n"
"Report-Msgid-Bugs-To: https://github.com/simple-icons/simple-icons-website/issues\n"
"POT-Creation-Date: 2022-09-28T13:51:38.923Z\n"
"PO-Revision-Date: 2022-10-02 16:41+0800\n"
"PO-Revision-Date: 2023-04-05T20:12:22.666Z\n"
"Last-Translator: @LitoMore <https://github.com/LitoMore>\n"
"Language-Team: zh-HK <https://github.com/simple-icons>\n"
"Language: zh-HK\n"
Expand Down Expand Up @@ -88,9 +88,6 @@ msgstr "建立支援服務單"
msgid "$iconTitle SVG"
msgstr "$iconTitle SVG"

msgid "$iconTitle icon"
msgstr "$iconTitle 圖標"

msgid "Deprecated"
msgstr "已過時(未來將停用)"

Expand Down
5 changes: 1 addition & 4 deletions locales/zh-TW.po
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ msgstr ""
"Project-Id-Version: simple-icons-website\n"
"Report-Msgid-Bugs-To: https://github.com/simple-icons/simple-icons-website/issues\n"
"POT-Creation-Date: 2022-09-28T14:14:50.054Z\n"
"PO-Revision-Date: 2022-10-02 16:41+0800\n"
"PO-Revision-Date: 2023-04-05T20:12:22.666Z\n"
"Last-Translator: @LitoMore <https://github.com/LitoMore>\n"
"Language-Team: zh-TW <https://github.com/simple-icons>\n"
"Language: zh-TW\n"
Expand Down Expand Up @@ -88,9 +88,6 @@ msgstr "提交服務單"
msgid "$iconTitle SVG"
msgstr "$iconTitle SVG"

msgid "$iconTitle icon"
msgstr "$iconTitle 圖標"

msgid "Deprecated"
msgstr "已過時(未來將停用)"

Expand Down
8 changes: 3 additions & 5 deletions public/index.pug
Original file line number Diff line number Diff line change
Expand Up @@ -378,11 +378,9 @@ html(lang=lang)
title=`${ t_('$iconTitle SVG').replace('$iconTitle', icon.localizedTitle || icon.title) }`,
disabled
)
img.icon-preview(
src=`/icons/${icon.slug}.svg`,
loading='lazy',
alt=`${ t_('$iconTitle icon').replace('$iconTitle', icon.localizedTitle || icon.title) }`
)
//- The images are lazy loaded via Javascript with an IntersectionObserver
//- using loading=lazy as fallback for browsers that don't support it.
img.icon-preview(d-src=`/icons/${icon.slug}.svg`)
if icon.deprecatedAt
span.deprecated
span.deprecated__text #{ t_('Deprecated') }
Expand Down
6 changes: 6 additions & 0 deletions public/scripts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ document.body.classList.remove('no-js');
const storage = newStorage(localStorage);
const orderingControls = initOrdering(window, document, storage, domUtils);

const detachedInitIntersectionObserver = async () => {
const { default: initIntersectionObserver } = await import('./iobserver.js');
initIntersectionObserver(document);
};

const detachedInitColorScheme = async () => {
const { default: initColorScheme } = await import('./color-scheme.js');
initColorScheme(document, storage);
Expand Down Expand Up @@ -52,6 +57,7 @@ const detachedInitLanguageSelector = async () => {
};

(() => {
detachedInitIntersectionObserver();
detachedInitColorScheme();
detachedInitCopyButtons();
detachedInitSearch();
Expand Down
45 changes: 45 additions & 0 deletions public/scripts/iobserver.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* @fileoverview
* Lazy loading of images using an intersection observer.
*/

export default (document) => {
const images = document.querySelectorAll('img[d-src]');

if ('IntersectionObserver' in window) {
const lazyLoad = (target) => {
const io = new IntersectionObserver(
(entries, observer) => {
entries.forEach((entry) => {
if (entry.isIntersecting) {
const img = entry.target;
const src = img.getAttribute('d-src');
img.onload = () => {
img.classList.add('ld');
};
img.setAttribute('src', src);
img.removeAttribute('d-src');
observer.disconnect();
}
});
},
{ threshold: [0], rootMargin: '80px' },
);

io.observe(target);
};
images.forEach(lazyLoad);
} else {
// Browser does not support IntersectionObserver,
// so we load using the loading=lazy attribute
for (const img of images) {
const src = img.getAttribute('d-src');
img.removeAttribute('d-src');
img.onload = () => {
img.classList.add('ld');
};
img.setAttribute('loading', 'lazy');
img.setAttribute('src', src);
mondeja marked this conversation as resolved.
Show resolved Hide resolved
}
}
};
9 changes: 9 additions & 0 deletions public/stylesheet.css
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,15 @@ body.dark .icon-preview {
filter: invert(1);
}

.icon-preview {
opacity: 0;
}
/* Transition when image loaded */
.icon-preview.ld {
opacity: 1;
transition: opacity 260ms;
}

/* General */

body {
Expand Down