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

Set lazy loading for images. #2179

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion application/src/Media/Renderer/Youtube.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function render(PhpRenderer $view, MediaRepresentation $media,
}
$url->setQuery($query);
$embed = sprintf(
'<iframe width="%s" height="%s" src="%s" frameborder="0"%s></iframe>',
'<iframe loading="lazy" width="%s" height="%s" src="%s" frameborder="0"%s></iframe>',
$view->escapeHtml($options['width']),
$view->escapeHtml($options['height']),
$view->escapeHtml($url),
Expand Down
3 changes: 2 additions & 1 deletion application/src/Stdlib/Oembed.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,9 @@ public function renderOembed(PhpRenderer $view, array $oembed)
$type = $oembed['type'] ?? null;
if ('photo' === $type) {
$url = $oembed['url'] ?? null;
$data = $oembed['oembed'] ?? [];
return sprintf(
'<img src="%s" width="%s" height="%s" alt="%s">',
'<img loading="lazy" src="%s" width="%s" height="%s" alt="%s">',
$view->escapeHtml($url),
$view->escapeHtml($oembed['width'] ?? ''),
$view->escapeHtml($oembed['height'] ?? ''),
Expand Down
2 changes: 1 addition & 1 deletion application/src/View/Helper/IiifViewer.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ public function __invoke(array $query, array $options = [])
$width = $options['width'] ?? '100%';
$height = $options['height'] ?? '700px';
$src = $view->url('iiif-viewer', [], ['force_canonical' => true, 'query' => $query]);
return sprintf('<iframe style="width: %s; height: %s;" src="%s"></iframe>', $width, $height, $view->escapeHtml($src));
return sprintf('<iframe loading="lazy" style="width: %s; height: %s;" src="%s"></iframe>', $width, $height, $view->escapeHtml($src));
}
}
7 changes: 7 additions & 0 deletions application/src/View/Helper/Thumbnail.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ public function __invoke(AbstractRepresentation $representation, $type, array $a
$params = $triggerHelper('view_helper.thumbnail.attribs', $params, true);
$attribs = $params['attribs'];

// Include element for lazy loading. See https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/loading
if (!isset($attribs['loading'])) {
// Due to a bug in firefox, the attribute "loading" should be set
// before src (see https://bugzilla.mozilla.org/show_bug.cgi?id=1647077).
$attribs = ['loading' => 'lazy'] + $attribs;
}

if (!isset($attribs['alt'])) {
$attribs['alt'] = $representation->thumbnailAltText();
}
Expand Down
4 changes: 2 additions & 2 deletions application/view/common/asset-form.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ if ($assetId) {
<?php if ($asset): ?>
<span class="selected-asset">
<?php
echo sprintf('<img class="selected-asset-image" src="%s" alt="%s"><div class="selected-asset-name">%s</div>',
echo sprintf('<img loading="lazy" class="selected-asset-image" src="%s" alt="%s"><div class="selected-asset-name">%s</div>',
$this->escapeHtml($asset->assetUrl()),
$this->escapeHtml($asset->altText()),
$this->escapeHtml($asset->name())
Expand All @@ -28,7 +28,7 @@ if ($assetId) {
</span>
<?php else: ?>
<span class="selected-asset" style="display: none;">
<img class="selected-asset-image"><div class="selected-asset-name"></div>
<img loading="lazy" class="selected-asset-image"><div class="selected-asset-name"></div>
</span>
<?php endif; ?>
<span class="no-selected-asset">
Expand Down
2 changes: 1 addition & 1 deletion application/view/common/asset-options.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ $form->prepare();
$selectedAssetTemplate = '
<h3><span class="selected-asset-name">' . $translate('No Asset') . '</span></h3>
<span class="selected-asset">
<img class="selected-asset-image">
<img loading="lazy" class="selected-asset-image">
<div class="selected-asset-name"></div>
<input type="hidden" name="selected-asset-id" class="selected-asset-id">
<span class="none-selected">' . $translate('[No asset selected]') . '</span>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ $this->headScript()->appendFile($this->assetUrl('js/site-theme.js', 'Omeka'));
<span class="error"><?php echo sprintf($translate('This theme requires Omeka S %s'), $theme->getIni('omeka_version_constraint')); ?></span>
<?php endif; ?>

<div class="theme-thumbnail"><img src="<?php echo $this->escapeHtml($thumbnailUrl); ?>"></div>
<div class="theme-thumbnail"><img loading="lazy" src="<?php echo $this->escapeHtml($thumbnailUrl); ?>"></div>

<h4><?php echo $this->escapeHtml($theme->getName()); ?></h4>
<div class="theme-meta">
Expand Down
2 changes: 1 addition & 1 deletion application/view/omeka/site-admin/index/theme.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ $fallbackThumbnailUrl = $this->assetUrl('img/theme.jpg', 'Omeka');
<div class="current-theme<?php echo Manager::STATE_ACTIVE !== $currentTheme->getState() ? ' invalid' : ''; ?>">
<?php $localThumbnailUrl = OMEKA_PATH . $currentTheme->getThumbnail(); ?>
<?php $absoluteThumbnailUrl = $this->basePath() . $currentTheme->getThumbnail(); ?>
<div class="theme-thumbnail"><img src="<?php echo $escape((file_exists($localThumbnailUrl)) ? $absoluteThumbnailUrl : $fallbackThumbnailUrl); ?>"></div>
<div class="theme-thumbnail"><img loading="lazy" src="<?php echo $escape((file_exists($localThumbnailUrl)) ? $absoluteThumbnailUrl : $fallbackThumbnailUrl); ?>"></div>

<div class="current-theme-info">
<h3>
Expand Down
Loading