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

First image return error "Missing alt" while the next image succeeds with the same content #153

Closed
adilsonfsantos opened this issue Jun 21, 2022 · 4 comments

Comments

@adilsonfsantos
Copy link

Hello everybody, in first place thanks for the plugin.

While trying to implement eleventy-img in my post.liquid layout, 11ty return the "Missing `alt` " error:

[11ty] 1. Having trouble writing template: "tmp/dist/projetos/fundacao/index.html" (via EleventyTemplateError)
[11ty] 2. Missing `alt` on responsiveimage from: src/assets/images/oscar-americano-wide.png, file:./tmp/src/_includes/layouts/post.liquid, line:19, col:7 (via RenderError)
[11ty] 3. Missing `alt` on responsiveimage from: src/assets/images/oscar-americano-wide.png (via Error)
[11ty] 
[11ty] Original error stack trace: Error: Missing `alt` on responsiveimage from: src/assets/images/oscar-americano-wide.png
[11ty]     at Object.imageShortcode (/home/adilson/Dev/Portfolio-11ty/.eleventy.js:60:11)
[11ty]     at Object.<anonymous> (/home/adilson/Dev/Portfolio-11ty/node_modules/@11ty/eleventy/src/BenchmarkGroup.js:32:26)
[11ty]     at Object.render (/home/adilson/Dev/Portfolio-11ty/node_modules/@11ty/eleventy/src/Engines/Liquid.js:153:25)
[11ty]     at async TemplateLayout.render (/home/adilson/Dev/Portfolio-11ty/node_modules/@11ty/eleventy/src/TemplateLayout.js:161:25)
[11ty]     at async Template.renderPageEntry (/home/adilson/Dev/Portfolio-11ty/node_modules/@11ty/eleventy/src/Template.js:853:17)
[11ty]     at async /home/adilson/Dev/Portfolio-11ty/node_modules/@11ty/eleventy/src/Template.js:883:21
[11ty]     at async Promise.all (index 0)
[11ty]     at async Promise.all (index 6)
[11ty]     at async Eleventy.executeBuild (/home/adilson/Dev/Portfolio-11ty/node_modules/@11ty/eleventy/src/Eleventy.js:1003:13)

post.liquid

      {% assign picture = image | prepend : "src" %}
      {% images picture, "Alt text test", "post__splash--picture", "post__splash--image" %}

eleventy.js

const Image = require("@11ty/eleventy-img");

async function imageShortcode(src, alt, classParent, classDescendent, sizes = "100vw") {
  if(alt === undefined) {
    // You bet we throw an error on missing alt (alt="" works okay)
    throw new Error(`Missing \`alt\` on responsiveimage from: ${src}`);
  }

  let metadata = await Image(src, {
    urlPath: "/assets/images",
    outputDir: "tmp/dist/assets/images",
    widths: [42, 320, 480, 768, 992, 1200, 1920],
    formats: ["avif", "webp", "jpeg"],
    sharpOptions: {
      ChromaSubsampling: "4:4:4",
      Progressive: true,
      Quality: 95,
    }
  });

  let lowsrc = metadata.jpeg[0];
  let highsrc = metadata.jpeg[metadata.jpeg.length - 1];

  return `<picture class="lazy ${classParent}">
    ${Object.values(metadata).map(imageFormat => {
      return `  <source type="${imageFormat[0].sourceType}" srcset="${imageFormat.map(entry => entry.srcset).join(", ")}" sizes="${sizes}">`;
    }).join("\n")}
      <img
        class="lazyload ${classDescendent}"
        src="${lowsrc.url}"
        width="${highsrc.width}"
        height="${highsrc.height}"
        alt="${alt}"
        loading="lazy"
        decoding="async">
    </picture>`;
}

eleventyConfig.addLiquidShortcode("images", imageShortcode);

So to test further i disabled the if(alt === undefined) from eleventy.js and by trying with the same content the results are fine for the second image but the first stiil have the same error

post.liquid

      {% assign picture = image | prepend : "src" %}
      {% images picture, "Alt text test", "post__splash--picture", "post__splash--image" %}
      {% images picture, "Alt text test", "post__splash--picture", "post__splash--image" %}

Result

<picture class="lazy undefined">
      <source type="image/avif" srcset="/assets/images/uHbSUDFpaB-42.avif 42w, /assets/images/uHbSUDFpaB-320.avif 320w, /assets/images/uHbSUDFpaB-480.avif 480w, /assets/images/uHbSUDFpaB-768.avif 768w, /assets/images/uHbSUDFpaB-992.avif 992w, /assets/images/uHbSUDFpaB-1200.avif 1200w, /assets/images/uHbSUDFpaB-1920.avif 1920w" sizes="100vw">
  <source type="image/webp" srcset="/assets/images/uHbSUDFpaB-42.webp 42w, /assets/images/uHbSUDFpaB-320.webp 320w, /assets/images/uHbSUDFpaB-480.webp 480w, /assets/images/uHbSUDFpaB-768.webp 768w, /assets/images/uHbSUDFpaB-992.webp 992w, /assets/images/uHbSUDFpaB-1200.webp 1200w, /assets/images/uHbSUDFpaB-1920.webp 1920w" sizes="100vw">
  <source type="image/jpeg" srcset="/assets/images/uHbSUDFpaB-42.jpeg 42w, /assets/images/uHbSUDFpaB-320.jpeg 320w, /assets/images/uHbSUDFpaB-480.jpeg 480w, /assets/images/uHbSUDFpaB-768.jpeg 768w, /assets/images/uHbSUDFpaB-992.jpeg 992w, /assets/images/uHbSUDFpaB-1200.jpeg 1200w, /assets/images/uHbSUDFpaB-1920.jpeg 1920w" sizes="100vw">
      <img class="lazyload undefined" src="/assets/images/uHbSUDFpaB-42.jpeg" alt="undefined" loading="lazy" decoding="async" width="1920" height="960">
    </picture>
      <picture class="lazy post__splash--picture">
      <source type="image/avif" srcset="/assets/images/uHbSUDFpaB-42.avif 42w, /assets/images/uHbSUDFpaB-320.avif 320w, /assets/images/uHbSUDFpaB-480.avif 480w, /assets/images/uHbSUDFpaB-768.avif 768w, /assets/images/uHbSUDFpaB-992.avif 992w, /assets/images/uHbSUDFpaB-1200.avif 1200w, /assets/images/uHbSUDFpaB-1920.avif 1920w" sizes="100vw">
  <source type="image/webp" srcset="/assets/images/uHbSUDFpaB-42.webp 42w, /assets/images/uHbSUDFpaB-320.webp 320w, /assets/images/uHbSUDFpaB-480.webp 480w, /assets/images/uHbSUDFpaB-768.webp 768w, /assets/images/uHbSUDFpaB-992.webp 992w, /assets/images/uHbSUDFpaB-1200.webp 1200w, /assets/images/uHbSUDFpaB-1920.webp 1920w" sizes="100vw">
  <source type="image/jpeg" srcset="/assets/images/uHbSUDFpaB-42.jpeg 42w, /assets/images/uHbSUDFpaB-320.jpeg 320w, /assets/images/uHbSUDFpaB-480.jpeg 480w, /assets/images/uHbSUDFpaB-768.jpeg 768w, /assets/images/uHbSUDFpaB-992.jpeg 992w, /assets/images/uHbSUDFpaB-1200.jpeg 1200w, /assets/images/uHbSUDFpaB-1920.jpeg 1920w" sizes="100vw">
      <img class="lazyload post__splash--image" src="/assets/images/uHbSUDFpaB-42.jpeg" alt="Alt text test" loading="lazy" decoding="async" width="1920" height="960">
    </picture>

Terminal output with Debug Mode

[11ty] 1. Having trouble writing template: "tmp/dist/projetos/fundacao/index.html" (via EleventyTemplateError)
  Eleventy:EleventyErrorHandler (error stack): EleventyTemplateError: Having trouble writing template: "tmp/dist/projetos/fundacao/index.html"
  Eleventy:EleventyErrorHandler     at /home/adilson/Dev/Portfolio-11ty/node_modules/@11ty/eleventy/src/TemplateWriter.js:291:15
  Eleventy:EleventyErrorHandler     at async Promise.all (index 6)
  Eleventy:EleventyErrorHandler     at async Eleventy.executeBuild (/home/adilson/Dev/Portfolio-11ty/node_modules/@11ty/eleventy/src/Eleventy.js:1003:13) +0ms
[11ty] 2. Missing `alt` on responsiveimage from: src/assets/images/oscar-americano-wide.png, file:./tmp/src/_includes/layouts/post.liquid, line:19, col:7 (via RenderError)
  Eleventy:EleventyErrorHandler (error stack): Missing `alt` on responsiveimage from: src/assets/images/oscar-americano-wide.png, file:./tmp/src/_includes/layouts/post.liquid, line:19, col:7
  Eleventy:EleventyErrorHandler    17|       <div class="post__splash--container">
  Eleventy:EleventyErrorHandler    18|       {% assign picture = image | prepend : "src" %}
  Eleventy:EleventyErrorHandler >> 19|       {% images picture, 'Alt text test', "post__splash--picture", "post__splash--image" %}
  Eleventy:EleventyErrorHandler    20|       {% images picture, 'Alt text test', "post__splash--picture", "post__splash--image" %}
  Eleventy:EleventyErrorHandler    21|       </div>
  Eleventy:EleventyErrorHandler    22|     </div>
  Eleventy:EleventyErrorHandler RenderError: Missing `alt` on responsiveimage from: src/assets/images/oscar-americano-wide.png, file:./tmp/src/_includes/layouts/post.liquid, line:19, col:7
  Eleventy:EleventyErrorHandler     at RenderError.LiquidError [as constructor] (/home/adilson/Dev/Portfolio-11ty/node_modules/liquidjs/dist/liquid.node.cjs.js:1004:28)
  Eleventy:EleventyErrorHandler     at new RenderError (/home/adilson/Dev/Portfolio-11ty/node_modules/liquidjs/dist/liquid.node.cjs.js:1043:28)
  Eleventy:EleventyErrorHandler     at Render.<anonymous> (/home/adilson/Dev/Portfolio-11ty/node_modules/liquidjs/dist/liquid.node.cjs.js:4124:55)
  Eleventy:EleventyErrorHandler     at step (/home/adilson/Dev/Portfolio-11ty/node_modules/liquidjs/dist/liquid.node.cjs.js:88:23)
  Eleventy:EleventyErrorHandler     at Object.throw (/home/adilson/Dev/Portfolio-11ty/node_modules/liquidjs/dist/liquid.node.cjs.js:69:53)
  Eleventy:EleventyErrorHandler     at /home/adilson/Dev/Portfolio-11ty/node_modules/liquidjs/dist/liquid.node.cjs.js:2596:34
  Eleventy:EleventyErrorHandler     at async TemplateLayout.render (/home/adilson/Dev/Portfolio-11ty/node_modules/@11ty/eleventy/src/TemplateLayout.js:161:25)
  Eleventy:EleventyErrorHandler     at async Template.renderPageEntry (/home/adilson/Dev/Portfolio-11ty/node_modules/@11ty/eleventy/src/Template.js:853:17)
  Eleventy:EleventyErrorHandler     at async /home/adilson/Dev/Portfolio-11ty/node_modules/@11ty/eleventy/src/Template.js:883:21
  Eleventy:EleventyErrorHandler     at async Promise.all (index 0)
  Eleventy:EleventyErrorHandler From Error: Missing `alt` on responsiveimage from: src/assets/images/oscar-americano-wide.png
  Eleventy:EleventyErrorHandler     at Object.imageShortcode (/home/adilson/Dev/Portfolio-11ty/.eleventy.js:60:11)
  Eleventy:EleventyErrorHandler     at Object.<anonymous> (/home/adilson/Dev/Portfolio-11ty/node_modules/@11ty/eleventy/src/BenchmarkGroup.js:32:26)
  Eleventy:EleventyErrorHandler     at Object.render (/home/adilson/Dev/Portfolio-11ty/node_modules/@11ty/eleventy/src/Engines/Liquid.js:153:25)
  Eleventy:EleventyErrorHandler     at async TemplateLayout.render (/home/adilson/Dev/Portfolio-11ty/node_modules/@11ty/eleventy/src/TemplateLayout.js:161:25)
  Eleventy:EleventyErrorHandler     at async Template.renderPageEntry (/home/adilson/Dev/Portfolio-11ty/node_modules/@11ty/eleventy/src/Template.js:853:17)
  Eleventy:EleventyErrorHandler     at async /home/adilson/Dev/Portfolio-11ty/node_modules/@11ty/eleventy/src/Template.js:883:21
  Eleventy:EleventyErrorHandler     at async Promise.all (index 0)
  Eleventy:EleventyErrorHandler     at async Promise.all (index 6)
  Eleventy:EleventyErrorHandler     at async Eleventy.executeBuild (/home/adilson/Dev/Portfolio-11ty/node_modules/@11ty/eleventy/src/Eleventy.js:1003:13) +0ms
[11ty] 3. Missing `alt` on responsiveimage from: src/assets/images/oscar-americano-wide.png (via Error)
  Eleventy:EleventyErrorHandler (error stack): Error: Missing `alt` on responsiveimage from: src/assets/images/oscar-americano-wide.png
  Eleventy:EleventyErrorHandler     at Object.imageShortcode (/home/adilson/Dev/Portfolio-11ty/.eleventy.js:60:11)
  Eleventy:EleventyErrorHandler     at Object.<anonymous> (/home/adilson/Dev/Portfolio-11ty/node_modules/@11ty/eleventy/src/BenchmarkGroup.js:32:26)
  Eleventy:EleventyErrorHandler     at Object.render (/home/adilson/Dev/Portfolio-11ty/node_modules/@11ty/eleventy/src/Engines/Liquid.js:153:25)
  Eleventy:EleventyErrorHandler     at async TemplateLayout.render (/home/adilson/Dev/Portfolio-11ty/node_modules/@11ty/eleventy/src/TemplateLayout.js:161:25)
  Eleventy:EleventyErrorHandler     at async Template.renderPageEntry (/home/adilson/Dev/Portfolio-11ty/node_modules/@11ty/eleventy/src/Template.js:853:17)
  Eleventy:EleventyErrorHandler     at async /home/adilson/Dev/Portfolio-11ty/node_modules/@11ty/eleventy/src/Template.js:883:21
  Eleventy:EleventyErrorHandler     at async Promise.all (index 0)
  Eleventy:EleventyErrorHandler     at async Promise.all (index 6)
  Eleventy:EleventyErrorHandler     at async Eleventy.executeBuild (/home/adilson/Dev/Portfolio-11ty/node_modules/@11ty/eleventy/src/Eleventy.js:1003:13) +0ms

Is this a bug or am i forgetting something?

System information:

  • OS: elementary OS 6.1
  • Node: 18.4.0
  • Npm: 8.12.2
  • Eleventy: 1.0.1
@zeroby0
Copy link
Contributor

zeroby0 commented Jun 21, 2022

Can you create a repository that reproduces this with bug with as less code as possible?

Most bugs solve themselves when they're reproduceable

@adilsonfsantos
Copy link
Author

@zeroby0 i tried to recreate the same workflow here https://github.com/adilsonfsantos/cautious-octo-carnival, if the repo i made still bloated please let me know so i can remove more things

@adilsonfsantos
Copy link
Author

Using console.log to understand more i noticed that using four attributes (src, alt, classParent, classDescendent) bypass the "Missing `alt`" error as using only two (src, alt) makes the error return.

// post.liquid
      {% assign picture = image | prepend : "src" %}
      {% images picture, "Alt text test", "post__splash--picture", "post__splash--image" %}
      {% images picture, "Alt text test", "post__splash--picture", "post__splash--image" %}
// Terminal output
{ src: 'src/assets/images/oscar-americano-wide.png' } { alt: 'post__splash--picture' } { classParent: undefined } { classDescendent: undefined } { sizes: '100vw' }
{ src: 'src/assets/images/oscar-americano-wide.png' } { alt: 'Alt text test' } { classParent: 'post__splash--picture' } { classDescendent: 'post__splash--image' } { sizes: '100vw' }
{ src: 'src/assets/images/fundacao-jornada-usuario-final.png' } { alt: 'Alt text test' } { classParent: 'post__splash--image' } { classDescendent: undefined } { sizes: '100vw' }
{ src: 'src/assets/images/fundacao-jornada-usuario-final.png' } { alt: 'Alt text test' } { classParent: 'post__splash--picture' } { classDescendent: 'post__splash--image' } { sizes: '100vw' }

Two attributes

// post.liquid
      {% assign picture = image | prepend : "src" %}
      {% images picture, "Alt text test" %}
      {% images picture, "Alt text test" %}
// Terminal output
[11ty] Problem writing Eleventy templates: (more in DEBUG output)
[11ty] 1. Having trouble writing template: "./dist/projetos/fundacao/index.html" (via EleventyTemplateError)
[11ty] 2. Missing `alt` on responsiveimage from: src/assets/images/oscar-americano-wide.png, file:./src/_includes/layouts/post.liquid, line:19, col:7 (via RenderError)
[11ty] 3. Missing `alt` on responsiveimage from: src/assets/images/oscar-americano-wide.png (via Error)
[11ty] 
[11ty] Original error stack trace: Error: Missing `alt` on responsiveimage from: src/assets/images/oscar-americano-wide.png
[11ty]     at Object.imageShortcode (/home/adilson/Dev/eleventy-test/.eleventy.js:12:11)
[11ty]     at Object.<anonymous> (/home/adilson/Dev/eleventy-test/node_modules/@11ty/eleventy/src/BenchmarkGroup.js:32:26)
[11ty]     at Object.render (/home/adilson/Dev/eleventy-test/node_modules/@11ty/eleventy/src/Engines/Liquid.js:153:25)
[11ty]     at async TemplateLayout.render (/home/adilson/Dev/eleventy-test/node_modules/@11ty/eleventy/src/TemplateLayout.js:161:25)
[11ty]     at async Template.renderPageEntry (/home/adilson/Dev/eleventy-test/node_modules/@11ty/eleventy/src/Template.js:853:17)
[11ty]     at async /home/adilson/Dev/eleventy-test/node_modules/@11ty/eleventy/src/Template.js:883:21
[11ty]     at async Promise.all (index 0)
[11ty]     at async Promise.all (index 4)
[11ty]     at async Eleventy.executeBuild (/home/adilson/Dev/eleventy-test/node_modules/@11ty/eleventy/src/Eleventy.js:1003:13)
[11ty]     at async Eleventy.watch (/home/adilson/Dev/eleventy-test/node_modules/@11ty/eleventy/src/Eleventy.js:846:18)

Also using return Image.generateHTML(metadata, imageAttributes); gives the same error

@adilsonfsantos
Copy link
Author

Fixed

The problem was on 11ty side no the image plugin, the undefined error was fixed in 11ty/eleventy#2367, which is available on https://github.com/11ty/eleventy/releases/tag/v2.0.0-canary.9 and higher or in a future v1.0.2 of 11ty.

I'll keep the https://github.com/adilsonfsantos/cautious-octo-carnival open so if anyone have this problem can check the fix

package.json

"@11ty/eleventy": "^2.0.0-canary.9"

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

No branches or pull requests

2 participants