Pure JS, lighweight, asynchronous content loader
npm install --save-dev asyncei
- First, initialize Asyncei:
import Asyncei from 'asyncei';
new Asyncei('/path/to/handler/');
- Then, in your HTML do:
<div data-fetch="subpath/to/block"></div>
- Watch your block loading asynchronous! 🎉
- Asyncei queries page in the lookup for specified attribute;
- Fetches all found urls asynchronously;
- Fetches images in each loaded content block;
- Dispatches
blockContentLoaded
event, on the load of the a block; - After all blocks are loaded dispatches
allBlocksLoaded
event;
When initializing Asyncei
three parameters may be set:
handlerURL
– (required) URL for content loading handler.renderFunction
– Function to handle content rendering.
Should take two params: element
– DOM Element, and text
– string of content. Default one is:
(element, text) => {element.innerHTML = text}
queryAttribute
– Attribute to query in search of loadable blocks. Default isdata-fetch
.
Each block load triggers custom event blockContentLoaded
. Example:
let example = document.getElementById('example-component');
example ? example.addEventListener('blockContentLoaded', () => onExampleLoad()) : null;
If all blocks queried on page are loaded, custom event allBlocksLoaded
is triggered. Example:
document.addEventListener('allBlocksLoaded', () => onAllLoaded());
Find our demo here: preload-demo repository