https://github.com/alloc/wants-mobile
Parse request headers to see if a mobile-specific response is wanted
https://github.com/alloc/wants-mobile
http nodejs
Last synced: 11 months ago
JSON representation
Parse request headers to see if a mobile-specific response is wanted
- Host: GitHub
- URL: https://github.com/alloc/wants-mobile
- Owner: alloc
- License: mit
- Created: 2020-11-02T17:07:44.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2021-01-23T19:13:19.000Z (about 5 years ago)
- Last Synced: 2025-02-12T03:39:51.236Z (12 months ago)
- Topics: http, nodejs
- Language: TypeScript
- Homepage:
- Size: 5.86 KB
- Stars: 2
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# wants-mobile
Parse request headers to see if a mobile-specific bundle is wanted.
Supports both Node.js and Web Workers.
### Usage
```ts
import wantsMobile from 'wants-mobile'
```
The `wantsMobile` function takes a headers object and returns true when a mobile-specific bundle should be served, otherwise false.
The headers object is expected to be:
- a plain object with lowercase keys, or
- an object whose `get(key)` method supports lowercase keys
It first checks the `Sec-CH-Mobile` header (part of the [WICG "Client Hints" proposal](https://github.com/WICG/ua-client-hints)). If defined, `wantsMobile` returns a boolean (true when `?1`, else false).
If that header is undefined, it then parses the `User-Agent` header using a small set of regular expressions, which can be found [here](https://github.com/alloc/wants-mobile/blob/8c701cb23ceb1570943e0167efd83adcae093e42/index.ts#L20-L38).