Package detail

@nuxtjs/device

nuxt-modules432.6kMIT3.2.4

Device detection module for Nuxt

readme

Nuxt banner

Nuxt Device

npm version npm downloads License Nuxt

Detect the type of device in your Nuxt applications.

See demo on CodeSandbox.

Installation

npx nuxi@latest module add device

[!NOTE] You can find the Nuxt 2 version of the module on the 2.x branch.

Flags

You can use the following flags to detect the device type:

  • $device.isDesktop
  • $device.isMobile
  • $device.isTablet
  • $device.isMobileOrTablet
  • $device.isDesktopOrTablet
  • $device.isIos
  • $device.isWindows
  • $device.isMacOS
  • $device.isApple
  • $device.isAndroid
  • $device.isFirefox
  • $device.isEdge
  • $device.isChrome
  • $device.isSafari
  • $device.isSamsung
  • $device.isCrawler

The user agent is also injected and accessible with $device.userAgent.

The crawler detection is powered by the crawler-user-agents package.

Usage

You can either use the useDevice() composable inside a script setup, or the $device helper directly in the template:

<template>
  <div>
    <div v-if="$device.isDesktop">Desktop</div>

    <div v-else-if="$device.isTablet">Tablet</div>

    <div v-else>Mobile</div>
  </div>
</template>

<script setup>
const { isMobile } = useDevice()
</script>

Changing Layout Dynamically

<template>
  <div>
    <NuxtLayout :name="$device.isMobile ? 'mobile' : 'default'">
      <!-- page content -->
    </NuxtLayout>
  </div>
</template>

<script setup>
definePageMeta({
  layout: false
})
</script>

Options

defaultUserAgent

Sets the default value for the user-agent header (useful when running npm run generate).

Default: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.39 Safari/537.36

enabled

Enables the module conditionally.

Default: true

[!WARNING] This option is deprecated. It will be removed in the next major release.

refreshOnResize

Refreshes flags on window resize.

Default: false

[!WARNING] This option is deprecated. It will be removed in the next major release.

Amazon CloudFront Support

If the user agent is Amazon CloudFront, the module checks for the following headers:

  • CloudFront-Is-Android-Viewer
  • CloudFront-Is-Desktop-Viewer
  • CloudFront-Is-IOS-Viewer
  • CloudFront-Is-Mobile-Viewer
  • CloudFront-Is-Tablet-Viewer

Read more about determining the viewer's device type in the Amazon CloudFront docs.

[!CAUTION] isWindows and isMacOS flags are not available in Amazon CloudFront.

Cloudflare Support

This module checks for the CF-Device-Type header.

Read more about the device type detection in the Cloudflare docs.

License

MIT License

changelog

Changelog

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

v3.2.4

compare changes

🩹 Fixes

  • Inline crawler-user-agents dependency (#232)

🤖 CI

  • Run CI workflow on Node version from matrix (c504a6f)

❤️ Contributors

v3.2.3

compare changes

🩹 Fixes

  • Generate crawlers regex at build time (#230)

🏡 Chore

  • Update README and module options JSDoc (5483663)
  • Unpin dependencies and update renovate config (a12c6a4)

❤️ Contributors

v3.2.2

compare changes

🩹 Fixes

  • Move runtime types to src/runtime/ dir (57a658a)

❤️ Contributors

v3.2.1

compare changes

🚀 Enhancements

  • Deprecate enabled option (7b64ac6)

🩹 Fixes

  • Do not warn about refreshOnResize deprecation if not explicitly used (11196cd)
  • Add augmentations for $device helper (e758cc6)

💅 Refactors

  • Use single return in plugin (401a366)
  • Shorten useDevice syntax (e528ba7)

🏡 Chore

  • Use local module in playground (eca8ff1)

❤️ Contributors

v3.2.0

compare changes

🚀 Enhancements

  • Module overhaul (#199)
  • Deprecate refreshOnResize option (#211)
  • Detect Google-InspectionTool crawler (#219)
  • Replace manual crawler regex generation with crawler-user-agents (#224)

🩹 Fixes

  • Import Nuxt composables from #imports (#174)
  • Update to latest @nuxt/module-builder (#223)
  • Augment runtime config on @nuxt/schema (a84ad64)
  • Disable 'vue/multi-word-component-names' for test/ dir (502d5b1)
  • Account for absence of module's runtime config in plugin (a311d11)

📖 Documentation

  • Use new nuxi module add command in installation (#187)
  • Update link to 2.x branch (2aec2e7)
  • Remove unused badges (4cdf090)
  • Revert removing badges (eea7813)
  • Update badges style and links (d290e56)
  • Add banner (1130799)

🏡 Chore

  • Add pull request template (2de7def)
  • Lint pull request template (0faf42f)
  • Update renovate config (855e082)
  • Test bundler module resolution (#181)
  • Indicate compatibility with new v4 major (#216)
  • Exclude 2.x branch from renovate (487f042)
  • Remove commented out module config from test fixture (6d860a4)

🤖 CI

  • Add label PR workflow (50a9f4f)
  • Add reproduire (#188)

❤️ Contributors

3.1.1 (2023-09-16)

  • 157 chore: export runtime

  • 158 fix of problem with export named 'default' in nuxt 3

3.1.0 (2023-02-08)

Features

  • cloudfront: extend device type detection (beb89ea)

3.0.0 (2022-11-12)

Features

  • Nuxt3 support (#119)

2.1.0 (2021-04-12)

Features

Bug Fixes

  • fix incorrect regex for edge browser (79e4ffd)
  • make the regex that detect a crawler case sensitive. (8032d85)

2.0.1 (2021-02-27)

Features

  • added support for the in-app browsers of Instagram and Facebook (#59)

2.0.0 (2021-01-27)

⚠ BREAKING CHANGES

  • improvements of the module (#56)

Features

1.2.7 (2020-02-14)

Bug Fixes

  • typings: clean up typings (61aa3ec)

1.2.6 (2020-01-31)

Bug Fixes

  • migrate a type declaration file to nuxt 2.9+

1.2.5 (2020-01-30)

Features

  • Add isAndroid property.

1.2.4 (2019-11-26)

Features

  • Add cloudflare detection.

1.2.3 (2019-08-24)

Bug Fixes

  • add type declaration file. (c971b60)

1.2.2 (2019-08-14)

1.2.1 (2019-08-14)