question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to reference different sizes in a template

See original GitHub issue

Responsive-loader is generating all my images beautifully with the correct names and sizes, but I can’t figure out how to reference other sizes than the default (smallest one) in my template!

// webpack.config file
...
{
  test: /\.(jpe?g|png)$/i,
  loader: 'responsive-loader',
  options: {
    adapter: require('responsive-loader/sharp'),
    sizes: [600, 1200],
    placeholder: true,
    placeholderSize: 50,
    name: '/assets/img/[hash]-[width].[ext]'
  }
},
...

And this is how I’m referencing the images in my template html

  <img src="${require('./assets/img/image_0.jpg')}" alt="">

This gives me the 600px wide image and I would like to get different images based on screen size. Any pointers?

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
bparticlecommented, Aug 30, 2018

I found the issue (and it cost me many hours so I hope this may help anyone in the same situation). My images were not displayed due to the preceding slash that I put in the responsive-loader configuration (see previous code example). Also I changed the way I implement the loader in the vue-cli setup. This setup has fixed my problem:

// vue.config.js
module.exports = {
  chainWebpack: config => {
    const imgRule = config.module.rule('images');
    imgRule.uses.clear();
    imgRule
    .use('responsive-loader')
    .loader('responsive-loader')
    .options({
      adapter: require('responsive-loader/sharp'),
      sizes: [600, 1200],
      placeholder: true,
      placeholderSize: 50,
      name: 'assets/img/[hash]-[width].[ext]'
    });
  }
}

So name: ‘assets/img/[hash]-[width].[ext]’ instead of name: ‘/assets/img/[hash]-[width].[ext]’. This is not a vue-cli specific fix so I think it is useful to post back my solution.

0reactions
brettinternetcommented, Sep 17, 2020

For other templates using html-loader this can be accomplished by using multiple source elements. Here’s a pug example:

picture
    source(media='(min-width: 1024px)', srcset=`${src}?size=1280`)
    source(media='(min-width: 768px)', srcset=`${src}?size=1024`)
    source(media='(min-width: 640px)', srcset=`${src}?size=768`)
    source(media='(min-width: 481px)', srcset=`${src}?size=640`)
    img(src=`${src}?size=481`, loading='lazy', alt=alt)

Multiple source elements are required in this case since html-loader won’t transform a single srcset with multiple target resolutions.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How does this "size of array" template function work? [duplicate]
Read declarations from inside out, right to left, parenthesis group first: It's an unnamed parameter that is a reference to an array of...
Read more >
Change the size of a picture, shape, text box, or WordArt in Word
Select the picture, shape, WordArt, or other object to resize. · Go to Shape Format, Picture Tools Format, or Drawing Tools Format, in...
Read more >
How to have different size of font in the Word Template
Configure the word template like below: (i mentioned font size just for your reference. Just select a field and increase the font size...
Read more >
grid-template-columns - CSS: Cascading Style Sheets | MDN
The grid-template-columns CSS property defines the line names and track sizing functions of the grid columns.
Read more >
docker images - Docker Documentation
The SIZE is the cumulative space taken up by the image and all its parent ... reference (pattern of an image reference) -...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found