aboutsummaryrefslogtreecommitdiffstats
path: root/src/js/utils/load-sprite.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/js/utils/load-sprite.js')
-rw-r--r--src/js/utils/load-sprite.js104
1 files changed, 52 insertions, 52 deletions
diff --git a/src/js/utils/load-sprite.js b/src/js/utils/load-sprite.js
index fe4add00..0a4eff99 100644
--- a/src/js/utils/load-sprite.js
+++ b/src/js/utils/load-sprite.js
@@ -8,68 +8,68 @@ import is from './is';
// Load an external SVG sprite
export default function loadSprite(url, id) {
- if (!is.string(url)) {
- return;
+ if (!is.string(url)) {
+ return;
+ }
+
+ const prefix = 'cache';
+ const hasId = is.string(id);
+ let isCached = false;
+ const exists = () => document.getElementById(id) !== null;
+
+ const update = (container, data) => {
+ // eslint-disable-next-line no-param-reassign
+ container.innerHTML = data;
+
+ // Check again incase of race condition
+ if (hasId && exists()) {
+ return;
}
- const prefix = 'cache';
- const hasId = is.string(id);
- let isCached = false;
- const exists = () => document.getElementById(id) !== null;
+ // Inject the SVG to the body
+ document.body.insertAdjacentElement('afterbegin', container);
+ };
- const update = (container, data) => {
- // eslint-disable-next-line no-param-reassign
- container.innerHTML = data;
+ // Only load once if ID set
+ if (!hasId || !exists()) {
+ const useStorage = Storage.supported;
+ // Create container
+ const container = document.createElement('div');
+ container.setAttribute('hidden', '');
- // Check again incase of race condition
- if (hasId && exists()) {
- return;
- }
+ if (hasId) {
+ container.setAttribute('id', id);
+ }
- // Inject the SVG to the body
- document.body.insertAdjacentElement('afterbegin', container);
- };
+ // Check in cache
+ if (useStorage) {
+ const cached = window.localStorage.getItem(`${prefix}-${id}`);
+ isCached = cached !== null;
- // Only load once if ID set
- if (!hasId || !exists()) {
- const useStorage = Storage.supported;
- // Create container
- const container = document.createElement('div');
- container.setAttribute('hidden', '');
+ if (isCached) {
+ const data = JSON.parse(cached);
+ update(container, data.content);
+ }
+ }
- if (hasId) {
- container.setAttribute('id', id);
+ // Get the sprite
+ fetch(url)
+ .then(result => {
+ if (is.empty(result)) {
+ return;
}
- // Check in cache
if (useStorage) {
- const cached = window.localStorage.getItem(`${prefix}-${id}`);
- isCached = cached !== null;
-
- if (isCached) {
- const data = JSON.parse(cached);
- update(container, data.content);
- }
+ window.localStorage.setItem(
+ `${prefix}-${id}`,
+ JSON.stringify({
+ content: result,
+ }),
+ );
}
- // Get the sprite
- fetch(url)
- .then(result => {
- if (is.empty(result)) {
- return;
- }
-
- if (useStorage) {
- window.localStorage.setItem(
- `${prefix}-${id}`,
- JSON.stringify({
- content: result,
- }),
- );
- }
-
- update(container, result);
- })
- .catch(() => {});
- }
+ update(container, result);
+ })
+ .catch(() => {});
+ }
}