lakka. An asynchronous request accelerator.
lakka is an accelerator for asyncronous requests. It works by caching the request's response in your users local storage. It caches JSON, text and HTML requests and considers the Cache-Control
and Expires
Headers of your requests and responses.
Usualy, the content of an AJAX-request does not change so often. In the best case, the backend-environemnt either caches them or responds with a "not-modified"-like header. But the client still need to wait for this response and the network roundtrip + response time + connection speed makes your website feels "slow". This is where lakka
enhances your website's snappiness: no more waiting, instant responses!
Install from npm $ yarn add lakka
Add dist/wrapper/lakka.js
to your project.
Basically you need to call two functions:
before()
with the uri before making your requestafter()
with the response afterwards
Call this function before the actual request. It checks the cache and returns a fresh cache item or throws an error.
Call this after the actual request. Checks the response and store it at the cache. Returns the cache item or throws an error.
This is an UMD-Module working in your browser, server, requirejs, browserify, commonjs or nodejs setup.
Load ./dist/lakka.js
to use the lakka accelerator in your code. This will give you access to the complete lakka-API. It exposes window.lakka
for your convienence.
Take a look example
folder:
For a simple example how to use lakka
with window.XMLHTTPRequest()
For a simple example how to use lakka
with window.fetch()
Lakka let's you define basic options:
exclude
: Adds a regular-expressions to ignore certain URIs. This can be part of the configuration object or directly set using ``.ignore(). If an URI matches an
exclude``` and ```include```pattern, the ```exclude```pattern takes precedence and the URI will be ignored. Default: emptyinclude
: Adds a regular-expressions to include certain URIs. This can be part of the configuration object or directly set using ``.recognize()```. If this is set, all non-matching URIs will be ignored. Default: emptyminutes
: sets the time a cache item is considered "fresh" if the response does not contain aCache-Control Header
orExpires Header
. Default: 60 minutes
Adds an ignore pattern (a string / RegEx) to the existing / default configuration. URIs matching this pattern will be ignored by lakka and passed throught to the remote location.
- String|RegEx: The pattern to look for
Adds an include pattern (a string / RegEx) to the existing / default configuration. URIs matching this pattern will be recognized and handled by lakka. If none is set, all URIs will be handled.
- String|RegEx: The pattern to look for
Sets the minutes (Number) we should cache items for. Defaut value is 60 minutes. It will be overwritten by the max-age
- value of the response's Cache-Control Header
or the response's Expires Header
.
- Number: The value the default caching time
Dynamically merge a configuration object into the current / default configuration object. include
and exclude
will be merged, minutes
will be replaced.
- Object: A configuration object
{
"include":["/include-me/"],
"exclude":["/exclude-me/"],
"minutes": 120
}
Call this after the actual request. Checks the response and store it at the cache. Returns the cache item or an error.
- Checks the
status code
for success or throws an error. - Checks the
include
andexclude
patterns to see if this should be handled by lakka or throws an error. - Checks for a cachable
Cache-Control Header
or throws an error.- Cachable:
public
,private
,Immutable
,proxy-revalidate
- Non-cachable
must-revalidate
,no-cache
,no-store
,
- Cachable:
- Checks the "Content-Type" or throws an error.
- Valid content types are:
application/json
,text/x-json
,text/plain
,text/html
- Valid content types are:
- Checks the
Expires Header
and theCache-Control Header
to see if the content is not already stale or throws an error. - Creates and saves the cache item
- Returns the cache item or the thrown error
Returns the cache item or an error if there's no cache-item for this request.
Call this before the actual request. Checks for and returns a fresh cache item or an error.
- Checks the
include
andexclude
patterns to see if this should be handled by lakka or throws an error. - Checks the
Cache-Control Header
or throws an Error- Cachable:
public
,private
,Immutable
,proxy-revalidate
- Non-cachable
must-revalidate
,no-cache
,no-store
,
- Cachable:
- Checks the
Accept Header
or throws an Error- Valid content types are:
application/json
,text/x-json
,text/plain
, ```text/html``
- Valid content types are:
- Checks the
Content Type Header
or throws an Error- Valid content types are:
application/json
,text/x-json
,text/plain
,text/html
- Valid content types are:
- Checks there's a fresh item for this URI or throws an Error.
- Returns the cache item or an error if there's no cache-item for this request.
Returns a fresh cache item or an error if there's no cache-item for this request.
Force clear an item from lakka. Removes all stored content for this URI.
- String: The URI to clear from lakka
Flush the lakka cache. Remove all items.
- Writen in ECMAScript 2018 on ```nodejs v9.0.0``
- Transpiled
babel v6.26.0
- Bundled with
webpack v3.10.0
- 100% code coverage using
mocha v3.5.0
,chai v4.1.1
andnyc v11.2.1
Licensed under the MIT license.
Copyright (c) 2016 - 2021 Martin Krause [email protected] http://martinkr.github.io)