Skip to content

Commit

Permalink
Merge pull request #76 from BKWLD/support-preload
Browse files Browse the repository at this point in the history
add support to image preload
  • Loading branch information
weotch authored Nov 15, 2023
2 parents ca19e37 + 1cb0b14 commit dc20a63
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 3 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,11 @@ A list of the [component properties](http://vuejs.org/v2/guide/components.html#P

#### Loading

- `preload (boolean)` - Requires Nuxt framework. If `true` will add `<link rel="preload"/>` tags to the `<head>` for the image assets.

- `autoload (boolean)` - *Default: `true`.* If `true`, assets are loaded immediately unless `lazyload`.

- `lazyload (boolean)` - Waits until the Visual enters the viewport to trigger loading. Overrides, `autoload`.
- `lazyload (boolean)` - Waits until the Visual enters the viewport to trigger loading. Overrides `autoload`.

- `intersection-options (object)` - IntersectionObserver options. Used with `lazyload` and `autopause`.

Expand Down
22 changes: 21 additions & 1 deletion concerns/supports-images.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,27 @@ export default
srcset: String
webpSrcset: String
sizes: String
preload: Boolean

# Add preload link tags
head: ->
return unless @preload and @image

# Create base link attributes
preloadTag =
rel: 'preload'
as: 'image'
href: @image

# Add srcset support
if imagesrcset = @webpSrcset || @srcset
then preloadTag = Object.assign preloadTag, {
imagesrcset
imagesizes: @sizes || '' # Prevent "undefined" value
}

# Add link tag
return link: [ preloadTag ]

computed:

Expand All @@ -22,4 +43,3 @@ export default

# Image has finished loading
when @imageLoaded then true

31 changes: 30 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,36 @@ Logic related rendering images
image: String,
srcset: String,
webpSrcset: String,
sizes: String
sizes: String,
preload: Boolean
},
// Add preload link tags
head: function head() {
var imagesrcset, preloadTag;

if (!(this.preload && this.image)) {
return;
} // Create base link attributes


preloadTag = {
rel: 'preload',
as: 'image',
href: this.image
}; // Add srcset support

if (imagesrcset = this.webpSrcset || this.srcset) {
preloadTag = Object.assign(preloadTag, {
imagesrcset: imagesrcset,
imagesizes: this.sizes || '' // Prevent "undefined" value

});
}

return {
// Add link tag
link: [preloadTag]
};
},
computed: {
// Determines whether the image should be shown via v-show
Expand Down

0 comments on commit dc20a63

Please sign in to comment.