Skip to content
Nuxt.js module for detecting device type.
JavaScript
Branch: master
Clone or download
dotneet Merge pull request #18 from nuxt-community/renovate/all-minor-patch
chore(deps): update devdependency eslint to ^6.2.2
Latest commit 71d21e5 Aug 24, 2019

README.md

@nuxtjs/device

npm version npm downloads Dependencies License Standard JS nuxt-device-detect Dev Token

This module injects flags that indicate a device type into the context and the component instance.

Setup

  • Add @nuxtjs/device to depedency using yarn or npm to your project
  • Add @nuxtjs/device to modules section of nuxt.config.js
{
  modules: [
   '@nuxtjs/device',
  ]
}

Options

defaultUserAgent option can be used for npm run generate.

{
  modules: [
    [
      '@nuxtjs/device',
      {defaultUserAgent: 'Mozilla/5.0 (Linux; Android 5.1.1; Nexus 6 Build/LYZ28E) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.39 Mobile Safari/537.36'}
    ]
  ]
}

Added flags

You can use these flags to detect the device type.

context.isDesktop
context.isMobile
context.isTablet
context.isMobileOrTablet
context.isDesktopOrTablet
context.isIos
context.isWindows
context.isMacOS

instance.$device.isDesktop
instance.$device.isMobile
instance.$device.isTablet
instance.$device.isMobileOrTablet
instance.$device.isDesktopOrTablet
instance.$device.isIos
instance.$device.isWindows
instance.$device.isMacOS

CloudFront Support

If a user-agent is 'Amazon CloudFront', this module checks the both headers 'CloudFront-Is-Mobile-Viewer' and 'CloudFront-Is-Tablet-Viewer'.

Here are the details about the headers: https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/header-caching.html#header-caching-web-device

Caution

isIos, isWindows and isMacOS flags are not available with CloudFront.

Usage

Switch a view

<template>
	<section>
		<div v-if="$device.isDesktop">
			Desktop
		</div>
		<div v-else-if="$device.isTablet">
			Tablet
		</div>
		<div v-else>
			Mobile
		</div>
	</section>
</template>

Ofcourse, you can use $device via this in a script.

Change a layout dynamically

export default {
	layout: (ctx) => ctx.isMobile ? 'mobile' : 'default'
}

License

MIT License

You can’t perform that action at this time.