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 error events from <NuxtImg> placeholder #1640

Merged
merged 2 commits into from
Dec 20, 2024

Conversation

jianxing-xu
Copy link
Contributor

resolves #1639

  • πŸ“– Documentation (updates to the documentation or readme)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • πŸ‘Œ Enhancement (improving an existing functionality)
  • ✨ New feature (a non-breaking change that adds functionality)
  • 🧹 Chore (updates to the build process or auxiliary tools and libraries)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

πŸ“š Description

I need to deal with the ERROR event when the picture is loaded, but if I use the Placehoder attribute at the same time, the ERROR event will not be thrown out

onMounted(() => {
  if (placeholder.value) {
    const img = new Image()

    if (mainSrc.value) {
      img.src = mainSrc.value
    }

    if (props.sizes) {
      img.sizes = sizes.value.sizes || ''
      img.srcset = sizes.value.srcset
    }
    // !!! Here, if the IMG loading error, there is no ERROR incident !!!
    img.onload = (event) => {
      placeholderLoaded.value = true
      emit('load', event)
    }
    // add this to fix
    img.onerror = (event) => {
      emit('error', event)
    }

    markFeatureUsage('nuxt-image')

    return
  }
  // ...other
}

Copy link
Collaborator

@Baroshem Baroshem left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me! :)

@codecov-commenter
Copy link

codecov-commenter commented Dec 17, 2024

Codecov Report

All modified and coverable lines are covered by tests βœ…

Project coverage is 62.87%. Comparing base (25c03a4) to head (92fefef).
Report is 2 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1640      +/-   ##
==========================================
+ Coverage   62.83%   62.87%   +0.03%     
==========================================
  Files          79       79              
  Lines        3536     3539       +3     
  Branches      412      412              
==========================================
+ Hits         2222     2225       +3     
  Misses       1285     1285              
  Partials       29       29              

β˜” View full report in Codecov by Sentry.
πŸ“’ Have feedback on the report? Share it here.

@danielroe danielroe changed the title fix: add onerror event trigger when use with placeholder fix: emit error events from <NuxtImg> placeholder Dec 20, 2024
@danielroe danielroe merged commit 5fd221a into nuxt:main Dec 20, 2024
2 checks passed
@github-actions github-actions bot mentioned this pull request Dec 20, 2024
@github-actions github-actions bot mentioned this pull request Jan 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

When using Placeholder, the ERROR event will not be triggered
4 participants