A simple JS library that detects mobile devices in both the browser and NodeJS.
Why use isMobile?
In the Browser
You might not need this library. In most cases, responsive design solves the problem of controlling how to render things across different screen sizes. I recommend a mobile first approach. But there are always edge cases. If you have an edge case, then this library might be for you.
My edge case at the time was redirecting users to a completely separate mobile site. I tried to keep this script small (currently ~1.3k bytes, minified) and simple, because it would need to execute in the
<head>, which is generally a bad idea, since JS blocks the downloading and rendering of all assets while it parses and executes. In the case of mobile redirection, I don't mind so much, because I want to start the redirect as soon as possible, before the device has a chance to start downloading and rendering other stuff. For non-mobile platforms, the script should execute fast, so the browser can quickly get back to downloading and rendering.
How it works in the browser
You might want to use this library to do server-side device detection to minimize the amount of bytes you send back to visitors. Or you have your own arbitrary use case.
How is works in NodeJS
You import and call the
Devices detected by isMobile
In a browser, the following properties of the global
isMobile object will either be
false. In Node,
isMobile will be whatever you named the variable.
isMobile.apple.device(any mobile Apple device)
isMobile.android.device(any mobile Android device; OkHttp user agents will match this)
Amazon Silk devices (also passes Android checks)
isMobile.amazon.device(any mobile Amazon Silk device)
isMobile.windows.device(any mobile Windows device)
isMobile.other.device(any "Other" device)
isMobile.any- any device matched
isMobile.phone- any device in the 'phone' groups above
isMobile.tablet- any device in the 'tablet' groups above
yarn install ismobilejs
A real-word example: I include the minified version of the script, inline, and at the top of the
<head>. Cellular connections tend to suck, so it would be wasteful overhead to open another connection, just to download ~1.3kb of JS:
Alternatively, you can include this library via jsDelivr CDN in a
Visit the isMobile jsDelivr page to get the most up-to-date URL pointing to the lastest version.
After checking out the repo, install dependencies:
Then build the library:
Three versions of the library will be generated:
./cjs/index.js- the CommonJS version of the library
./esm/index.js- the ESModule version of the library
./dist/isMobile.min.js- the browser version of the library
Additionally, types will be output to
This library uses Spotify's web-scripts project to build, lint, test, format and release the this library.
You must use
yarn commit rather than
git commit to commit files. This enforced commit messages to following a specific format and enables automation of release notes and version bump.