Basket.js is a script loader that efficiently caches scripts in localStorage for improved page load times.
- Script Caching: Automatically caches loaded scripts in localStorage
- Version Control: Built-in support for script versioning
- Expiration Control: Set expiration times for cached scripts
- Promise-based API: Modern Promise-based interface
- Custom Handlers: Support for different content types
- Modular: ESM and CommonJS support
npm install basket.js
import basket from 'basket.js';
// Load a single script
basket.require({ url: 'jquery.min.js' })
.then(() => {
console.log('jQuery loaded!');
});
// Load multiple scripts
basket.require(
{ url: 'jquery.min.js' },
{ url: 'backbone.min.js' }
).then(() => {
console.log('Libraries loaded!');
});
// Advanced usage with options
basket.require({
url: 'my-script.js',
key: 'custom-key', // Custom key for storage
expire: 24, // Expires in 24 hours
execute: false, // Don't execute after loading
unique: '1.0.0', // Version control
skipCache: false, // Use cache when available
live: false // Always fetch fresh copy
});
const basket = require('basket.js');
Loads one or more scripts with specified options.
Options:
url
(String): URL of the script to loadkey
(String): Custom storage key (defaults to URL)expire
(Number): Hours until expiration (default: 5000)execute
(Boolean): Whether to execute the script (default: true)unique
(String/Number): Version identifierskipCache
(Boolean): Skip localStorage cachinglive
(Boolean): Always fetch from network
Returns a Promise that resolves when all scripts are loaded.
Removes a script from localStorage.
basket.remove('jquery');
Clears all scripts from localStorage. If expired
is true, only removes expired scripts.
// Clear all scripts
basket.clear();
// Clear only expired scripts
basket.clear(true);
Retrieves a script's cached data.
const scriptData = basket.get('jquery');
Add a custom handler for specific content types.
basket.addHandler('text/css', (obj) => {
const style = document.createElement('style');
style.textContent = obj.data;
document.head.appendChild(style);
});
# Install dependencies
npm install
# Run development server
npm run dev
# Build for production
npm run build
# Run tests
npm test
Basket.js requires browsers with:
localStorage
supportPromise
support (or use a polyfill)- Modern JavaScript features (or use appropriate build tools)
MIT License
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add some amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request