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

fix: emit synthetic load/error events on initial hydration #842

Merged
merged 4 commits into from
Jun 6, 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
24 changes: 19 additions & 5 deletions src/runtime/components/nuxt-img.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useImage } from '../composables'
import { parseSize } from '../utils'
import { prerenderStaticImages } from '../utils/prerender'
import { baseImageProps, useBaseImage } from './_base'
import { useHead } from '#imports'
import { useHead, useNuxtApp } from '#imports'

export const imgProps = {
...baseImageProps,
Expand All @@ -13,7 +13,7 @@ export const imgProps = {
export default defineComponent({
name: 'NuxtImg',
props: imgProps,
emits: ['load'],
emits: ['load', 'error'],
setup: (props, ctx) => {
const $img = useImage()
const _base = useBaseImage(props)
Expand Down Expand Up @@ -95,6 +95,8 @@ export default defineComponent({

const imgEl = ref<HTMLImageElement>()

const nuxtApp = useNuxtApp()
const initialLoad = nuxtApp.isHydrating
onMounted(() => {
if (placeholder.value) {
const img = new Image()
Expand All @@ -109,17 +111,29 @@ export default defineComponent({
return
}

if (imgEl.value) {
imgEl.value.onload = (event) => {
ctx.emit('load', event)
if (!imgEl.value) { return }

if (imgEl.value.complete && initialLoad) {
if (imgEl.value.getAttribute('data-error')) {
ctx.emit('error', new Event('error'))
} else {
ctx.emit('load', new Event('load'))
}
}

imgEl.value.onload = (event) => {
ctx.emit('load', event)
}
imgEl.value.onerror = (event) => {
ctx.emit('error', event)
}
})

return () => h('img', {
ref: imgEl,
key: src.value,
src: src.value,
...process.server ? { onerror: 'this.setAttribute(\'data-error\', 1)' } : {},
...attrs.value,
...ctx.attrs
})
Expand Down
16 changes: 11 additions & 5 deletions src/runtime/components/nuxt-picture.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { h, defineComponent, ref, computed, onMounted } from 'vue'
import { prerenderStaticImages } from '../utils/prerender'
import { useBaseImage, baseImageProps } from './_base'
import { useImage, useHead } from '#imports'
import { useImage, useHead, useNuxtApp } from '#imports'
import { getFileExtension } from '#image'

export const pictureProps = {
Expand Down Expand Up @@ -81,11 +81,16 @@ export default defineComponent({
}
}

const nuxtApp = useNuxtApp()
const initialLoad = nuxtApp.isHydrating
onMounted(() => {
if (imgEl.value) {
imgEl.value.onload = (event) => {
ctx.emit('load', event)
}
if (!imgEl.value) { return }

if (imgEl.value.complete && initialLoad && !imgEl.value.getAttribute('data-error')) {
ctx.emit('load', new Event('load'))
}
imgEl.value.onload = (event) => {
ctx.emit('load', event)
}
})

Expand All @@ -100,6 +105,7 @@ export default defineComponent({
h('img', {
ref: imgEl,
..._base.attrs.value,
...process.server ? { onerror: 'this.setAttribute(\'data-error\', 1)' } : {},
...imgAttrs,
src: sources.value[0].src,
sizes: sources.value[0].sizes,
Expand Down
2 changes: 0 additions & 2 deletions test/e2e/no-ssr.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ describe('browser (ssr: false)', () => {

expect(requests.map(r => r.replace(url('/'), '/')).filter(r => r !== providerPath && !r.match(/\.(js|css)/))).toMatchSnapshot()
})
})

describe('browser (ssr: false) common', () => {
it('should emit load and error events', async () => {
const page = await createPage(url('/events'))
const logs: string[] = []
Expand Down
14 changes: 14 additions & 0 deletions test/e2e/ssr.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,20 @@ describe('browser (ssr: true)', () => {
expect(requests.map(r => r.replace(url('/'), '/')).filter(r => r !== providerPath && !r.match(/\.(js|css)/))).toMatchSnapshot()
})

it('should emit load and error events', async () => {
const page = await createPage()
const logs: string[] = []

page.on('console', (msg) => { logs.push(msg.text()) })

page.goto(url('/events'))

await page.waitForLoadState('networkidle')

expect(logs.filter(log => log === 'Image was loaded').length).toBe(4)
expect(logs.filter(log => log === 'Error loading image').length).toBe(1)
})

it('works with runtime ipx', async () => {
const res = await fetch(url('/_ipx/s_300x300/images/colors.jpg'))
expect(res.headers.get('content-type')).toBe('image/jpeg')
Expand Down