@nuxtjs/device

Nuxt用のデバイス検出モジュール

Nuxt banner

Nuxt Device

npm version npm downloads License Nuxt

Nuxtアプリケーションでデバイスの種類を検出します。

CodeSandboxのデモをご覧ください。

インストール

npx nuxi@latest module add device

!NOTE モジュールのNuxt 2バージョンは、2.xブランチにあります。

フラグ

以下のフラグを使用して、デバイスの種類を検出できます。

  • $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

ユーザーエージェントも注入され、$device.userAgentでアクセスできます。

クローラー検出は、crawler-user-agentsパッケージによって提供されます。

使用方法

script setup内でuseDevice()コンポーザブルを使用するか、テンプレート内で直接$deviceヘルパーを使用できます。

<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>

レイアウトを動的に変更する

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

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

オプション

defaultUserAgent

user-agentヘッダーのデフォルト値を設定します(npm run generateを実行する際に便利です)。

デフォルト: 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

有効

モジュールを条件付きで有効にします。

デフォルト: true

!WARNING このオプションは非推奨です。次のメジャーリリースで削除されます。

refreshOnResize

ウィンドウのリサイズ時にフラグを更新します。

デフォルト: false

!WARNING このオプションは非推奨です。次のメジャーリリースで削除されます。

Amazon CloudFrontのサポート

ユーザーエージェントがAmazon CloudFrontの場合、モジュールは以下のヘッダーをチェックします。

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

ビューアのデバイスタイプの決定に関する詳細については、Amazon CloudFrontのドキュメントをご覧ください。

!CAUTION Amazon CloudFrontではisWindowsisMacOSフラグは利用できません。

Cloudflareのサポート

このモジュールはCF-Device-Typeヘッダーをチェックします。

デバイスタイプ検出に関する詳細については、Cloudflareのドキュメントをご覧ください。

ライセンス

MITライセンス