Giter VIP home page Giter VIP logo

inline-cacher's Introduction

Inline Cacher

Improve performance by caching inline styles. The best of both worlds!

This is an NPM implementation of the Inline Cache methodology.

How does it work?

InlineCacher looks for all inline <style> tags on the page and stores their contents in localStorage. It then sets a cookie so that the server can detect subsequent page requests and avoid rendering the inline styles again. On subsequent pages, InlineCacher injects the cached styles into the page.

This combines a fast first page load and the power of caching.

First page load (demo page) Subsequent page load (demo page)
47.4 KB page load, almost entirely the index page with inline styles 312 B page load

InlineCacher is just 156 bytes g-zipped, and has the power to shave tens/hundreds of KBs per page.

Usage

npm install inline-cacher or grab the dist/inline-cacher.min.js file directly.

On the client

InlineCacher is exported as a UMD module, so import it however you want:

  • RequireJS: define(['inline-cacher'], function (InlineCacher) {})
  • CommonJS: var InlineCacher = require('inline-cacher')
  • Global: <script src="inline-cacher.min.js"></script>, then var InlineCacher = window.InlineCacher

Then just call InlineCacher.init() when you're ready to cache your inline styles.

InlineCacher will set a cookie - inline-cacher - which will be sent to your server on subsequent page visits. You can override the name of the cookie (and other options) by passing an options object to the init function:

InlineCacher.init({
  cookieName: "inline-cacher",
  localStorageName: "InlineCacher"
});

If you need to clear the localStorage cache and the cookie, call InlineCacher.reset().

On the server

Check for the presence of the inline-cacher cookie. If it exists, then assume inline styles have been cached, and avoid sending them in the response to the client.

Example NodeJS (Express):

app.get("/", (req, res) => {
  const styledAlready = req.cookies["inline-cacher"] === "true";
  res.send(`
    <html>
      <head>
        <title>Proof of concept</title>
        ${styledAlready ? "" : `<style>/* lots of inline styles go here */</style>`}
      </head>
      ...etc
  `);
);

Example PHP code:

if (!isset($_COOKIE['inline-cacher'])) {
  echo "<style>/* lots of inline styles go here */</style>";
}

Contributing

Clone the repository, npm install.

Check out the example page:

npm start

Build the minified JS file:

npm run build

The top priority right now is deciding on an invalidation strategy. Other things for the roadmap include passing an optional selector (e.g. style[data-cache-inline]) so that only specified inline styles are cached.

License

MIT.

inline-cacher's People

Contributors

chrisbashton avatar

Stargazers

Atanas Atanasov avatar Palitha Lahiru avatar Kingsley Abru avatar vvvaleee avatar wei xiong avatar Jakub avatar  avatar Красимир Беров avatar Tomcy John avatar Hofer Ivan avatar Omar López avatar Conner Bryan avatar Ira Fich avatar Rudrangshu Nandi avatar Patrick LEFEVRE avatar

Watchers

Patrick LEFEVRE avatar James Cloos avatar  avatar

inline-cacher's Issues

Invalidation strategy

We need a way for the server to tell what has been cached on the client, and not just whether something is cached.

Currently, if you update the CSS for your site, there's no easy way to make sure your visitors' InlineCacher localStorage is cleared. We need to know when to call InlineCacher.reset() and send the latest inline styles.

There are a few approaches we could take:

  1. Setting the cookie value as an expiration date on the cache (e.g. inline-cacher="1559076458")and invalidating if too old.
  2. Setting the cookie value as a hash of the cached CSS (e.g. inline-cacher="abc123def456") and invalidating if different to what's on the server.
  3. Setting a data attribute on the style tag (<style data-version="v1.2.3>) and setting the cookie value to this (e.g. inline-cacher="v1.2.3"), invalidating if too old.

I'm keen to get this right from the beginning to avoid setting up any technical debt.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.