Skip to content

fix: emit error events from <NuxtImg> placeholder - #1640

Merged
danielroe merged 2 commits into
nuxt:mainfrom
jianxing-xu:fix_nuxtimg_onerror
Dec 20, 2024
Merged

fix: emit error events from <NuxtImg> placeholder#1640
danielroe merged 2 commits into
nuxt:mainfrom
jianxing-xu:fix_nuxtimg_onerror

Conversation

@jianxing-xu

Copy link
Copy Markdown
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
}

@Baroshem Baroshem left a comment

Copy link
Copy Markdown
Collaborator

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

codecov-commenter commented Dec 17, 2024

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 62.87%. Comparing base (25c03a4) to head (92fefef).
⚠️ Report is 429 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.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@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
@github-actions github-actions Bot mentioned this pull request Dec 20, 2024
@github-actions github-actions Bot mentioned this pull request Jan 6, 2025
@github-actions github-actions Bot mentioned this pull request Jul 30, 2026
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