Nuxt.js: data-src doesn't load images from assets folder

Created on 20 Jan 2018  路  8Comments  路  Source: nuxt/nuxt.js

I am using great https://github.com/aFarkas/lazysizes package

It need such code

    <img data-src="~assets/images/kitten-large.png" class="lazyload lazy" />

But with data-src attribute Nuxt doesn't load images.
What should I do?

This question is available on Nuxt.js community (#c2305)

Most helpful comment

You can add data-src to transformToRequire via build.extend in nuxt.config.js

module.exports = {
  build: {
    extend(config, { isDev }) {
      const vueLoader = config.module.rules.find((rule) => rule.loader === 'vue-loader')
      vueLoader.options.transformToRequire['img'] = ['src', 'data-src']
    }
  }
}

All 8 comments

Try to move your images to /static/images folder and use this path :

<img data-src="/images/kitten-large.png" class="lazyload lazy">

The assets directory should contain only your un-compiled assets such as LESS, SASS, or JS.

@NicoPennec yes this is working from static or from another domain, but I am wondering why t doesnt work from aasets? I have images in assets and Nuxt/webpack using them succesfully. Why you said I shouldn't store images there? Otherwise I can't use base64 inlining, for example.

Other words, I guess I need to store only small images in assets that I want to proceed with webpack? Right?

Also, I have other plugins that use custom attributes to load images - and they working fine with images from assets. How to make this particular package to work? I mean, it's best in class solution, it whould be nice to make it work properly with Nuxt.

You can add data-src to transformToRequire via build.extend in nuxt.config.js

module.exports = {
  build: {
    extend(config, { isDev }) {
      const vueLoader = config.module.rules.find((rule) => rule.loader === 'vue-loader')
      vueLoader.options.transformToRequire['img'] = ['src', 'data-src']
    }
  }
}

Any feedback on this?

@shindeefranco
Hope this can help you https://github.com/nuxt/nuxt.js/issues/2008#issuecomment-405521354

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jaredreich picture jaredreich  路  3Comments

bimohxh picture bimohxh  路  3Comments

gary149 picture gary149  路  3Comments

VincentLoy picture VincentLoy  路  3Comments

pehbehbeh picture pehbehbeh  路  3Comments