Skip to content

"srcset" doesn't seem to be working #396

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

Closed
dfdgsdfg opened this issue Dec 4, 2016 · 4 comments
Closed

"srcset" doesn't seem to be working #396

dfdgsdfg opened this issue Dec 4, 2016 · 4 comments
Assignees
Labels

Comments

@dfdgsdfg
Copy link

dfdgsdfg commented Dec 4, 2016

Here is my file structure.

component > subcomponent > Example.vue
component > subcomponent > assets > photo.jpg
component > subcomponent > assets > [email protected]

Below is what I am trying to.

<!-- Example.vue -->
<img src="./assets/photo.jpg" srcset="./assets/[email protected] 2x">

<!-- Renderd -->
<img src="/static/img/photo.jpg" srcset="./assets/[email protected] 2x">

<!-- Expected -->
<img src="/static/img/photo.jpg" srcset="/static/img/[email protected] 2x">

Maybe html-loader support srcset, but vue-html-loader does not support it?

Looks like same problem here.
https://stackoverflow.com/questions/40733102/vue-webpack-project-img-cannot-load-static-src

@LinusBorg
Copy link
Contributor

you have to tell vue-html loader to parse these attributes.

How to do this is explained here, and you have to insert those options in your wenpack files after this line:

Not tested but should look something like this:

vue: {
  html: {
    query: {attrs: "img:src img:srcset"}
  }
  loaders: //....
}

@dfdgsdfg
Copy link
Author

dfdgsdfg commented Dec 15, 2016

I try to your recipe. Unfortunately it does not work.

<!-- Example.vue -->
<img src="./assets/photo.jpg" srcset="./assets/photo.jpg 1x, ./assets/[email protected] 2x">

<!-- Renderd -->
<img src="/static/img/photo.jpg" srcset="./assets/photo.jpg 1x, ./assets/[email protected] 2x">
<img src="data:image/png;base64,..." srcset="./assets/photo.jpg 1x, ./assets/[email protected] 2x">

<!-- Expected -->
<img src="/static/img/photo.jpg" srcset="/static/img/photo.jpg 1x, /static/img/[email protected] 2x">
<img src="data:image/png;base64,..." srcset="data:image/png;base64,... 1x, data:image/png;base64,... 2x"> <!-- really? -->

Also I found it related base64 encoding loader(probably url-loader?). But I am not sure srcset attirbute can parse base64 encoding with multiple image target such as 1x or 2x.

@LinusBorg LinusBorg self-assigned this Dec 15, 2016
@LinusBorg
Copy link
Contributor

LinusBorg commented Dec 15, 2016

It seems that srcset specifically is not currently supported by HTML-loader. There was an attempt to add this as a feature, but it was reverted: webpack-contrib/html-loader#79

So I guess you have to do this manually, by importing the impages in the Javascript and using a templatze literal:

var photos = {
  1: require('./assets/photo.jpg'), 
  2: require('./assets/[email protected]')
}

export default {
  // ...component stuff
  computed: {
    photos() { return photos }
  }
}
<img src="/static/img/photo.jpg" v-bind:srcset="`${photos.1} 1x, ${photos.1} 2x`">

If your webpack config has a limit for the url-loader and you want to prevent inline data-urls, you may have to overwrite the loader:

require('!!url-loader?limit=0!./assets/photo.jpg')

@AndreKR
Copy link

AndreKR commented Aug 24, 2017

I made a PR. (vuejs/vue-loader#953)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants