@iconify/react
Advanced tools
Comparing version 5.1.0 to 5.2.0
@@ -27,2 +27,7 @@ import { IconifyIcon } from '@iconify/types'; | ||
/** | ||
* Storage types | ||
* | ||
* @deprecated This type is not used anymore | ||
*/ | ||
declare type BrowserStorageType = 'local' | 'session'; | ||
@@ -53,2 +58,4 @@ | ||
* Disable cache | ||
* | ||
* @deprecated No longer used | ||
*/ | ||
@@ -59,2 +66,4 @@ export declare function disableCache(storage: IconifyBrowserCacheType): void; | ||
* Enable cache | ||
* | ||
* @deprecated No longer used | ||
*/ | ||
@@ -182,2 +191,4 @@ export declare function enableCache(storage: IconifyBrowserCacheType): void; | ||
* Interface for exported functions | ||
* | ||
* @deprecated This type is not used anymore | ||
*/ | ||
@@ -191,2 +202,4 @@ export declare interface IconifyBrowserCacheFunctions { | ||
* Cache types | ||
* | ||
* @deprecated This type is not used anymore | ||
*/ | ||
@@ -193,0 +206,0 @@ export declare type IconifyBrowserCacheType = BrowserStorageType | 'all'; |
@@ -1177,197 +1177,2 @@ 'use client'; | ||
const browserCacheVersion = "iconify2"; | ||
const browserCachePrefix = "iconify"; | ||
const browserCacheCountKey = browserCachePrefix + "-count"; | ||
const browserCacheVersionKey = browserCachePrefix + "-version"; | ||
const browserStorageHour = 36e5; | ||
const browserStorageCacheExpiration = 168; | ||
const browserStorageLimit = 50; | ||
function getStoredItem(func, key) { | ||
try { | ||
return func.getItem(key); | ||
} catch (err) { | ||
} | ||
} | ||
function setStoredItem(func, key, value) { | ||
try { | ||
func.setItem(key, value); | ||
return true; | ||
} catch (err) { | ||
} | ||
} | ||
function removeStoredItem(func, key) { | ||
try { | ||
func.removeItem(key); | ||
} catch (err) { | ||
} | ||
} | ||
function setBrowserStorageItemsCount(storage, value) { | ||
return setStoredItem(storage, browserCacheCountKey, value.toString()); | ||
} | ||
function getBrowserStorageItemsCount(storage) { | ||
return parseInt(getStoredItem(storage, browserCacheCountKey)) || 0; | ||
} | ||
const browserStorageConfig = { | ||
local: true, | ||
session: true | ||
}; | ||
const browserStorageEmptyItems = { | ||
local: /* @__PURE__ */ new Set(), | ||
session: /* @__PURE__ */ new Set() | ||
}; | ||
let browserStorageStatus = false; | ||
function setBrowserStorageStatus(status) { | ||
browserStorageStatus = status; | ||
} | ||
let _window = typeof window === "undefined" ? {} : window; | ||
function getBrowserStorage(key) { | ||
const attr = key + "Storage"; | ||
try { | ||
if (_window && _window[attr] && typeof _window[attr].length === "number") { | ||
return _window[attr]; | ||
} | ||
} catch (err) { | ||
} | ||
browserStorageConfig[key] = false; | ||
} | ||
function iterateBrowserStorage(key, callback) { | ||
const func = getBrowserStorage(key); | ||
if (!func) { | ||
return; | ||
} | ||
const version = getStoredItem(func, browserCacheVersionKey); | ||
if (version !== browserCacheVersion) { | ||
if (version) { | ||
const total2 = getBrowserStorageItemsCount(func); | ||
for (let i = 0; i < total2; i++) { | ||
removeStoredItem(func, browserCachePrefix + i.toString()); | ||
} | ||
} | ||
setStoredItem(func, browserCacheVersionKey, browserCacheVersion); | ||
setBrowserStorageItemsCount(func, 0); | ||
return; | ||
} | ||
const minTime = Math.floor(Date.now() / browserStorageHour) - browserStorageCacheExpiration; | ||
const parseItem = (index) => { | ||
const name = browserCachePrefix + index.toString(); | ||
const item = getStoredItem(func, name); | ||
if (typeof item !== "string") { | ||
return; | ||
} | ||
try { | ||
const data = JSON.parse(item); | ||
if (typeof data === "object" && typeof data.cached === "number" && data.cached > minTime && typeof data.provider === "string" && typeof data.data === "object" && typeof data.data.prefix === "string" && // Valid item: run callback | ||
callback(data, index)) { | ||
return true; | ||
} | ||
} catch (err) { | ||
} | ||
removeStoredItem(func, name); | ||
}; | ||
let total = getBrowserStorageItemsCount(func); | ||
for (let i = total - 1; i >= 0; i--) { | ||
if (!parseItem(i)) { | ||
if (i === total - 1) { | ||
total--; | ||
setBrowserStorageItemsCount(func, total); | ||
} else { | ||
browserStorageEmptyItems[key].add(i); | ||
} | ||
} | ||
} | ||
} | ||
function initBrowserStorage() { | ||
if (browserStorageStatus) { | ||
return; | ||
} | ||
setBrowserStorageStatus(true); | ||
for (const key in browserStorageConfig) { | ||
iterateBrowserStorage(key, (item) => { | ||
const iconSet = item.data; | ||
const provider = item.provider; | ||
const prefix = iconSet.prefix; | ||
const storage = getStorage( | ||
provider, | ||
prefix | ||
); | ||
if (!addIconSet(storage, iconSet).length) { | ||
return false; | ||
} | ||
const lastModified = iconSet.lastModified || -1; | ||
storage.lastModifiedCached = storage.lastModifiedCached ? Math.min(storage.lastModifiedCached, lastModified) : lastModified; | ||
return true; | ||
}); | ||
} | ||
} | ||
function updateLastModified(storage, lastModified) { | ||
const lastValue = storage.lastModifiedCached; | ||
if ( | ||
// Matches or newer | ||
lastValue && lastValue >= lastModified | ||
) { | ||
return lastValue === lastModified; | ||
} | ||
storage.lastModifiedCached = lastModified; | ||
if (lastValue) { | ||
for (const key in browserStorageConfig) { | ||
iterateBrowserStorage(key, (item) => { | ||
const iconSet = item.data; | ||
return item.provider !== storage.provider || iconSet.prefix !== storage.prefix || iconSet.lastModified === lastModified; | ||
}); | ||
} | ||
} | ||
return true; | ||
} | ||
function storeInBrowserStorage(storage, data) { | ||
if (!browserStorageStatus) { | ||
initBrowserStorage(); | ||
} | ||
function store(key) { | ||
let func; | ||
if (!browserStorageConfig[key] || !(func = getBrowserStorage(key))) { | ||
return; | ||
} | ||
const set = browserStorageEmptyItems[key]; | ||
let index; | ||
if (set.size) { | ||
set.delete(index = Array.from(set).shift()); | ||
} else { | ||
index = getBrowserStorageItemsCount(func); | ||
if (index >= browserStorageLimit || !setBrowserStorageItemsCount(func, index + 1)) { | ||
return; | ||
} | ||
} | ||
const item = { | ||
cached: Math.floor(Date.now() / browserStorageHour), | ||
provider: storage.provider, | ||
data | ||
}; | ||
return setStoredItem( | ||
func, | ||
browserCachePrefix + index.toString(), | ||
JSON.stringify(item) | ||
); | ||
} | ||
if (data.lastModified && !updateLastModified(storage, data.lastModified)) { | ||
return; | ||
} | ||
if (!Object.keys(data.icons).length) { | ||
return; | ||
} | ||
if (data.not_found) { | ||
data = Object.assign({}, data); | ||
delete data.not_found; | ||
} | ||
if (!store("local")) { | ||
store("session"); | ||
} | ||
} | ||
function emptyCallback() { | ||
@@ -1395,3 +1200,3 @@ } | ||
} | ||
function parseLoaderResponse(storage, icons, data, isAPIResponse) { | ||
function parseLoaderResponse(storage, icons, data) { | ||
function checkMissing() { | ||
@@ -1415,5 +1220,2 @@ const pending = storage.pendingIcons; | ||
} | ||
if (isAPIResponse) { | ||
storeInBrowserStorage(storage, data); | ||
} | ||
} catch (err) { | ||
@@ -1458,3 +1260,3 @@ console.error(err); | ||
(data) => { | ||
parseLoaderResponse(storage, icons2, data, false); | ||
parseLoaderResponse(storage, icons2, data); | ||
} | ||
@@ -1474,3 +1276,3 @@ ); | ||
} : null; | ||
parseLoaderResponse(storage, [name], iconSet, false); | ||
parseLoaderResponse(storage, [name], iconSet); | ||
}); | ||
@@ -1482,3 +1284,3 @@ }); | ||
if (invalid.length) { | ||
parseLoaderResponse(storage, invalid, null, false); | ||
parseLoaderResponse(storage, invalid, null); | ||
} | ||
@@ -1490,3 +1292,3 @@ if (!valid.length) { | ||
if (!api) { | ||
parseLoaderResponse(storage, valid, null, false); | ||
parseLoaderResponse(storage, valid, null); | ||
return; | ||
@@ -1497,3 +1299,3 @@ } | ||
sendAPIQuery(provider, item, (data) => { | ||
parseLoaderResponse(storage, item.icons, data, true); | ||
parseLoaderResponse(storage, item.icons, data); | ||
}); | ||
@@ -1588,16 +1390,2 @@ }); | ||
function toggleBrowserCache(storage, value) { | ||
switch (storage) { | ||
case "local": | ||
case "session": | ||
browserStorageConfig[storage] = value; | ||
break; | ||
case "all": | ||
for (const key in browserStorageConfig) { | ||
browserStorageConfig[key] = value; | ||
} | ||
break; | ||
} | ||
} | ||
function mergeCustomisations(defaults, item) { | ||
@@ -1919,11 +1707,15 @@ const result = { | ||
* Enable cache | ||
* | ||
* @deprecated No longer used | ||
*/ | ||
function enableCache(storage) { | ||
toggleBrowserCache(storage, true); | ||
// | ||
} | ||
/** | ||
* Disable cache | ||
* | ||
* @deprecated No longer used | ||
*/ | ||
function disableCache(storage) { | ||
toggleBrowserCache(storage, false); | ||
// | ||
} | ||
@@ -1941,4 +1733,2 @@ /** | ||
if (typeof document !== 'undefined' && typeof window !== 'undefined') { | ||
// Set cache and load existing cache | ||
initBrowserStorage(); | ||
const _window = window; | ||
@@ -1945,0 +1735,0 @@ // Load icons from global "IconifyPreload" |
@@ -6,3 +6,3 @@ { | ||
"type": "module", | ||
"version": "5.1.0", | ||
"version": "5.2.0", | ||
"license": "MIT", | ||
@@ -44,16 +44,16 @@ "bugs": "https://github.com/iconify/iconify/issues", | ||
"devDependencies": { | ||
"@microsoft/api-extractor": "^7.48.0", | ||
"@rollup/plugin-node-resolve": "^15.3.0", | ||
"@microsoft/api-extractor": "^7.48.1", | ||
"@rollup/plugin-node-resolve": "^15.3.1", | ||
"@testing-library/jest-dom": "^6.6.3", | ||
"@testing-library/react": "^16.1.0", | ||
"@types/react": "^18.3.14", | ||
"@types/react-dom": "^18.3.2", | ||
"@types/react": "^18.3.18", | ||
"@types/react-dom": "^18.3.5", | ||
"jsdom": "^25.0.1", | ||
"react": "^18.3.1", | ||
"rimraf": "^6.0.1", | ||
"rollup": "^4.28.1", | ||
"rollup": "^4.29.1", | ||
"typescript": "^5.7.2", | ||
"vitest": "^2.1.8", | ||
"@iconify/core": "^3.0.0", | ||
"@iconify/utils": "^2.2.0" | ||
"@iconify/core": "^3.1.0", | ||
"@iconify/utils": "^2.2.1" | ||
}, | ||
@@ -60,0 +60,0 @@ "peerDependencies": { |
@@ -7,3 +7,4 @@ import { defineConfig } from 'vitest/config'; | ||
environment: 'jsdom', | ||
watch: false, | ||
}, | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
194560
5881