Cloudflare support
mainCF-Device-Type header to determine the device type.repository·main·Indexed 21 days ago
https://github.com/nuxt-modules/deviceA Nuxt module for detecting device types (mobile, tablet, desktop), operating systems, browsers, and crawlers. It provides the $device helper and useDevice() composable to access detection flags, supports Amazon CloudFront and Cloudflare headers, and includes a configurable defaultUserAgent for static site generation.
CF-Device-Type header to determine the device type.When the user agent is Amazon CloudFront, the module automatically checks for specific CloudFront device headers instead of the standard user agent string:
CloudFront-Is-Android-ViewerCloudFront-Is-Desktop-ViewerCloudFront-Is-IOS-ViewerCloudFront-Is-Mobile-ViewerCloudFront-Is-Tablet-ViewerCAUTION The
$device.isWindowsand$device.isMacOSflags are not available when using Amazon CloudFront.
To add device detection capabilities to your Nuxt application, use the Nuxt module installation command.
npx nuxt module add deviceThe defaultUserAgent option allows you to set a fallback value for the user-agent header. This is particularly useful when running npm run generate (Static Site Generation) to ensure the crawler/generator simulates a specific device.
Default value: 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
To switch layouts based on the device type, disable the default layout in definePageMeta and use the $device helper within a <NuxtLayout> component.
<template>
<NuxtLayout :name="$device.isMobile ? 'mobile' : 'default'">
<!-- page content -->
</NuxtLayout>
</template>
<script setup>
definePageMeta({
layout: false
})
</script>You can access device information in two ways:
$device helper directly.useDevice() composable to destructure specific properties.<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>
<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>The defaultUserAgent option allows you to set a fallback user-agent header. This is particularly useful during static site generation (npm run generate) to ensure the device detection logic has a consistent user agent to parse when no real browser header is present.
Defaults to: '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'
export interface ModuleOptions {
defaultUserAgent?: string
}The module provides several flags via the $device helper to detect device types, operating systems, browsers, and crawlers. It also injects the raw userAgent string.
$device.isDesktop$device.isMobile$device.isTablet$device.isMobileOrTablet$device.isDesktopOrTablet$device.isIos$device.isLinux$device.isWindows$device.isMacOS$device.isApple$device.isAndroid$device.isSamsung$device.isFirefox$device.isEdge$device.isChrome$device.isSafari$device.isCrawler (powered by crawler-user-agents)$device.userAgent (the raw user agent string)The @nuxtjs/device module injects the $device object into your application. You can access it via the Nuxt app instance or directly within Vue components using the following interfaces:
nuxtApp.$device.this.$device (or via injection/provide patterns).// Accessing via NuxtApp
const { $device } = useNuxtApp()
if ($device.isMobile) { /* ... */ }
// Accessing in a Vue component
export default {
mounted() {
console.log(this.$device.isDesktop)
}
}The Device type defines the structure of the device detection object available in your Nuxt application. It contains boolean flags for detecting operating systems, browsers, device categories, and more, as well as the raw userAgent string.
export type Device = {
userAgent: string
isDesktop: boolean
isIos: boolean
isAndroid: boolean
isMobile: boolean
isMobileOrTablet: boolean
isDesktopOrTablet: boolean
isTablet: boolean
isLinux: boolean
isWindows: boolean
isMacOS: boolean
isApple: boolean
isSafari: boolean
isFirefox: boolean
isEdge: boolean
isChrome: boolean
isSamsung: boolean
isCrawler: boolean
}Use the useDevice composable within your Nuxt components (e.g., in <script setup>) to access the device detection object. This object provides information about the current user's device, such as whether they are on a mobile, tablet, or desktop platform.
The composable returns an object conforming to the Device type, which is attached to the Nuxt app instance as $device.
<script setup>
const device = useDevice();
if (device.isMobile) {
// Handle mobile specific logic
}
</script>