All Projects → ShanaMaid → archer-svgs

ShanaMaid / archer-svgs

Licence: MIT license
异步加载svg解决方案

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language
CSS
56736 projects

Projects that are alternatives of or similar to archer-svgs

geticon
Web / IT project stack / tool / technique icon / logo collection & markdown / HTML generator 🚀
Stars: ✭ 101 (+339.13%)
Mutual labels:  svg-icons, icon
Tabler Icons
A set of over 1400 free MIT-licensed high-quality SVG icons for you to use in your web projects.
Stars: ✭ 10,858 (+47108.7%)
Mutual labels:  svg-icons, icon
Icons Flat Osx
Free Flat icons For OSX
Stars: ✭ 366 (+1491.3%)
Mutual labels:  svg-icons, icon
Iconpark
🍎Transform an SVG icon into multiple themes, and generate React icons,Vue icons,svg icons
Stars: ✭ 4,924 (+21308.7%)
Mutual labels:  icon, svg-icon
Akar Icons
A perfectly rounded icon library made for designers, developers, and pretty much everyone.
Stars: ✭ 184 (+700%)
Mutual labels:  svg-icons, icon
vue-iconfont
更优雅地使用 Iconfont.cn,同时支持 font-class 引入和 symbol 引入。
Stars: ✭ 27 (+17.39%)
Mutual labels:  svg-icons, icon
Icons
The premium icon font for @uiwjs Component Library. https://uiwjs.github.io/icons
Stars: ✭ 99 (+330.43%)
Mutual labels:  svg-icons, icon
icons-flat-osx
Free Flat icons For OSX
Stars: ✭ 371 (+1513.04%)
Mutual labels:  svg-icons, icon
Cryptocurrency Icons
A set of icons for all the main cryptocurrencies and altcoins, in a range of styles and sizes.
Stars: ✭ 2,116 (+9100%)
Mutual labels:  svg-icons, icon
Svgtofont
Read a set of SVG icons and ouput a TTF/EOT/WOFF/WOFF2/SVG font.
Stars: ✭ 149 (+547.83%)
Mutual labels:  svg-icons, icon
React Icomoon
It allows you to simply view the icons in the selection.json file provided by Icomoon.
Stars: ✭ 48 (+108.7%)
Mutual labels:  svg-icons, icon
react-svg-icon-generator
Generate React Icon Component from SVG icons to show, resize and recolor them.
Stars: ✭ 65 (+182.61%)
Mutual labels:  svg-icons, icon
Coreui Icons
CoreUI Free Icons - Premium designed free icon set with marks in SVG, Webfont and raster formats
Stars: ✭ 1,813 (+7782.61%)
Mutual labels:  svg-icons, svg-icon
neu
Free icon set to the world
Stars: ✭ 26 (+13.04%)
Mutual labels:  icon, svg-icon
svg-gobbler
Open source browser extension that makes designing and developing easier by finding, processing, exporting, optimizing, and managing SVG content.
Stars: ✭ 272 (+1082.61%)
Mutual labels:  svg-icons, icon
mdn-dinocons
A scalable set of icons for use across Mozilla Developer websites
Stars: ✭ 21 (-8.7%)
Mutual labels:  svg-icons
host-webfonts-locally
OMGF automagically caches the Google Fonts used by your theme/plugins locally. No configuration (or brains) required!
Stars: ✭ 13 (-43.48%)
Mutual labels:  localstorage
async
A lightweight and high performance async CSS and script loader for frontend optimization.
Stars: ✭ 17 (-26.09%)
Mutual labels:  localstorage
WP-SVG-Icons-WordPress-Plugin
WPIcons Plugin (Formally WP SVG Icons) - Quickly and easily install font icons and custom SVG icons on any WordPress site.
Stars: ✭ 18 (-21.74%)
Mutual labels:  svg-icons
octicons-modular
GitHub Octicons with tree-shaking support and icon-per-file style.
Stars: ✭ 25 (+8.7%)
Mutual labels:  icon

archer-svgs

travis ci badge version license downloads downloads-month

背景

实际生产项目中基本都会用到svg,随着项目迭代svg bundle的体积会越来越大,在后续迭代中如果每新增或删除一个svg就会导致svgbundle的hash发生变化,用户就必须重新下载这个文件,为此会付出额外的流量开支。例如,现在有一个svgbundle为100kb,此时新增了一个1kbsvg,项目上线后,用户就需要为这1kb的更新重新加载整个svg bundle,即101kb,毫无疑问,这是一种非常愚蠢的行为。这种方式还存在另一个问题,当两个不同的项目拥有相同的svg bundle时,由于svg bundle资源在不同域下,用户需要下载2份资源。archer-svgs就是为了解决这个问题,通过它你可以更灵活、轻便地加载svg。

如果你觉得这个项目还不错,可以给我一个starfollow来支持我 😘

在线示例

目录

安装

npm

npm install archer-svgs

yarn

yarn add archer-svgs

script

<script src="//unpkg.com/archer-svgs/lib/Archer.min.js"></script>

性能

svg资源的加载顺序为内存->硬盘缓存->远程服务器资源,大大提高资源加载效率。

如果想进一步提高加载速度,可以将svg资源放在cdn上。

同时由于svg资源是通过url进行加载的,因此可以跨域共享diskCache

特性

  • 使用TypeScript进行, 提供d.ts文件提高开发效率。
  • 通过XMLHttpRequest异步加载 svg
  • 高效率加载svg,加载顺序:内存->diskCache->远程服务器
  • 体积小
  • 支持预加载 svg

兼容性

archer-svgs 基于XMLHttpRequest, 只要你的浏览器支持 xhr,你就可以使用它!兼容性如下图所示: XMLHttpRequest

如果需要在低版本浏览器使用,需要引入promises poly-fill, 推荐使用taylorhakes/promise-polyfill,体积小并且兼容性很好。 你也可以使用<script>去加载它。

<script src="https://cdn.jsdelivr.net/npm/promise-polyfill@8/dist/polyfill.min.js"></script>

方法

set()

必须先调用 set()初始化配置,然后才能使用其它的Archer方法!

import Archer from 'archer-svgs';

const archer = new Archer();

archer.set({
  'ios-airplane': 'https://unpkg.com/[email protected]/dist/ionicons/svg/ios-airplane.svg',
  'md-airplane': 'https://unpkg.com/[email protected]/dist/ionicons/svg/md-airplane.svg',
})

config - paramas

export interface IConfig {
  [index: string]: string;
}

add()

添加配置

archer.set({
  'ios-airplane': 'https://unpkg.com/[email protected]/dist/ionicons/svg/ios-airplane.svg',
})
archer.add({
  'md-airplane': 'https://unpkg.com/[email protected]/dist/ionicons/svg/md-airplane.svg',
})
/**
 * config = {
 *  'ios-airplane': 'https://unpkg.com/[email protected]/dist/ionicons/svg/ios-airplane.svg',
 *  'md-airplane': 'https://unpkg.com/[email protected]/dist/ionicons/svg/md-airplane.svg',
 * }
 * /

startPrefetch()

startPrefetch会对config中的svg进行预加载!当你调用svg的时候将大大提高使用速度。

  • diskCache为空时,从远程服务资源拉取资源,同时将资源缓存到memorydiskCache中。
  • diskCache不为空时,将本地资源加载到memory中。
archer.startPrefetch();

downloadSvg()

paramsconfig.svgskey, 这个方法将返回svg的内容。

console.log(archer.downloadSvg('ios-airplane'));

result:

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><path d="M407.7 224c-3.4 0-14.8.1-18 .3l-64.9 1.7c-.7 0-1.4-.3-1.7-.9L225.8 79.4c-2.9-4.6-8.1-7.4-13.5-7.4h-23.7c-5.6 0-7.5 5.6-5.5 10.8l50.1 142.8c.5 1.3-.4 2.7-1.8 2.7L109 230.1c-2.6.1-5-1.1-6.6-3.1l-37-45c-3-3.9-7.7-6.1-12.6-6.1H36c-2.8 0-4.7 2.7-3.8 5.3l19.9 68.7c1.5 3.8 1.5 8.1 0 11.9l-19.9 68.7c-.9 2.6 1 5.3 3.8 5.3h16.7c4.9 0 9.6-2.3 12.6-6.1L103 284c1.6-2 4.1-3.2 6.6-3.1l121.7 2.7c1.4.1 2.3 1.4 1.8 2.7L183 429.2c-2 5.2-.1 10.8 5.5 10.8h23.7c5.5 0 10.6-2.8 13.5-7.4L323.1 287c.4-.6 1-.9 1.7-.9l64.9 1.7c3.3.2 14.6.3 18 .3 44.3 0 72.3-14.3 72.3-32S452.1 224 407.7 224z"/></svg>

fetchSvg()

通过url加载svg

const svg = archer.fetchSvg('https://unpkg.com/[email protected]/dist/ionicons/svg/ios-airplane.svg')

console.log(svg);

result:

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><path d="M407.7 224c-3.4 0-14.8.1-18 .3l-64.9 1.7c-.7 0-1.4-.3-1.7-.9L225.8 79.4c-2.9-4.6-8.1-7.4-13.5-7.4h-23.7c-5.6 0-7.5 5.6-5.5 10.8l50.1 142.8c.5 1.3-.4 2.7-1.8 2.7L109 230.1c-2.6.1-5-1.1-6.6-3.1l-37-45c-3-3.9-7.7-6.1-12.6-6.1H36c-2.8 0-4.7 2.7-3.8 5.3l19.9 68.7c1.5 3.8 1.5 8.1 0 11.9l-19.9 68.7c-.9 2.6 1 5.3 3.8 5.3h16.7c4.9 0 9.6-2.3 12.6-6.1L103 284c1.6-2 4.1-3.2 6.6-3.1l121.7 2.7c1.4.1 2.3 1.4 1.8 2.7L183 429.2c-2 5.2-.1 10.8 5.5 10.8h23.7c5.5 0 10.6-2.8 13.5-7.4L323.1 287c.4-.6 1-.9 1.7-.9l64.9 1.7c3.3.2 14.6.3 18 .3 44.3 0 72.3-14.3 72.3-32S452.1 224 407.7 224z"/></svg>

setThreadNum

设置预加载svg的最大并发下载数,默认值是2

例如修改最大并发量为5

archer.setThreadNum(5);

注意

跨域加载svg静态资源的时候需要服务端配置Access-Control-Allow-Origin

用法

React

import Icon from 'archer-svgs/lib/react';

// 初始化配置
Icon.archer.set({
  'ios-airplane': 'https://unpkg.com/[email protected]/dist/ionicons/svg/ios-airplane.svg',
});

// 预加载 - 根据实际需求,也可以不进行预加载

Icon.archer.startPrefetch();

<Icon type="ios-airplane"/>

谁在用

贡献者

archer-svgs 受到以下项目启发

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].