Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/malchata/preconnect
A nano-sized preconnect hint wrapper.
https://github.com/malchata/preconnect
Last synced: 3 months ago
JSON representation
A nano-sized preconnect hint wrapper.
- Host: GitHub
- URL: https://github.com/malchata/preconnect
- Owner: malchata
- Created: 2019-07-21T15:31:23.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2023-01-06T02:02:43.000Z (about 2 years ago)
- Last Synced: 2024-10-08T23:33:11.094Z (3 months ago)
- Language: JavaScript
- Homepage:
- Size: 536 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 10
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# preconnect
A nano-sized `preconnect` hint wrapper.
ES5 (.js) version
ES6 (.mjs) version
---
preconnect is a very small script that allows you to programmatically invoke a [`preconnect` hint](https://developers.google.com/web/fundamentals/performance/resource-prioritization#preconnect) to any host to mask connection latency. The script is designed so that you can invoke this hint whenever is appropriate for your application.
## Installing
Since preconnect is Just JavaScript™, you can install it with npm as a production dependency:
```
npm i preconnect --save
```If you're not the npm type, grab one (or both) of the minified versions in [this repo's `dist` folder](https://github.com/malchata/preconnect/tree/master/dist). There are two minified versions:
- [`preconnect.min.js`](https://raw.githubusercontent.com/malchata/preconnect/master/dist/preconnect.min.js) is the Babel-fied ES5 build. It assigns a variable named `preconnect` on the `window`.
- [`preconnect.min.mjs`](https://raw.githubusercontent.com/malchata/preconnect/master/dist/preconnect.min.mjs) is the untransformed minified ES6 build. Its `default` `export` is a function eponymously named `preconnect`.## Usage
```javascript
import preconnect from "preconnect";const preconnecter = new preconnect({
// Injects `dns-prefetch` hints in addition to `preconnect` hints
getDns: true
});// Preconnects to a link as the user hovers over it
document.getElementById("some-link").addEventListener("mouseover", event => {
preconnecter.add(event.target.href);
}, {
// Execute this event handler code only once avoid multiple injections of hints
once: true
});
```In the above example, an early connection is established to the host specified in an `` element's `href` value. This could potentially speed up navigation to that host for the user, improving the perceived performance of the navigation.
## Options
When you instantiate a new instance of preconnect, you can pass in an options object. There are two options:
- **`getDns`** _(default: `false`)_
Inject a [`dns-prefetch` hint](https://developer.mozilla.org/en-US/docs/Learn/Performance/dns-prefetch) in addition to a `preconnect` hint. Some browsers don't support `preconnect`, but some of those browsers _do_ support `dns-prefetch`. Enabling this ensures those browsers still receive some kind of benefit.
- **`timeout`** _(default: `0`)_
This script can use [`requestIdleCallback`](https://developer.mozilla.org/en-US/docs/Web/API/Window/requestIdleCallback) to take advantage of idle browser time. This helps to reduce monopolization of the main thread so that the page is more responsive to user input. This option specifies, in milliseconds, the deadline by which `requestIdleCallback` must inject the resource hint elements into the document ``. A value of `0` (the default) disables the use of `requestIdleCallback` entirely.## Contributing
If you'd like to contribute, please file an issue first so we can discuss. Because I'd like to keep this script very small and simple, new features aren't likely to be added unless the weight they add can be justified.
## Author info
My name is Jeremy Wagner. I'm an [independent web performance consultant](https://jeremy.codes/). I [write about web stuff](https://jeremy.codes/writing/), and sometimes I even get to [talk about web stuff](https://speaking.jeremy.codes/). I also ramble on Twitter [@malchata](https://twitter.com/malchata) and make [web performance videos on YouTube](https://hellforperf.dev/).