Nuxt Bowser
Bowser module for Nuxt 2.
Features
- Helps you integrate
browser/platform/engine
detector
- Provides a
lightweight
, fast
and rich-API
solution (~5kB/min+gz) 🔥
- Allows you to access the module globally by using
this.$browser
- Automatically updates the
html
tag with the appropriate values based on the device info
- Super-easy to use without complicated configurations ✨
- Supports options for
additional
customization
Zero-config
setup ready to go 🚀
Quick Start
- Add
nuxt-bowser
dependency to your project
$ npm i -D nuxt-bowser
- Enable
nuxt-bowser
in the buildModules
section
export default {
buildModules: ['nuxt-bowser'],
bowser: {
}
}
That's it! Start developing your app!
Examples
Here are some code examples
Basic usage
<template>
<section>
<div v-if="$browser.is('mobile')">
<h3>Mobile</h3>
</div>
<div v-else-if="$browser.is('tablet')">
<h3>Tablet</h3>
</div>
<div v-else-if="$browser.is('desktop')">
<h3>Desktop</h3>
</div>
<div v-else-if="$browser.is('tv')">
<h3>TV</h3>
</div>
<div v-else>
<h3>Other</h3>
</div>
</section>
</template>
Get Platform
console.log(this.$browser.getPlatform())
{
type: 'desktop',
vendor: 'Apple'
}
Get OS
console.log(this.$browser.getOS())
{
name: "macOS",
version: "10.15.7",
versionName: "Catalina"
}
Get Browser
console.log(this.$browser.getBrowser())
{
name: 'Chrome',
version: '91.0.4472.77'
}
Get Engine
console.log(this.$browser.getEngine())
{
name: 'Blink'
}
Get Result
console.log(this.$browser.getResult())
{
browser: {
name: 'Chrome',
version: '91.0.4472.77'
},
engine: {
name: 'Blink'
},
os: {
name: 'macOS',
version: '10.15.7',
versionName: 'Catalina'
},
platform: {
type: 'desktop',
vendor: 'Apple'
}
}
Is Anything
Check if the browser
is called anything
, the OS
called anything
or the platform
called anything
console.log(this.$browser.is('mobile'))
console.log(this.$browser.is('desktop'))
console.log(this.$browser.is('tv'))
console.log(this.$browser.is('chrome'))
console.log(this.$browser.is('firefox'))
console.log(this.$browser.is('safari'))
console.log(this.$browser.is('macos'))
Full list
this.$browser.is('mobile')
this.$browser.is('tablet')
this.$browser.is('desktop')
this.$browser.is('tv')
this.$browser.is('windowsphone')
this.$browser.is('windows')
this.$browser.is('macos')
this.$browser.is('ios')
this.$browser.is('android')
this.$browser.is('webos')
this.$browser.is('blackberry')
this.$browser.is('bada')
this.$browser.is('tizen')
this.$browser.is('linux')
this.$browser.is('chromeos')
this.$browser.is('playstation4')
this.$browser.is('roku')
this.$browser.is('amazon_silk')
this.$browser.is('android')
this.$browser.is('bada')
this.$browser.is('blackberry')
this.$browser.is('chrome')
this.$browser.is('chromium')
this.$browser.is('electron')
this.$browser.is('epiphany')
this.$browser.is('firefox')
this.$browser.is('focus')
this.$browser.is('generic')
this.$browser.is('googlebot')
this.$browser.is('google_search')
this.$browser.is('ie')
this.$browser.is('k_meleon')
this.$browser.is('maxthon')
this.$browser.is('edge')
this.$browser.is('mz')
this.$browser.is('naver')
this.$browser.is('opera')
this.$browser.is('opera_coast')
this.$browser.is('phantomjs')
this.$browser.is('puffin')
this.$browser.is('qupzilla')
this.$browser.is('qq')
this.$browser.is('qqlite')
this.$browser.is('safari')
this.$browser.is('sailfish')
this.$browser.is('samsung_internet')
this.$browser.is('seamonkey')
this.$browser.is('sleipnir')
this.$browser.is('swing')
this.$browser.is('tizen')
this.$browser.is('uc')
this.$browser.is('vivaldi')
this.$browser.is('webos')
this.$browser.is('wechat')
this.$browser.is('yandex')
this.$browser.isEngine('edgehtml')
this.$browser.isEngine('blink')
this.$browser.isEngine('trident')
this.$browser.isEngine('presto')
this.$browser.isEngine('gecko')
this.$browser.isEngine('webkit')
Bowser API
this.$browser.getPlatform()
this.$browser.getPlatformType()
this.$browser.getOS()
this.$browser.getOSName()
this.$browser.getOSVersion()
this.$browser.getBrowser()
this.$browser.getBrowserName()
this.$browser.getBrowserVersion()
this.$browser.getEngine()
this.$browser.getEngineName()
this.$browser.getResult()
this.$browser.getUA()
this.$browser.is(anything, includingAlias)
this.$browser.isPlatform(platformType)
this.$browser.isOS(osName)
this.$browser.isBrowser(browserName, includingAlias)
this.$browser.isEngine(engineName)
More info
Module Options
Default options
export default {
bowser: {
name: 'browser',
autoDetect: false,
autoOrientation: false,
userAgent:
'Mozilla/5.0 (Macintosh; Intel Mac OS X 11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36'
}
}
Name
Allows you to customize
the global module name.
export default {
bowser: {
name: 'browser'
}
}
Available globally
this.$browser
const browser = this.$browser
Additional customization (optional)
For example, you can set it to 'device'
or any other name that suits you best.
export default {
bowser: {
name: 'device'
}
}
Available globally
this.$device
const device = this.$device
this.$device.getBrowser()
this.$device.is('chrome')
this.$device.isEngine('blink')
<template>
<div>
<h3 v-if="$device.is('mobile')">Mobile</h3>
<h3 v-else-if="$device.is('tablet')">Tablet</h3>
<h3 v-else>Desktop</h3>
</div>
</template>
Auto Detect
Automatically inserts a custom data-browser
attribute into the html
tag with the appropriate values based on the detected device informations such as operating system
, browser name
and platform type
.
This can be very useful if you need to set style for specific cases.
export default {
bowser: {
autoDetect: true
}
}
Outputs
<html data-browser="macos chrome desktop"></html>
Example (custom styling for chrome
& mobile
devices)
html[data-browser*='chrome mobile'] {
background-color: blue;
}
Example (custom styling for desktop
device)
html[data-browser*='desktop'] {
background-color: green;
}
Additional customization (optional)
Also, you can customize attribute name
or specify a custom prefix
for the detected values
.
export default {
bowser: {
autoDetect: {
attributeName: 'data-device',
valuePrefix: 'is-'
}
}
}
Outputs
<html data-device="is-macos is-chrome is-desktop"></html>
Example
html[data-device*='is-desktop'] {
background-color: green;
}
Auto Orientation
Automatically inserts a custom data-orientation
attribute into the html
tag with the appropriate values based on the detected device orientation
such as portrait
or landscape
.
export default {
bowser: {
autoOrientation: true
}
}
Outputs
<html data-orientation="portrait"></html>
Example (custom styling for portrait
mode)
html[data-orientation='portrait'] {
background-color: greenyellow;
}
Additional customization (optional)
Also, you can customize attribute name
or specify a custom prefix
for the detected values
.
export default {
bowser: {
autoOrientation: {
attributeName: 'data-device-orientation',
valuePrefix: 'is-'
}
}
}
Outputs
<html data-device-orientation="is-landscape"></html>
Example
html[data-device-orientation='is-landscape'] {
background-color: aqua;
}
User Agent
Default userAgent
fallback for Nuxt static
target (nuxt generate
).
export default {
bowser: {
userAgent:
'Mozilla/5.0 (Macintosh; Intel Mac OS X 11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36'
}
}
License
Bowser
MIT License
Copyright (c) Bowser
Nuxt Bowser
MIT License
Copyright (c) Ivo Dolenc