Skip to content

Commit dc20a63

Browse files
authored
Merge pull request #76 from BKWLD/support-preload
add support to image preload
2 parents ca19e37 + 1cb0b14 commit dc20a63

File tree

3 files changed

+54
-3
lines changed

3 files changed

+54
-3
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,11 @@ A list of the [component properties](http://vuejs.org/v2/guide/components.html#P
6666

6767
#### Loading
6868

69+
- `preload (boolean)` - Requires Nuxt framework. If `true` will add `<link rel="preload"/>` tags to the `<head>` for the image assets.
70+
6971
- `autoload (boolean)` - *Default: `true`.* If `true`, assets are loaded immediately unless `lazyload`.
7072

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

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

concerns/supports-images.coffee

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,27 @@ export default
77
srcset: String
88
webpSrcset: String
99
sizes: String
10+
preload: Boolean
11+
12+
# Add preload link tags
13+
head: ->
14+
return unless @preload and @image
15+
16+
# Create base link attributes
17+
preloadTag =
18+
rel: 'preload'
19+
as: 'image'
20+
href: @image
21+
22+
# Add srcset support
23+
if imagesrcset = @webpSrcset || @srcset
24+
then preloadTag = Object.assign preloadTag, {
25+
imagesrcset
26+
imagesizes: @sizes || '' # Prevent "undefined" value
27+
}
28+
29+
# Add link tag
30+
return link: [ preloadTag ]
1031

1132
computed:
1233

@@ -22,4 +43,3 @@ export default
2243

2344
# Image has finished loading
2445
when @imageLoaded then true
25-

index.js

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,36 @@ Logic related rendering images
624624
image: String,
625625
srcset: String,
626626
webpSrcset: String,
627-
sizes: String
627+
sizes: String,
628+
preload: Boolean
629+
},
630+
// Add preload link tags
631+
head: function head() {
632+
var imagesrcset, preloadTag;
633+
634+
if (!(this.preload && this.image)) {
635+
return;
636+
} // Create base link attributes
637+
638+
639+
preloadTag = {
640+
rel: 'preload',
641+
as: 'image',
642+
href: this.image
643+
}; // Add srcset support
644+
645+
if (imagesrcset = this.webpSrcset || this.srcset) {
646+
preloadTag = Object.assign(preloadTag, {
647+
imagesrcset: imagesrcset,
648+
imagesizes: this.sizes || '' // Prevent "undefined" value
649+
650+
});
651+
}
652+
653+
return {
654+
// Add link tag
655+
link: [preloadTag]
656+
};
628657
},
629658
computed: {
630659
// Determines whether the image should be shown via v-show

0 commit comments

Comments
 (0)