Wayne is an open source service worker routing library designed to simplify and enhance in-browser HTTP requests. By utilizing service workers, it allows developers to efficiently manage network interactions, improving performance and user experience while providing a flexible approach to route handling.
Wayne is an open-source service worker routing library designed to facilitate in-browser HTTP requests. With a simple API structured akin to Express, Wayne empowers developers to create innovative web applications that can handle HTTP requests efficiently within the context of a service worker.
Key Features
- Local Request Handling: Utilize service workers to generate responses to requests without leaving the browser. This can enable applications to function seamlessly in offline scenarios, making it especially useful for Progressive Web Applications (PWAs).
- Express-Like API: Developers familiar with Express will find it easy to transition to Wayne, as it offers similar routing capabilities.
- Enhanced Functionality: Wayne not only handles standard HTTP requests but also provides functionality for files, middleware, and even remote procedure calls (RPC), enriching the web application experience.
Usage Examples
Basic Routing
Define routes with ease, such as:
const users = {
1: 'Jakub T. Jankiewicz',
2: 'John Doe',
3: 'Jane Doe'
};
app.get('/user/{id}', function(req, res) {
const user = users[req.params.id];
if (user) {
res.json({result: user});
} else {
res.json({error: 'User Not Found'});
}
});
File System Middleware
Easily access a virtual file system to handle file operations:
import { Wayne, FileSystem } from 'https://cdn.jsdelivr.net/npm/@jcubic/wayne';
import FS from "https://cdn.skypack.dev/@isomorphic-git/lightning-fs";
import mime from "https://cdn.skypack.dev/mime";
const { promises: fs } = new FS("__wayne__");
const app = new Wayne();
app.use(FileSystem({ path, fs, mime, prefix: '__fs__' }));
Server-Sent Events
Stream updates to the client effortlessly:
app.get('/sse', function(req, res) {
const stream = res.sse();
const timerId = setInterval(function() {
stream.send({ data: (new Date()).toString() });
}, 1000);
});
Advanced Features
Wayne supports various advanced functionalities, such as filtering requests based on origin, caching responses, and performing remote procedure calls (RPC). This flexibility accommodates sophisticated application needs, ensuring that developers can build robust web applications efficiently.
Conclusion
By using Wayne, developers can leverage the capabilities of service workers for routing HTTP requests locally. This approach not only enhances application resilience and performance but also promotes engaging user experiences, especially under offline conditions. For more detailed examples and a live demo, visit the project’s documentation.
No comments yet.
Sign in to be the first to comment.