Skip to content

Commit 9a608c3

Browse files
Optimize <ImageAsset /> component (#415)
* adds `decoding="async"` attribute to images * adds default `loading="lazy"` attribute to images
1 parent bc7fe55 commit 9a608c3

File tree

3 files changed

+14
-0
lines changed

3 files changed

+14
-0
lines changed

src/components/ImageAsset.vue

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
v-if="fallbackImageSrcSet"
1414
class="fallback"
1515
title="Image failed to load"
16+
decoding="async"
1617
:alt="alt"
1718
:srcset="fallbackImageSrcSet"
1819
/>
@@ -31,6 +32,8 @@
3132
v-if="prefersDark && darkVariantAttributes"
3233
v-bind="darkVariantAttributes"
3334
ref="img"
35+
decoding="async"
36+
:loading="loading"
3437
:alt="alt"
3538
:width="darkVariantAttributes.width || optimalWidth"
3639
:height="(darkVariantAttributes.width || optimalWidth) ? 'auto' : null"
@@ -43,6 +46,8 @@
4346
v-else
4447
v-bind="defaultAttributes"
4548
ref="img"
49+
decoding="async"
50+
:loading="loading"
4651
:alt="alt"
4752
:width="defaultAttributes.width || optimalWidth"
4853
:height="(defaultAttributes.width || optimalWidth) ? 'auto' : null"
@@ -129,6 +134,10 @@ export default {
129134
type: Array,
130135
required: true,
131136
},
137+
loading: {
138+
type: String,
139+
default: 'lazy',
140+
},
132141
},
133142
methods: {
134143
handleImageLoadError() {

tests/unit/components/Asset.spec.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ describe('Asset', () => {
185185
expect(asset.exists()).toBe(true);
186186
expect(asset.props()).toEqual({
187187
alt: image.alt,
188+
loading: "lazy",
188189
variants: image.variants,
189190
});
190191
});
@@ -194,6 +195,7 @@ describe('Asset', () => {
194195
const asset = wrapper.find(ImageAsset);
195196
expect(asset.props()).toEqual({
196197
alt: image.alt,
198+
loading: "lazy",
197199
variants: image.variants,
198200
});
199201
});

tests/unit/components/ImageAsset.spec.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ describe('ImageAsset', () => {
5454
expect(image.attributes('width')).toBe('1202');
5555
expect(image.attributes('height')).toBe('auto');
5656
expect(image.attributes('alt')).toBe(alt);
57+
expect(image.attributes('decoding')).toBe('async');
5758
});
5859

5960
it('renders an image that has one variant with no appearance trait', () => {
@@ -85,6 +86,7 @@ describe('ImageAsset', () => {
8586
expect(image.attributes('width')).toBe('300');
8687
expect(image.attributes('height')).toBe('auto');
8788
expect(image.attributes('alt')).toBe('');
89+
expect(image.attributes('decoding')).toBe('async');
8890
});
8991

9092
it('renders an image that has two light variants', () => {
@@ -128,6 +130,7 @@ describe('ImageAsset', () => {
128130
expect(image.attributes('srcset')).toBe(`${url2x} 2x, ${url3x} 3x`);
129131
expect(image.attributes('width')).toBe('1202');
130132
expect(image.attributes('height')).toBe('auto');
133+
expect(image.attributes('decoding')).toBe('async');
131134
});
132135

133136
it('renders an image that has two dark variants', () => {

0 commit comments

Comments
 (0)