Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fetch upstream #1

Open
wants to merge 51 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
f26142c
Fix: condition where DOM can load before script loads
shubhamjain Jun 12, 2022
cb882dd
Fix inline events not working on firefox
shubhamjain Jun 24, 2022
0076bf5
Fallback to localstorage is indexedDb not available, refs #13, #10
shubhamjain Jun 24, 2022
f096252
Use [email protected]
shubhamjain Jun 24, 2022
97d44ac
Increase caching time, add method to destory cache
shubhamjain Jun 24, 2022
48c3537
Bump npm version
shubhamjain Jun 24, 2022
37d2a77
Fix typo
shubhamjain Jun 25, 2022
6c30c9d
Merge pull request #16 from shubhamjain/shubham/release-1.6.0
shubhamjain Jun 28, 2022
86b081c
Fix JS attribues not removed, refs #17
shubhamjain Jun 26, 2022
a90de89
Update svg-loader.js
honzabilek4 Jul 5, 2022
9c0e450
Bump version
shubhamjain Aug 4, 2022
beafd43
Remove dist directory
shubhamjain May 1, 2023
36c8ef4
add github action
shubhamjain May 1, 2023
e30886c
Create svg-loader.min.js automatically
shubhamjain May 1, 2023
e5faa1b
Fix Github Action to run on push and only publish if version changed
shubhamjain May 1, 2023
46e6888
Fix #23: convert cache duration to miliseconds
shubhamjain May 1, 2023
85a2b67
Update Husky precommit action to add updated files
shubhamjain May 1, 2023
b4cff68
Fix: Add id to github actions, so that envs are picked
shubhamjain May 1, 2023
0381a11
Fix #28: Add support for valid Doctype syntax
shubhamjain May 1, 2023
aa7ba44
Support icon loading from sprite
JSteunou Jan 13, 2023
76ec25b
Fix missing args when calling renderBody
JSteunou Jan 13, 2023
13d89c6
Fix check on elem id
JSteunou Jan 17, 2023
425c563
Fix: use document.body that has more events
shubhamjain May 3, 2023
ee2d51b
Fix: special handling for sprite icons and add test cases
shubhamjain May 3, 2023
036e1c8
Merge pull request #29 from shubhamjain/JSteunou-patch-1
shubhamjain May 3, 2023
456eba9
Bump version
shubhamjain May 3, 2023
1133ad4
Fix #30: move dependencies for postinstall from `dev`
shubhamjain May 5, 2023
4ae1dfa
Add cross-env dependency
SzymonGalazka May 18, 2023
baae537
cross environment safe build command
SzymonGalazka May 18, 2023
d2ab136
Delete package-lock.json
SzymonGalazka May 18, 2023
831a17d
Merge pull request #31 from SzymonGalazka/enable-cross-env-commands
shubhamjain May 18, 2023
53f800b
Update yarn.lock package urls
shubhamjain May 18, 2023
046e139
Bump version
shubhamjain May 18, 2023
522c0e3
Update README.md
shubhamjain May 19, 2023
80981ee
Use cross-env in dependencies instead of devDependencies
shubhamjain May 19, 2023
a512bff
Update README.md
shubhamjain May 20, 2023
c174672
Update package.json
ohqte May 24, 2023
1594a38
Bump version
shubhamjain Jun 29, 2023
0004635
Merge pull request #35 from shubhamjain/patch-1
shubhamjain Jun 29, 2023
8213ac3
Fix XSS vulnerability
shubhamjain Aug 11, 2023
1a0904e
Bump version
shubhamjain Aug 11, 2023
c35e032
Handle some non-standard events
shubhamjain Aug 11, 2023
d3562fc
Merge pull request from GHSA-xc2r-jf2x-gjr8
shubhamjain Aug 12, 2023
a3deb0f
Separate dev dependencies
victorlacorte Aug 17, 2023
2be8b9c
Merge pull request #41 from victorlacorte/separate-dev-dependencies
shubhamjain Aug 22, 2023
9766f79
Fix #22: throw custom error event on SVG load failure
MonkeyDo Aug 28, 2023
85d2a28
Fix access to error object in iconloaderror event
MonkeyDo Dec 18, 2023
b645e18
Add support for oniconloaderror inline function
MonkeyDo Dec 18, 2023
ce8c23d
Merge pull request #42 from MonkeyDo/error-event
shubhamjain Feb 29, 2024
527f6ae
Bump version
shubhamjain Feb 29, 2024
b792068
Don't exclude dist directory because of .gitignore refs #40
shubhamjain Feb 29, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions .github/workflows/npm-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Publish Package to npmjs
on:
push:
branches:
- 'main'
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
# Setup .npmrc file to publish to npm
- uses: actions/setup-node@v3
with:
node-version: '16.x'
registry-url: 'https://registry.npmjs.org'
# Defaults to the user or organization that owns the workflow file
scope: '@octocat'
- name: vars
id: vars
run: |
echo "latest_version=$(npm view external-svg-loader version)" >> $GITHUB_OUTPUT
echo "package_version=$(cat package.json | jq -r '.version')" >> $GITHUB_OUTPUT
- name: publish
run: |
yarn
yarn build
yarn publish
if: ${{ steps.vars.outputs.latest_version != steps.vars.outputs.package_version }}
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
node_modules
dist
4 changes: 4 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

yarn run build && git add -A .
3 changes: 3 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.github
.husky
test
49 changes: 41 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
[![NPM](https://img.shields.io/npm/v/external-svg-loader.svg)](https://www.npmjs.com/package/external-svg-loader)
![minified size](http://img.badgesize.io/shubhamjain/svg-loader/master/svg-loader.min.js?label=minified%20size&v=10) ![gzip size](http://img.badgesize.io/shubhamjain/svg-loader/master/svg-loader.min.js?compression=gzip&v=10)

SVGs from an external source can be rendered via `<img>` tags, but this has multiple drawbacks: you can't customize the fill, use CSS variables, or use focus/hover states.
SVGs from an external source can be rendered with `<img>` tags, but this has multiple drawbacks: you can't customize the fill or stroke colors, use CSS variables, or use focus/hover states.

SVG loader is a simple JS code you can include that fetches SVGs using XHR and injects the SVG code in the tag's place, giving you best of both worlds: externally stored SVGs (e.g, on CDN) and inline SVGs.
SVG Loader is a simple JS library that fetches SVGs using XHR and injects the SVG code in the tag's place. This lets you use externally stored SVGs (e.g, on CDN) just like inline SVGs.

It's super-tiny, works well with frameworks and has minimal to no impact on performance.
It's super-tiny, works with all frameworks, requires no additional code except the initial script load, and has minimal to no impact on performance.

[**Demo →**](https://codepen.io/shubhamjainco/pen/rNyBVmY)

Expand Down Expand Up @@ -90,14 +90,24 @@ ReactDOM.render(<App />, document.getElementById("container"));
SVG loader can also be included via unpkg CDN. Example:

```html
<script type="text/javascript" src="https://unpkg.com/external-svg-loader@latest/svg-loader.min.js" async></script>
<script
type="text/javascript"
src="https://unpkg.com/external-svg-loader@latest/svg-loader.min.js"
async></script>
```

## Configuration

### 1. Disable/Modify Caching
By default, the XHR response is cached for 24 hours, so that any subsequent loads are instantenous. You can disable this behavior by passing `data-cache="disabled"`. You can also modify
the caching period by passing number of seconds. Example:
By default, the XHR response is cached for 30 days, so that any subsequent loads are instantenous. You can disable this behavior by passing `data-cache="disabled"`.

You can destroy the currently stored cache by calling:

```js
SVGLoader.destroyCache();
```

You can also modify the caching period by passing number of seconds. Example:

#### Cache for a week
```html
Expand Down Expand Up @@ -201,8 +211,28 @@ When the SVG has been loaded an event `iconload` is triggered. This can be used
</script>
```

### Using the `iconloaderror` event
When an error occurs during loading of the SVG file, an `iconloaderror` event is triggered, passing the error message as the event's `detail`.

```html
<svg data-src="https://unpkg.com/@mdi/[email protected]/svg/this-svg-does-not-exist.svg"></svg>

<script>
window.addEventListener('iconloaderror', (e) => {
console.error('Failed to load SVG:', e.detail);
});
</script>
```

Similarly to the `iconload` event, `iconloaderror` can also be used with an inline function, which will have access to an `error` argument (the `Error` object that was thrown):
```html
<svg
data-src="https://unpkg.com/@mdi/[email protected]/svg/cog.svg"
oniconloaderror="console.log('Error loading SVG:', error.toString())"></svg>
```

### Using Events in React
React doesn't support custom events out of the box. To circumvent this limitation, you can [refs](https://reactjs.org/docs/refs-and-the-dom.html).
React doesn't support custom events out of the box. To circumvent this limitation, you can use [refs](https://reactjs.org/docs/refs-and-the-dom.html).

```jsx
class MyApp extends React.Component {
Expand All @@ -216,7 +246,10 @@ class MyApp extends React.Component {
componentDidMount() {
this.ref.current.addEventListener('iconload', () => {
console.log("Icon Loaded", this.ref.current)
})
});
this.ref.current.addEventListener('iconloaderror', (e) => {
console.error('Failed to load SVG:', e.detail);
});
}
}
```
Expand Down
Loading