npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

inline-cacher

v0.1.0

Published

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

Downloads

5

Readme

Inline Cacher

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

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.