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

feat(image-usage): add support for CSS images #1868

Merged
merged 7 commits into from
Mar 30, 2017
Merged

feat(image-usage): add support for CSS images #1868

merged 7 commits into from
Mar 30, 2017

Conversation

patrickhulce
Copy link
Collaborator

fixes #978

Supports an MVP of responsive CSS images. The area here is a lot more gray than with HTML image elements, so I've kept it very limited in scope, but I believe it should cover the most naive case that's likely to have bloated images.

After this lands, I'd like to do some cleanup so that we're not potentially fetching images multiple times in the optimized image gatherer and this one.

isPicture: element.parentElement.tagName === 'PICTURE',
};
});

// Only match basic background-image: url("http://host/image.jpeg") declarations
const ALLOWED_IMAGE = /^\s*url\("([^"]+)"\)\s*$/;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lets do a trim() on the string before running this regex so we can drop the \s* on the ends

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

quotes aren't required inside of a url().. And i personally rarely use them.
also some people use single quotes.

so let's allow these combos. this guy (thanks SO) seems good:

/(?:\(['"]?)(.*?)(?:['"]?\))/

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and naming nit: how about css_url_regex instead of allowed_image

Copy link
Collaborator Author

@patrickhulce patrickhulce Mar 17, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in getComputedStyle Chrome normalizes the URL to full URL in quotes :) in fact the spacing is probably totally useless too since I bet those are eliminated as well (just not as thoroughly tested by me) I'll add a comment saying so

@paulirish
Copy link
Member

paulirish commented Mar 17, 2017 via email

const CSS_URL_REGEX = /^url\("([^"]+)"\)$/;
// Only find images that aren't specifically scaled
const CSS_SIZE_REGEX = /(auto|contain|cover)/;
const cssImages = [...document.querySelectorAll('*')].reduce((images, element) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should probably make this shadow dom-aware: document.querySelectorAll('html /deep/ *').

totalDOMNodes: document.querySelectorAll('html, html /deep/ *').length,

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Collecting all the dom nodes on the page + running gCS on them....wowza. How heavy is that?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

heh yeah...you have any better ideas? i'm all ears :D

clientWidth: element.clientWidth,
clientHeight: element.clientHeight,
naturalWidth: Number.MAX_VALUE,
naturalHeight: Number.MAX_VALUE,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this b/c "css images have no natural size"? Might want to add that comment here too. I was expecting these to be element.clientWidth/Height.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yup, will do

Copy link
Member

@brendankenny brendankenny left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Collecting all the dom nodes on the page + running gCS on them....wowza. How heavy is that?

heh yeah...you have any better ideas? i'm all ears :D

lol, so turns out this does slow things way down for some sites since we've introduced CPU throttling and that apparently slows down our own injected code too. D'oh. Not sure what we should do because we do need the network throttling from being in this pass.

Details:

I was testing https://santatracker.google.com, but only the desktop version has a ton of CSS images. So if I run node lighthouse-cli/index.js https://santatracker.google.com --disable-device-emulation:

  • On master, the ImageUsage gatherer takes 17ms
  • On this branch, the ImageUsage gatherer takes 6 seconds

While playing with network throttling I accidentally disabled CPU throttling. If I run node lighthouse-cli/index.js https://santatracker.google.com --disable-device-emulation --disable-cpu-throttling:

  • On master, the ImageUsage gatherer takes 7ms
  • On this branch, the ImageUsage gatherer takes 261ms

So, still a significant increase in execution time, but nothing like it is with CPU throttling.

I would suggest moving it to another pass, but we do want the throttled network data. Is there something else we can do here to cut down execution time?

@brendankenny
Copy link
Member

Just to be clear, this isn't really a problem on many sites, even ones with a similar number of DOM nodes, so not sure exactly what's going on yet.

And of course it's next to impossible to profile the code we run in the context of the browser to find out...

@patrickhulce
Copy link
Collaborator Author

@paulirish @brendankenny more feedback?

@patrickhulce
Copy link
Collaborator Author

bump PTAL :)

Copy link
Member

@brendankenny brendankenny left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. It's still a little heavyweight even after #1901, but a few hundred milliseconds for a very image heavy site isn't so bad. Still would be interesting to see where the time was going if we could easily profile the js code we run in the browser context.

@paulirish
Copy link
Member

Still would be interesting to see where the time was going if we could easily profile the js code we run in the browser context.

we can if we just keep the trace on during this time.. just some gatherer juggling. :)

@paulirish paulirish merged commit ef52025 into master Mar 30, 2017
@paulirish paulirish deleted the css_images branch March 30, 2017 23:08
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.

DBW: site uses responsive images
4 participants