Asyncei
Pure JS, lighweight, asynchronous content loader
Install
npm install --save-dev asyncei
Quick start
- 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! 🎉
How it works?
- 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;
API
Customization
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 is data-fetch
.
Events
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());
Demo
Find our demo here: preload-demo repository