Skip to content

Commit 8db86ef

Browse files
committed
cache svg icons
1 parent 24fcacb commit 8db86ef

File tree

1 file changed

+28
-17
lines changed

1 file changed

+28
-17
lines changed

src/components/mdIcon/mdIcon.vue

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
<script>
1414
import theme from '../../core/components/mdTheme/mixin';
1515
16+
let registeredIcons = {};
17+
1618
export default {
1719
props: {
1820
mdSrc: String
@@ -36,30 +38,39 @@
3638
isSVG(mimetype) {
3739
return mimetype.indexOf('svg') >= 0;
3840
},
41+
setSVGContent(value) {
42+
this.svgContent = value;
43+
44+
this.$nextTick(() => {
45+
this.$el.children[0].removeAttribute('fill');
46+
});
47+
},
3948
loadSVG() {
40-
const request = new XMLHttpRequest();
41-
const self = this;
49+
if (!registeredIcons[this.mdSrc]) {
50+
const request = new XMLHttpRequest();
51+
const self = this;
4252
43-
request.open('GET', this.mdSrc, true);
53+
request.open('GET', this.mdSrc, true);
4454
45-
request.onload = function() {
46-
const mimetype = this.getResponseHeader('content-type');
55+
request.onload = function() {
56+
const mimetype = this.getResponseHeader('content-type');
4757
48-
if (this.status >= 200 && this.status < 400 && self.isImage(mimetype)) {
49-
if (self.isSVG(mimetype)) {
50-
self.svgContent = this.response;
51-
window.setTimeout(() => {
52-
self.$el.children[0].removeAttribute('fill');
53-
}, 0);
58+
if (this.status >= 200 && this.status < 400 && self.isImage(mimetype)) {
59+
if (self.isSVG(mimetype)) {
60+
registeredIcons[self.mdSrc] = this.response;
61+
self.setSVGContent(this.response);
62+
} else {
63+
self.loadImage();
64+
}
5465
} else {
55-
this.loadImage();
66+
console.warn(`The file ${self.mdSrc} is not a valid image.`);
5667
}
57-
} else {
58-
console.warn(`The file ${this.mdSrc} is not a valid image.`);
59-
}
60-
};
68+
};
6169
62-
request.send();
70+
request.send();
71+
} else {
72+
this.setSVGContent(registeredIcons[this.mdSrc]);
73+
}
6374
},
6475
loadImage() {
6576
this.imageSrc = this.mdSrc;

0 commit comments

Comments
 (0)