GithubHelp home page GithubHelp logo

tobias-edwards / astro-internalise-css Goto Github PK

View Code? Open in Web Editor NEW
1.0 2.0 0.0 74 KB

Astro integration to internalise all external style sheets in HTML files.

License: MIT License

TypeScript 100.00%
astro-component astro-integration css

astro-internalise-css's Introduction

DEPRECATED

Astro supports this capability natively via build.inlineStylesheets since [email protected].

astro-internalise-css

Astro integration to internalise all external style sheets in HTML files.

Before:

<html>
  <head>
    <link href="/foo.css" />
    <link href="/bar.css" />
  </head>
</html>
/* foo.css */
.foo {
  color: blue;
}
/* bar.css */
.bar {
  color: red;
}

After:

<html>
  <head>
    <style type="text/css">
      .foo {
        color: red;
      }
    </style>
    <style type="text/css">
      .bar {
        color: blue;
      }
    </style>
  </head>
</html>

And unreferenced external style sheets foo.css and bar.css removed!

Installation

Automatic integration setup:

npx astro add astro-internalise-css

Or manually:

npm install -D astro-internalise-css
// astro.config.mjs
import astroInternaliseCSS from "astro-internalise-css";

export default {
  integrations: [astroInternaliseCSS()],
};

Motivation

When a browser requests a website, the website is transferred to the client in packets. The size of these packet transfers will get incrementally larger (based on TCP slow start), but the first packet is 14KB.

Many websites are small enough to internalise external style sheets for each HTML file and still be under 14KB, thus the website can be delivered to the client in the first packet transfer. This is faster than delivering the HTML and external style sheets in separate packets where delays are incurred from extra fetches. Even if the website is not under 14KB, internalising CSS will usually be more performant for smaller sites.

Note: one such benefit of using external style sheets is the browser can cache individual CSS files, and so if the website is large and changes over time are made to select CSS files, then it may make sense to avoid this integration to take advantage of browser caching.

Contributing

Please report any issues or feature requests on GitHub. Contributions are welcome!

I appreciate the current implementation is quite simple and naive but it gets the job done (for now).

Acknowledgements

Inspired by astro-single-file.

Astro is working on integrating CSS inlining natively. When this is available, this integration may be redundant.

astro-internalise-css's People

Contributors

tobias-edwards avatar

Stargazers

 avatar

Watchers

 avatar  avatar

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.