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

Optimizer: Add OptimizeHeroImages configuration to allow media-less images to be preloaded #1230

Open
westonruter opened this issue May 1, 2021 · 0 comments

Comments

@westonruter
Copy link
Member

As of #1132, image preloading was turned off for images that lack a media attribute, since otherwise images can accidentally be preloaded which are shown exclusively to mobile to desktop:

if (!heroImage.media) {
// We can only safely preload a hero image if there's a media attribute
// as we can't detect whether it's hidden on certain viewport sizes otherwise.
return;
}

However, there are many cases where a template doesn't actually have viewport-dependent hero images. For them, therefore whether or not a hero image without a media attribute gets preloaded should be dictated by by a configuration option (which defaults to false).

It's also possible that some media-less hero images may be viewport-specific whereas others are shared. Consider a template that has a square logo for mobile but a rectangular logo for desktop, but both mobile and desktop share the same top story image. This case should also be accounted for, however I'm not sure the best way to do so with configuration. The most flexible way to do so would be for the configuration to take a callback that would allow arbitrary logic to be used to determine whether an image can be preloaded. For example, if only the logo is can't be prerendered, and logos have the class names desktop-logo and mobile-logo respectively for desktop and mobile viewports, then this could be configured as follows:

const transformer = new OptimizeHeroImage( {
    maxHeroCount: 6,
    canPreloadCallback: ( heroImage ) => {
        return ! /logo/.test( heroImage.attribs.class );
    }
} );

I realize that transformer configurations up until now may be limited to JSON-primitives, but this seems the most flexible.

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

No branches or pull requests

1 participant