use-internet-connection-status 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +178 -0
- package/dist/cjs/index.js +2 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/esm/index.js +2 -0
- package/dist/esm/index.js.map +1 -0
- package/package.json +45 -0
package/README.md
ADDED
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
# useInternetConnectionStatus
|
|
2
|
+
|
|
3
|
+
A React hook to detect the user's internet connection status.
|
|
4
|
+
|
|
5
|
+
## How it works
|
|
6
|
+
|
|
7
|
+
The hook uses the browser's native APIs to detect connectivity:
|
|
8
|
+
|
|
9
|
+
1. **Initial state**: Reads `navigator.onLine` to get the current connection status
|
|
10
|
+
2. **Events**: Listens to the `online` and `offline` events on the global object (`globalThis`)
|
|
11
|
+
3. **Synchronization**: When connectivity changes, the state is updated
|
|
12
|
+
|
|
13
|
+
The hook returns an object with the `online` property (boolean) that indicates whether the user is connected to the internet.
|
|
14
|
+
|
|
15
|
+
## Requirements
|
|
16
|
+
|
|
17
|
+
- **Node.js**: >= 22
|
|
18
|
+
- **React**: >= 19.2.4
|
|
19
|
+
|
|
20
|
+
> The hook may work with earlier React versions, but React 19.2.4 is recommended as it includes security patches for [CVE-2025-55184 and CVE-2025-67779](https://react.dev/blog/2025/12/11/denial-of-service-and-source-code-exposure-in-react-server-components). If your app’s React code does not use a server, your app is not affected by this vulnerability.
|
|
21
|
+
|
|
22
|
+
## Installation
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
bun install
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
### What is `--ignore-scripts`?
|
|
29
|
+
|
|
30
|
+
During installation, some packages may run post-installation scripts. If you don't need these features or want a faster installation, you can use:
|
|
31
|
+
|
|
32
|
+
```bash
|
|
33
|
+
bun install --ignore-scripts
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
In this project, `--ignore-scripts` is enabled by default for security.
|
|
37
|
+
|
|
38
|
+
This skips all post-installation scripts. Use it when:
|
|
39
|
+
- You only need the JavaScript/TypeScript code
|
|
40
|
+
- You want to speed up the installation process
|
|
41
|
+
- The scripts are not necessary for your use case
|
|
42
|
+
|
|
43
|
+
## Available Scripts
|
|
44
|
+
|
|
45
|
+
| Script | Command | Description |
|
|
46
|
+
|--------|---------|-------------|
|
|
47
|
+
| Install dependencies | `bun install` | Installs all project dependencies |
|
|
48
|
+
| Build | `bun run build` | Compiles the project using Rollup |
|
|
49
|
+
| Development | `bun run dev` | Compiles in watch mode (automatically recompiles when detecting changes) |
|
|
50
|
+
|
|
51
|
+
### Build
|
|
52
|
+
|
|
53
|
+
Generates CommonJS and ES Modules files in the `dist/` folder:
|
|
54
|
+
|
|
55
|
+
```bash
|
|
56
|
+
bun run build
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
### Development (Watch mode)
|
|
60
|
+
|
|
61
|
+
Automatically compiles whenever changes are detected in the code:
|
|
62
|
+
|
|
63
|
+
```bash
|
|
64
|
+
bun run dev
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## Project Configuration
|
|
68
|
+
|
|
69
|
+
This project includes security and best practice configurations in the following files:
|
|
70
|
+
|
|
71
|
+
### `.npmrc`
|
|
72
|
+
|
|
73
|
+
Configuration file for npm/bun:
|
|
74
|
+
|
|
75
|
+
```ini
|
|
76
|
+
ignoreScripts = true # Prevents scripts from running during installation
|
|
77
|
+
engine-strict = true # Fails if Node version doesn't satisfy the engines field in package.json
|
|
78
|
+
audit = true # Reports vulnerabilities in dependencies after installation
|
|
79
|
+
minimumReleaseAge = 10080 # Prevents installing packages released in the last 7 days
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
### `bunfig.toml`
|
|
83
|
+
|
|
84
|
+
Bun-specific configuration file:
|
|
85
|
+
|
|
86
|
+
```toml
|
|
87
|
+
[install]
|
|
88
|
+
ignoreScripts = true # Prevents scripts from running during installation
|
|
89
|
+
audit = true # Reports vulnerabilities in dependencies
|
|
90
|
+
|
|
91
|
+
[install.engines]
|
|
92
|
+
strict = true # Strict engine version (Node >= 22)
|
|
93
|
+
|
|
94
|
+
[install.resolution]
|
|
95
|
+
minimumReleaseAge = "7d" # Prevents installing packages released in the last 7 days
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
### Configuration Explanation
|
|
99
|
+
|
|
100
|
+
| Configuration | File | What it does |
|
|
101
|
+
|--------------|------|--------------|
|
|
102
|
+
| `ignoreScripts` | .npmrc, bunfig.toml | Prevents malicious scripts from running during `bun install`. Improves security. |
|
|
103
|
+
| `engine-strict` | .npmrc | Requires Node.js version to meet `engines` in package.json (>= 22). Prevents incompatible versions. |
|
|
104
|
+
| `audit` | .npmrc, bunfig.toml | After installing, shows a summary of known vulnerabilities in dependencies. |
|
|
105
|
+
| `strict` | bunfig.toml | Strict engine version for bun (equivalent to engine-strict). |
|
|
106
|
+
| `minimumReleaseAge` | .npmrc, bunfig.toml | Prevents installing packages released in the last 7 days. Avoids vulnerabilities in new packages. |
|
|
107
|
+
|
|
108
|
+
### `.npmignore`
|
|
109
|
+
|
|
110
|
+
File that specifies which files are excluded from the published npm package:
|
|
111
|
+
|
|
112
|
+
```
|
|
113
|
+
node_modules/ # Dependencies (already included in install)
|
|
114
|
+
src/ # Source code (already compiled to dist/)
|
|
115
|
+
rollup.config.js # Build configuration
|
|
116
|
+
tsconfig.json # TypeScript configuration
|
|
117
|
+
bunfig.toml # Bun configuration
|
|
118
|
+
.git/ # Git folder
|
|
119
|
+
bun.lock # Bun lock file
|
|
120
|
+
*.md # Documentation (except README)
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
Only published to npm: `dist/`, `package.json`, `README.md` and `README-en.md`.
|
|
124
|
+
|
|
125
|
+
## React Usage Example
|
|
126
|
+
|
|
127
|
+
```tsx
|
|
128
|
+
import { useInternetConnectionStatus } from 'use-internet-connection-status'
|
|
129
|
+
|
|
130
|
+
function App() {
|
|
131
|
+
const { online } = useInternetConnectionStatus()
|
|
132
|
+
|
|
133
|
+
return (
|
|
134
|
+
<div>
|
|
135
|
+
<h1>Connection Status</h1>
|
|
136
|
+
{online ? (
|
|
137
|
+
<p>✅ You are connected to the internet</p>
|
|
138
|
+
) : (
|
|
139
|
+
<p>❌ No internet connection</p>
|
|
140
|
+
)}
|
|
141
|
+
</div>
|
|
142
|
+
)
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
export default App
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
### Example with fallback message
|
|
149
|
+
|
|
150
|
+
```tsx
|
|
151
|
+
import { useInternetConnectionStatus } from 'use-internet-connection-status'
|
|
152
|
+
|
|
153
|
+
function NetworkStatus() {
|
|
154
|
+
const { online } = useInternetConnectionStatus()
|
|
155
|
+
|
|
156
|
+
if (!online) {
|
|
157
|
+
return (
|
|
158
|
+
<div style={{ padding: '1rem', backgroundColor: '#fee2e2' }}>
|
|
159
|
+
⚠️ You have disconnected from the internet. Some features may not be available.
|
|
160
|
+
</div>
|
|
161
|
+
)
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
return (
|
|
165
|
+
<div style={{ padding: '1rem', backgroundColor: '#dcfce7' }}>
|
|
166
|
+
🌐 Connected
|
|
167
|
+
</div>
|
|
168
|
+
)
|
|
169
|
+
}
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
## API
|
|
173
|
+
|
|
174
|
+
### Returns
|
|
175
|
+
|
|
176
|
+
| Property | Type | Description |
|
|
177
|
+
|-----------|------|-------------|
|
|
178
|
+
| `online` | `boolean` | `true` if there is an internet connection, `false` otherwise |
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
"use strict";var e=require("react");exports.useInternetConnectionStatus=function(){const[n,t]=e.useState(navigator.onLine),i=async()=>t(navigator.onLine);return e.useEffect(()=>(globalThis.addEventListener("online",i),globalThis.addEventListener("offline",i),()=>{globalThis.removeEventListener("online",i),globalThis.removeEventListener("offline",i)}),[]),{online:n}};
|
|
2
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../../../src/hooks/useInternetStatus.ts"],"sourcesContent":["import { useEffect, useState } from 'react'\r\n\r\nexport default function useInternetConnectionStatus() {\r\n const [online, setOnline] = useState(navigator.onLine)\r\n\r\n const handleOnlineStatus = async () => setOnline(navigator.onLine)\r\n\r\n useEffect(() => {\r\n globalThis.addEventListener('online', handleOnlineStatus)\r\n globalThis.addEventListener('offline', handleOnlineStatus)\r\n\r\n return () => {\r\n globalThis.removeEventListener('online', handleOnlineStatus)\r\n globalThis.removeEventListener('offline', handleOnlineStatus)\r\n }\r\n }, [])\r\n\r\n return { online }\r\n}\r\n"],"names":["online","setOnline","useState","navigator","onLine","handleOnlineStatus","async","useEffect","globalThis","addEventListener","removeEventListener"],"mappings":"wEAEc,WACZ,MAAOA,EAAQC,GAAaC,EAAAA,SAASC,UAAUC,QAEzCC,EAAqBC,SAAYL,EAAUE,UAAUC,QAY3D,OAVAG,EAAAA,UAAU,KACRC,WAAWC,iBAAiB,SAAUJ,GACtCG,WAAWC,iBAAiB,UAAWJ,GAEhC,KACLG,WAAWE,oBAAoB,SAAUL,GACzCG,WAAWE,oBAAoB,UAAWL,KAE3C,IAEI,CAAEL,SACX"}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import{useState as n,useEffect as e}from"react";function o(){const[o,i]=n(navigator.onLine),t=async()=>i(navigator.onLine);return e(()=>(globalThis.addEventListener("online",t),globalThis.addEventListener("offline",t),()=>{globalThis.removeEventListener("online",t),globalThis.removeEventListener("offline",t)}),[]),{online:o}}export{o as useInternetConnectionStatus};
|
|
2
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../../../src/hooks/useInternetStatus.ts"],"sourcesContent":["import { useEffect, useState } from 'react'\r\n\r\nexport default function useInternetConnectionStatus() {\r\n const [online, setOnline] = useState(navigator.onLine)\r\n\r\n const handleOnlineStatus = async () => setOnline(navigator.onLine)\r\n\r\n useEffect(() => {\r\n globalThis.addEventListener('online', handleOnlineStatus)\r\n globalThis.addEventListener('offline', handleOnlineStatus)\r\n\r\n return () => {\r\n globalThis.removeEventListener('online', handleOnlineStatus)\r\n globalThis.removeEventListener('offline', handleOnlineStatus)\r\n }\r\n }, [])\r\n\r\n return { online }\r\n}\r\n"],"names":["useInternetConnectionStatus","online","setOnline","useState","navigator","onLine","handleOnlineStatus","async","useEffect","globalThis","addEventListener","removeEventListener"],"mappings":"gDAEc,SAAUA,IACtB,MAAOC,EAAQC,GAAaC,EAASC,UAAUC,QAEzCC,EAAqBC,SAAYL,EAAUE,UAAUC,QAY3D,OAVAG,EAAU,KACRC,WAAWC,iBAAiB,SAAUJ,GACtCG,WAAWC,iBAAiB,UAAWJ,GAEhC,KACLG,WAAWE,oBAAoB,SAAUL,GACzCG,WAAWE,oBAAoB,UAAWL,KAE3C,IAEI,CAAEL,SACX"}
|
package/package.json
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "use-internet-connection-status",
|
|
3
|
+
"description": "A React hook to check the internet status of the user.",
|
|
4
|
+
"version": "0.0.1",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "https://github.com/adv2028/use-internet-connection-status.git"
|
|
9
|
+
},
|
|
10
|
+
"bugs": {
|
|
11
|
+
"url": "https://github.com/adv2028/use-internet-connection-status/issues"
|
|
12
|
+
},
|
|
13
|
+
"homepage": "https://github.com/adv2028/use-internet-connection-status#readme",
|
|
14
|
+
"main": "dist/cjs/index.js",
|
|
15
|
+
"module": "dist/esm/index.js",
|
|
16
|
+
"files": [
|
|
17
|
+
"dist"
|
|
18
|
+
],
|
|
19
|
+
"scripts": {
|
|
20
|
+
"build": "rollup -c",
|
|
21
|
+
"dev": "rollup -c -w"
|
|
22
|
+
},
|
|
23
|
+
"peerDependencies": {
|
|
24
|
+
"typescript": "^6.0.2",
|
|
25
|
+
"react": "^19.2.4"
|
|
26
|
+
},
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"@rollup/plugin-commonjs": "^29.0.2",
|
|
29
|
+
"@rollup/plugin-node-resolve": "^16.0.3",
|
|
30
|
+
"@rollup/plugin-terser": "^1.0.0",
|
|
31
|
+
"@rollup/plugin-typescript": "^12.3.0",
|
|
32
|
+
"@types/bun": "latest",
|
|
33
|
+
"react": "^19.2.4",
|
|
34
|
+
"@types/react": "^19.2.14",
|
|
35
|
+
"rollup": "^4.60.0",
|
|
36
|
+
"rollup-plugin-dts": "^6.4.1",
|
|
37
|
+
"tslib": "^2.8.1"
|
|
38
|
+
},
|
|
39
|
+
"engines": {
|
|
40
|
+
"node": ">=22",
|
|
41
|
+
"npm": ">=11.12.1"
|
|
42
|
+
},
|
|
43
|
+
"license": "MIT",
|
|
44
|
+
"author": "Antonella Di Virgilio"
|
|
45
|
+
}
|