Fanos is a lightweight JavaScript promise-based
library that simplifies sending asynchronous requests using the Beacon API. It ensures reliable, non-blocking transmission of data to a server, making it ideal for scenarios like analytics, logging, and tracking user interactions. However, it does but not guaranteed if the browser is closed suddenly.
To improve reliability, Fanos includes an optional fallback mechanism using Fetch
and supports automatic retrying of failed requests. This ensures that your data is transmitted reliably, even in challenging network conditions.
- Features
- Installation
- Quickstart
- Important Notes
- Next Steps
- Browser & Node.js Compatibility
- Contributions
- Changelog
- License
- ⏳ Asynchronous request sending: Uses the
Beacon API
for non-blocking requests. - 🔄 Optional Fetch fallback: Enables
Fetch
as a backup ifsendBeacon
fails. - 🔁 Automatic retry: Stores failed requests in
localStorage
and retries them. - 🔄 Multiple payload formats: Supports
JSON
,FormData
,URL-encoded data
, andBlobs
. - 🧑💻 Custom headers: Allows adding
headers
for requests. - 🐞 Debug mode: Logs internal operations for troubleshooting.
You can include Fanos in your project via a script tag or install it using npm
:
npm install fanos
Include the library directly in your HTML:
<script src="path/to/fanos.umd.min.js"></script>
Use the library via a CDN:
<script src="https://cdn.jsdelivr.net/npm/fanos@latest/dist/fanos.umd.min.js"></script>
Import the library in your JavaScript project:
import Fanos from 'dist/fanos.esm'
Here’s how to get started with Fanos in just a few lines of code:
const fanos = new Fanos({
url: 'https://example.com/api/log',
});
const payload = {
userId: 123,
action: 'login',
details: { browser: 'Chrome', os: 'Windows' },
};
fanos.send(payload)
.then(() => console.log('Sent successfully'))
.catch(err => console.error('Failed to send:', err));
Fanos.configure({
url: 'https://example.com/api/log',
debug: true,
});
Fanos.send(payload)
.then(() => console.log('Sent successfully'))
.catch(err => console.error('Failed to send:', err));
The flush
method sends all queued requests immediately.
fanos.flush()
The destroy
method cleans up resources and stops retries:
fanos.destroy()
-
Beacon API Dependency: Fanos relies on the Beacon API, to send data asynchronously to the server. Note that the Beacon API is specifically designed for sending data and does not support receiving responses from the server. For older browsers that do not support the Beacon API, you can enable the
fallbackToFetch
option to useFetch
as a fallback mechanism. - Node.js Compatibility: Fanos is specifically designed for browser environments and is not compatible with Node.js. Ensure it is used in client-side applications only.
-
Debugging: To assist with troubleshooting, you can enable the
debug
option. This will log internal operations and provide insights into the library's behavior during execution.
- Explore the full list of Configuration Options.
- Check out more examples for advanced usage.
- Refer to the detailed API documentation for all available methods and properties.
Browser/Environment | Support |
---|---|
Chrome | Latest ✔ |
Firefox | Latest ✔ |
Safari | Latest ✔ |
Opera | Latest ✔ |
Edge | Latest ✔ |
Node.js | No ❌ |
We welcome contributions! Please read the Contributions Guid to learn how to contribute.
See Changelog for release details.
This package is open-source software licensed under the MIT license.