
Security News
Django Joins curl in Pushing Back on AI Slop Security Reports
Django has updated its security policies to reject AI-generated vulnerability reports that include fabricated or unverifiable content.
fetch-inject
Advanced tools
A library used to dynamically inline content into the DOM using Fetch Injection.
Fetch Injection is performance optimization technique for loading resources into the DOM asynchronously using the Fetch API. Use it to fetch async resources in parallel (even across the network), and inject them into a document programmatically in a desired sequence.
Understand why this library exists by reading the intro article on Hack Cabin.
Don't have time to read? Here's a playground:
https://codepen.io/jhabdas/pen/MpVeOE?editors=0012
Fetch Inject is available on NPM, Bower and CDN.
npm i fetch-inject
,bower install fetch-inject
; or,Promise<Array<Object>> fetchInject(inputs, promise)
Array
containing elements of type USVString
or Request
.
Promise
to await before injecting fetched resources.
A Promise that resolves to an Array
of Object
s. Each Object
contains a list of resolved properties of the Response Body used in the module, e.g.
[{
blob: { size: 44735, type: "application/javascript" },
text: "/*!↵ * Bootstrap v4.0.0-alpha.5 ... */"
}, {
blob: { size: 31000, type: "text/css" },
text: "/*!↵ * Font Awesome 4.7.0 ..."
}]
Problem: Remote assets can lead to jank or, worse yet, SPOF if not handled correctly.
Solution: Asynchronously load remote scripts without blocking:
fetchInject([
'/bower_components/jquery/dist/jquery.js',
'/bower_components/what-input/dist/what-input.js'
])
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:
fetchInject([
'/css/non-critical.css',
'https://cdn.jsdelivr.net/fontawesome/4.7.0/css/font-awesome.min.css'
])
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) => {
fetchInject([
'https://cdn.jsdelivr.net/smooth-scroll/10.2.1/smooth-scroll.min.js'
])
el.onclick = null
}
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...
fetchInject([
'https://cdn.jsdelivr.net/momentjs/2.17.1/moment.min.js'
]).then(() => {
console.log(`Finish in less than ${moment().endOf('year').fromNow(true)}`)
})
Problem: You have several scripts that depend on one another and you want to load them all asynchronously, in parallel, without causing a race condition.
Solution:
Pass fetchInject
to itself as a second argument, forming a recursion:
fetchInject([
'https://npmcdn.com/bootstrap@4.0.0-alpha.5/dist/js/bootstrap.min.js'
], fetchInject([
'https://cdn.jsdelivr.net/jquery/3.1.1/jquery.slim.min.js',
'https://npmcdn.com/tether@1.2.4/dist/js/tether.min.js'
]))
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 }})
fetchInject([
'/css/photoswipe.css',
'/css/default-skin/default-skin.css',
'/js/photoswipe.min.js',
'/js/photoswipe-ui-default.min.js'
]).then(() => {
const gallery = new PhotoSwipe(container, PhotoSwipeUI_Default, items)
gallery.init()
})
Problem: You want to load some dependencies which require some dependencies, which require a dependency. You want it all in parallel, and you want it now.
Solution:
You could scatter some link
s into your document head, blocking initial page render. You could bloat your application bundle with scripts the user might not actually need half the time. Or you could...
const tether = ['https://cdn.jsdelivr.net/tether/1.4.0/tether.min.js']
const drop = [
'https://cdn.jsdelivr.net/drop/1.4.2/js/drop.min.js'
]
const tooltip = [
'https://cdn.jsdelivr.net/tooltip/1.2.0/tooltip.min.js',
'https://cdn.jsdelivr.net/tooltip/1.2.0/tooltip-theme-arrows.css'
]
fetchInject(tooltip,
fetchInject(drop,
fetchInject(tether)
)
).then(() => {
new Tooltip({
target: document.querySelector('h1'),
content: "You moused over the first <b>H1</b>!",
classes: 'tooltip-theme-arrows',
position: 'bottom center'
})
})
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
git clone https://github.com/jhabdas/fetch-inject
.npm i
.npm run
for a listing of available commands.Please use Issues for bugs and enhancement requests only. Use npm run commit
to create Commitizen-friendly commit messages. If you need support, you know where to go. Thanks!
window.fetch
fetch
in NodeISC
FAQs
Dynamically inline assets into the DOM using Fetch Injection.
The npm package fetch-inject receives a total of 445 weekly downloads. As such, fetch-inject popularity was classified as not popular.
We found that fetch-inject demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Django has updated its security policies to reject AI-generated vulnerability reports that include fabricated or unverifiable content.
Security News
ECMAScript 2025 introduces Iterator Helpers, Set methods, JSON modules, and more in its latest spec update approved by Ecma in June 2025.
Security News
A new Node.js homepage button linking to paid support for EOL versions has sparked a heated discussion among contributors and the wider community.