From 55dd189ef973aa2d519fa82eecb960a1eeeb082f Mon Sep 17 00:00:00 2001 From: Ragura Date: Tue, 23 Apr 2024 13:49:02 +0200 Subject: [PATCH] fix: ensure `src` comes after `loading` when rendering image (#1338) --- src/runtime/components/nuxt-img.ts | 2 +- test/unit/image.test.ts | 26 +++++++++++++------------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/runtime/components/nuxt-img.ts b/src/runtime/components/nuxt-img.ts index 37388aa42..0df2f93b8 100644 --- a/src/runtime/components/nuxt-img.ts +++ b/src/runtime/components/nuxt-img.ts @@ -147,11 +147,11 @@ export default defineComponent({ return () => h('img', { ref: imgEl, - src: src.value, ...import.meta.server ? { onerror: 'this.setAttribute(\'data-error\', 1)' } : {}, ...attrs.value, ...ctx.attrs, class: props.placeholder && !placeholderLoaded.value ? [props.placeholderClass] : undefined, + src: src.value, }) }, }) diff --git a/test/unit/image.test.ts b/test/unit/image.test.ts index b51016afa..bbc29c3a3 100644 --- a/test/unit/image.test.ts +++ b/test/unit/image.test.ts @@ -22,7 +22,7 @@ describe('Renders simple image', () => { }) it('Matches snapshot', () => { - expect(wrapper.html()).toMatchInlineSnapshot('""') + expect(wrapper.html()).toMatchInlineSnapshot(`""`) }) it('props.src is picked up by getImage()', () => { @@ -52,7 +52,7 @@ describe('Renders simple image', () => { densities: '1x 2x 3x', src: 'image.png', }) - expect(img.html()).toMatchInlineSnapshot('""') + expect(img.html()).toMatchInlineSnapshot(`""`) }) it('empty densities (fallback to global)', () => { @@ -63,7 +63,7 @@ describe('Renders simple image', () => { densities: '', src: 'image.png', }) - expect(img.html()).toMatchInlineSnapshot('""') + expect(img.html()).toMatchInlineSnapshot(`""`) }) it('empty string densities (fallback to global)', () => { @@ -74,7 +74,7 @@ describe('Renders simple image', () => { densities: ' ', src: 'image.png', }) - expect(img.html()).toMatchInlineSnapshot('""') + expect(img.html()).toMatchInlineSnapshot(`""`) }) it('error on invalid densities', () => { @@ -93,7 +93,7 @@ describe('Renders simple image', () => { height: 400, sizes: '150', }) - expect(img.html()).toMatchInlineSnapshot('""') + expect(img.html()).toMatchInlineSnapshot(`""`) }) it('with single sizes entry (responsive)', () => { @@ -103,7 +103,7 @@ describe('Renders simple image', () => { height: 400, sizes: 'sm:150', }) - expect(img.html()).toMatchInlineSnapshot('""') + expect(img.html()).toMatchInlineSnapshot(`""`) }) it('de-duplicates sizes & srcset', () => { @@ -113,7 +113,7 @@ describe('Renders simple image', () => { sizes: '200:200px,300:200px,400:400px,400:400px,500:500px,800:800px', src: 'image.png', }) - expect(img.html()).toMatchInlineSnapshot('""') + expect(img.html()).toMatchInlineSnapshot(`""`) }) it('encodes characters', () => { @@ -123,7 +123,7 @@ describe('Renders simple image', () => { sizes: '200,500:500,900:900', src: '/汉字.png', }) - expect(img.html()).toMatchInlineSnapshot('""') + expect(img.html()).toMatchInlineSnapshot(`""`) }) it('correctly sets crop', () => { @@ -133,7 +133,7 @@ describe('Renders simple image', () => { height: 2000, sizes: 'xs:100vw sm:100vw md:300px lg:350px xl:350px 2xl:350px', }) - expect(img.html()).toMatchInlineSnapshot('""') + expect(img.html()).toMatchInlineSnapshot(`""`) }) it('without sizes, but densities', () => { @@ -143,7 +143,7 @@ describe('Renders simple image', () => { height: 400, densities: '1x 2x 3x', }) - expect(img.html()).toMatchInlineSnapshot('""') + expect(img.html()).toMatchInlineSnapshot(`""`) }) it('with nonce', () => { @@ -307,7 +307,7 @@ describe('Renders image, applies module config', () => { sizes: '200,500:500,900:900', }, }) - expect(img.html()).toMatchInlineSnapshot('""') + expect(img.html()).toMatchInlineSnapshot(`""`) }) it('Module config .quality + props.quality => props.quality applies', () => { @@ -327,7 +327,7 @@ describe('Renders image, applies module config', () => { quality: 90, }, }) - expect(img.html()).toMatchInlineSnapshot('""') + expect(img.html()).toMatchInlineSnapshot(`""`) }) it('Without quality config => default image', () => { @@ -345,7 +345,7 @@ describe('Renders image, applies module config', () => { sizes: '200,500:500,900:900', }, }) - expect(img.html()).toMatchInlineSnapshot('""') + expect(img.html()).toMatchInlineSnapshot(`""`) }) })