true-link
v0.1.0
Published
Tiny zero-config network truth detector. Detects real connectivity over navigator.onLine via fetch ping with timeout. Catches lie-fi, classifies connection strength.
Maintainers
Readme
true-link
Tiny zero-config network truth detector. Catches lie-fi, classifies connection strength.
navigator.onLine lies in 30% of cases on mobile. true-link uses fetch-ping with timeout to detect real connectivity, classifies as strong | weak | offline, and catches lie-fi before your users notice.
Zero runtime deps, framework adapters included, SSR-safe.
npm i true-link
# or: yarn add true-link
# or: pnpm add true-linkQuick Start
Minimal flow: import → create → subscribe → get { isOnline, strength, latency }.
import { createTrueLink } from "true-link/vanilla";
const tl = createTrueLink({ pingUrl: "/health" });
const unsub = tl.subscribe((v) => {
if (!v) return;
console.log(
`online=${v.isOnline} strength=${v.strength} latency=${v.latency}ms`
);
});
// later:
// unsub();
// tl.destroy();Demo snippet
Use this in a real page (Vite/Parcel/Next). Toggle your network to see it react.
<div id="app" style="padding:16px">
<pre id="out">loading…</pre>
</div>
<script type="module">
import { createTrueLink } from "true-link/vanilla";
const out = document.getElementById("out");
Object.assign(out.style, {
padding: "12px 16px",
font: "14px/1.4 ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace",
background: "#111",
color: "#0f0",
borderRadius: "8px",
});
const tl = createTrueLink({ pingUrl: "/favicon.ico" });
tl.subscribe((v) => {
if (!v) {
out.textContent = "SSR / null";
return;
}
out.textContent =
`online: ${v.isOnline}
strength: ${v.strength}
latency: ${v.latency ?? "n/a"}ms
checking: ${v.isChecking}`;
});
</script>Features
A few concrete, technical reasons it behaves well:
- Catches lie-fi: Ping timeout classifies slow/unstable networks as
weakoroffline. - Zero runtime deps: 0 dependencies at runtime (tree-shakeable ESM).
- Connection strength:
strong | weak | offlinebased on latency, not just boolean online/offline. - SSR-safe: Returns
nullon server, works with React 18useSyncExternalStore.
API (short)
Core snapshot fields you'll typically use:
isOnline—trueifstrength !== 'offline'strength—'strong' | 'weak' | 'offline'latency— ping time in ms, ornullif offline/checkingisChecking—truewhile ping is in flight
Vanilla store:
createTrueLink(options)fromtrue-link/vanilla→ creates a controller withget(),subscribe(),destroy()
Framework adapters:
- React:
useTrueLinkfromtrue-link/react - Vue:
useTrueLinkfromtrue-link/vue - Svelte:
trueLinkfromtrue-link/svelte - Solid:
createTrueLinkfromtrue-link/solid - Angular:
TrueLinkDirectivefromtrue-link/angular
Full types and signatures: see TypeScript IntelliSense or published dist/*.d.ts files.
Adapter Docs: React • Vue • Svelte • Solid • Angular
Links
FAQ • Common pitfalls • Smoke test • Versioning policy
Support the project
If this library saved you time, please consider supporting the development:
- Fiat (Cards/PayPal): via Boosty (one-time or monthly).
- Crypto (USDT/BTC/ETH): view wallet addresses on Telegram.
License
MIT
