:cherries: Async dependency loader for CSS and JavaScript

Fetch Inject

Build Status Standard - JavaScript Style Guide NPM Downloads per Month NPM Version

Dynamically inline assets into the DOM using Fetch Injection.


Improve website performance and UX by fetching external assets and inlining them into the DOM programmatically. Get a Promise in return.

  • Wraps Fetch API
  • No hard dependencies
  • 398 bytes gzipped


Learn about Fetch Injection and why I created this library on Hack Cabin.

Don’t like to read? Here’s a playground:


Get it on NPM with npm i fetch-inject or Bower with bower install fetch-inject. Also available via CDN using jsDelivr.


  1. Call fetchInject with an array of URLs.
  2. Optionally, handle the returned Promise.

Use Cases

Loading Utility Scripts

Problem: You want to prototype some code using the browser as a REPL.

Solution: Skip the emulators and use the real deal:

]).then(() => {
  console.log(`Successfully loaded Lodash ${_.VERSION}`)

Loading CSS Asynchronously

Problem: PageSpeed Insights dings you for loading blocking CSS and unnecessary styles on initial render.

Solution: Inline your critical path CSS and load non-critical styles asynchronously:


Preventing Script Blocking

Problem: Remote assets can lead to jank or, worse yet, SPOF if not handled correctly.

Solution: Asynchronously load remote scripts without blocking:


Loading Scripts Lazily

Problem: You want to load a script in response to a event without optimistically preloading the asset.

Solution: Create an event listener, respond to the event and then destroy the listener.

const el = document.querySelector('details summary')
el.onclick = (evt) => {
  el.onclick = null  

Responding to Asynchronous Scripts

Problem: You need to perform a synchronous operation immediately after an asynchronous script is loaded.

Solution: You could create a script element and use the async and onload attributes. Or you could…

]).then(() => {
  console.log(`Finish in less than ${moment().endOf('year').fromNow(true)}`)

Combining Resource Types

Problem: You need to asynchronously download multiple related assets of different types.

Solution: Specify multiple URLs of different types when calling:


Ordering Dependent Scripts

Problem: You have several scripts that depend on one another and you want to load them all asynchronously without causing race conditions.

Solution: Call multiple times, forming a promise chain:

  'https://npmcdn.com/[email protected]/dist/js/tether.min.js',
]).then(() => {
    'https://npmcdn.com/[email protected]/dist/js/bootstrap.min.js'

Loading and Handling Composite Libraries

Problem: You want to use library composed of several resources and initialize it as soon as possible.

Solution: This is precisely the kind of activity fetchInject works best at:

const container = document.querySelector('.pswp')
const items = JSON.parse({{ .Params.gallery.main | jsonify }})
]).then(() => {
  const gallery = new PhotoSwipe(container, PhotoSwipeUI_Default, items)

Managing Asynchronous Dependencies

Problem: You want to load some dependencies which requires some dependencies, which requires a dependency.

Solution: You could scatter some links into your document head, blocking initial page render and bloat your application bundle. Or…

const tether = ['/js/tether.min.js']
const drop = [
const tooltip = [
  .then(() => fetchInject(drop))
  .then(() => fetchInject(tooltip))
  .then(() => {
    new Tooltip({
      target: document.querySelector('h1'),
      content: "You moused over the first <b>H1</b>!",
      classes: 'tooltip-theme-arrows',
      position: 'bottom center'

Supported Browsers

All browsers with support for Fetch and Promises.

Fetch will become available in Safari in the Safari 10.1 release that ships with macOS Sierra 10.12.4 and Safari on iOS 10.3. Jon Davis,  Web Technologies Evangelist


  1. Clone the repo with git clone https://github.com/jhabdas/fetch-inject.
  2. Install dev dependencies with npm i (brew install node first on macOS).
  3. Execute npm run for a listing of available commands.


Please use Issues for bugs and enhancement requests only. If you need support, you know where to go. Thanks!

See Also


MIT License 2017 © Josh Habdas

Related Repositories



:cherries: Async dependency loader for CSS and JavaScript ...



Fetch JSON from remote URLs, transform and inject as files into metalsmith pipeline ...



Fetch environmental data from an EDS 1-Wire device and inject it into Cube ...