Skip to content
This repository was archived by the owner on Jan 21, 2022. It is now read-only.

TrigenSoftware/import-css

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

NPM

import-css

Load CSS asynchronously without render blocking.

Getting Started

Install with npm

npm i import-css

or

yarn add import-css

About

import-css contains 3 variants of importCSS function

  1. link.js variant uses <link> tag. If you want to import multiple styles for "progressive loading" recommended move all function calls before </body>.

  2. link-and-body.js also uses<link> tag, but this variant a little lighter than link.js because of restricted to use only in <body>.

  3. xhr.js obviously uses XMLHttpRequest to load styles. For earlier loading start recommended to call this function only in <head>. xhr.js variant loads styles fully async and more faster (if started at <head>). But with <link> variant you have possibilities to include styles from other hosts without CORS. In IE and Edge xhr.js loads styles in parallel unlike <link> variants.

Examples

critical css

section, footer {
	display: none;
}

section.css

section {
	display: block;
	color: green;
}

footer.css

footer {
	display: block;
	color: red;
}

index.html with link.js

<!DOCTYPE html>
<html>
	<head>
		<script>(link.js)</script>
		<style>(critical css)</style>
	</head>
	<body>
		<header>
			<h1>Header</h1>
		</header>
		<section>
			<h1>Section</h1>
		</section>
		<footer>
			<h1>Footer</h1>
		</footer>
		<script>importCSS('section.css')</script>
		<script>importCSS('footer.css')</script>
	</body>
</html>

index.html with xhr.js

<!DOCTYPE html>
<html>
	<head>
		<script>(xhr.js)</script>
		<style>(critical css)</style>
		<script>importCSS('section.css')</script>
		<script>importCSS('footer.css')</script>
	</head>
	<body>
		<header>
			<h1>Header</h1>
		</header>
		<section>
			<h1>Section</h1>
		</section>
		<footer>
			<h1>Footer</h1>
		</footer>
	</body>
</html>

About

Load CSS asynchronously without render blocking in the given order.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published