akashic-js

Akashic.js - Quicksave for Websites

Akashic.js is a lightweight JavaScript library designed to enhance website performance and user experience by caching and serving static assets (CSS, HTML fragments, and JavaScript).

Features

Getting Started

Installation

The easiest way to include Akashic.js in your project is by adding the script tag to your HTML:

<script src="path/to/akashic.min.js" defer></script>

Replace path/to/akashic.min.js with the actual path to the Akashic.js file in your project. It’s recommended to include the defer attribute for optimal loading.

Usage

Akashic.js offers two ways to declare and manage your assets:

1. Using the data-akashic attribute

You can define all your assets as a JSON string within the data-akashic attribute of the Akashic.js script tag.

<script src="akashic.min.js" defer data-akashic='{ /* JSON config */ }'></script>

The JSON config can also be LZW compressed and/or encoded to Base64. Use a tool like Code Beautify to minify your JSON config.

{
  "html": [
    {
      "fn": "header.html.min.js",
      "pt": "/hfrags/",
      "vr": "1",
      "st": "ls"
    },
    {
      "fn": "images.html.min.js",
      "pt": "/hfrags/",
      "vr": "1",
      "st": "indb"
    }
  ],
  "css": [
    {
      "fn": "main.min.css",
      "pt": "/styles/",
      "id": "",
      "vr": "1",
      "st": "ls"
    },
    {
      "fn": "normalize.min.css",
      "pt": "/styles/",
      "id": "",
      "vr": "1",
      "st": "ls"
    }
  ],
  "js": [
    {
      "fn": "theme.min.js",
      "pt": "/scripts/",
      "id": "",
      "pr": "head",
      "vr": "1",
      "st": "ls"
    },
    {
      "fn": "main.min.js",
      "pt": "/scripts/",
      "id": "",
      "pr": "body",
      "vr": "1",
      "st": "indb"
    }
  ]
}
Attribute details for data-akashic JSON:

2. Using Custom HTML Tags

You can also declare assets using custom HTML tags directly within your head or body.

<head>
    <link rel="akashic" data-tag="css" data-name="normalize.min.css" data-path="https://cdnjs.cloudflare.com/ajax/libs/normalize/8.0.1/" data-version="1" data-store="ls">
    <link rel="akashic" data-tag="css" data-name="main.min.css" data-path="/styles/" data-id="light" data-version="1" data-store="indb">
    <link rel="akashic" data-tag="js" data-name="theme.min.js" data-path="/scripts/" data-version="1" data-store="indb">
</head>
<body>
    <akashic-html data-name="home.html.min.js" data-path="/hfrags/" data-version="1" data-store="ls"></akashic-html>
    <akashic-css data-name="special.min.css" data-path="/styles/" data-version="1" data-store="ls"></akashic-html>
    <akashic-js data-name="main.min.js" data-path="/scripts/" data-version="1" data-store="indb"></akashic-js>
</body>
Attribute Details for Custom Tags:

How it Works

  1. Asset Consolidation: On page load, Akashic.js scans for declared assets (via data-akashic attribute or custom tags, or both) and consolidates them into a single internal object.

  2. Version Check: For each asset, it checks if a cached version exists in localStorage and if its version matches or is newer than the declared version.

  3. Fetch & Cache:
    • If an asset is new or its version is outdated, Akashic.js fetches the resource from its specified path and filename.
    • The fetched content is then compressed using LZW and stored in either localStorage (“ls”) or IndexedDB (“indb”) based on your configuration.
  4. Inject & Serve:
    • For assets already in cache or newly fetched, Akashic.js dynamically injects the content directly into the DOM (as <style> tags for CSS, and <script> tags for HTML fragments and JavaScript). This minimizes network requests on subsequent visits.
    • As HTML fragments are stored as *.html.js or *.html.min.js files, they will contain both the html code and some js code for inserting the html code somewhere in the DOM when the file is loaded into a <script> tag.
  5. Cleanup: After processing, Akashic.js removes its temporary global variables and any custom tags from the DOM, leaving a clean page.

Releases

Release links can be found in CDN.md.

Development

To contribute or set up a development environment:

  1. Clone the repository:
git clone https://github.com/NaeemBolchhi/akashic-js.git
cd akashic-js
  1. Make your changes.
  2. Run tests.
  3. Open a Pull Request!

Gratitude

License

Akashic.js is released under the MIT License.