Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jsheaven/runtime-info
Isomorphic, nano-sized library for runtime detection: isServer(), isBrowser(), isWebWorker()
https://github.com/jsheaven/runtime-info
browser deno javascript nodejs runtime ssg ssr typescript webworker
Last synced: 3 days ago
JSON representation
Isomorphic, nano-sized library for runtime detection: isServer(), isBrowser(), isWebWorker()
- Host: GitHub
- URL: https://github.com/jsheaven/runtime-info
- Owner: jsheaven
- License: mit
- Created: 2023-02-04T18:53:22.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2023-02-05T02:49:26.000Z (over 1 year ago)
- Last Synced: 2024-05-01T22:14:40.916Z (5 months ago)
- Topics: browser, deno, javascript, nodejs, runtime, ssg, ssr, typescript, webworker
- Language: TypeScript
- Homepage:
- Size: 58.6 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
runtime-info
> Nano untility library for JavaScript runtime detection
Purpose
This is important for SSR/SSG, isomorphic runtime code checks: Should certain code be executed or not?
Some code works only on server, some code works in browser, and some in browser, but not in WebWorkers.Instead of a weak `if (typeof window !== 'undefined') { ... }` etc. checks which are easy to mistaken
you can use this safe, fast and nano sized library. Using it gives you more readable code and reliability.Features
- ✅ JavaScript runtime checks: `isBrowser()`, `isServer()`, `isWebWorker()`
- ✅ Just `174 byte` nano sized (ESM, gizpped)
- ✅ Tree-shakable, side-effect free, so maybe just `~58 byte` for you
- ✅ Zero dependencies
- ✅ First class TypeScript support
- ✅ 100% Unit Test coverageInstall
- yarn: `yarn add runtime-info`
- npm: `npm install runtime-info`Use
ESM
```ts
import { isBrowser, isServer, isWebWorker } from 'runtime-info'if (isBrowser()) {
// safely use window, location, etc. here
}if (isServer()) {
// safely use Node.js/Deno API's here like process
}if (isWebWorker()) {
// safely use postMessage() inside of a webworker
}
```CommonJS
```ts
const { isBrowser, isServer, isWebWorker } = require('runtime-info')// same API like ESM variant
```One thing about JSDOM
If you're running inside of a server-side environment where globals like `window` and `document`
are defined (JSDOM), then you're effectively running in a browser-compatible environment, and
this library will return `true` for `isBrowser` and `false` for `isServer`. This is an edge case.
You can check for this using the following code: `navigator.userAgent.includes("jsdom")`