Skip to content

fix: detect CloudFront device headers without relying on user-agent - #314

Open
luc122c wants to merge 2 commits into
nuxt-modules:mainfrom
luc122c:fix/cloudfront-header-detection
Open

luc122c wants to merge 2 commits into
nuxt-modules:mainfrom
luc122c:fix/cloudfront-header-detection

Conversation

@luc122c

@luc122c luc122c commented Oct 10, 2026

Copy link
Copy Markdown
Contributor

Description

Closes #141.

CloudFront's device detection headers (CloudFront-Is-*-Viewer) were only honored when the incoming User-Agent was exactly Amazon CloudFront:

if (userAgent === 'Amazon CloudFront') { ... }

Per the AWS docs, CloudFront only replaces the viewer's User-Agent with Amazon CloudFront when the header is not part of the cache/origin request policy. When User-Agent is forwarded (e.g. with an AllViewer policy), the origin sees the real browser user agent, the check fails, and the module silently ignores the CloudFront device headers in favor of user-agent parsing.

This change detects the CloudFront device headers by presence instead of keying off the user agent, so detection is configuration-independent. The headers still take precedence over user-agent parsing, and the existing mobile/tablet/desktop precedence is preserved.

Changes

  • src/runtime/generateFlags.ts: detect CloudFront via the presence of any CloudFront-Is-*-Viewer header rather than the User-Agent.
  • test/basic.test.ts: exercise the CloudFront headers with a real browser user agent (desktop UA for mobile/tablet/ios/android, iPhone UA for desktop) so the header precedence is actually covered, plus keep a case for the replaced Amazon CloudFront UA and a negative case with no headers.
  • README.md: clarify that detection no longer requires the Amazon CloudFront user agent, and that platform/browser flags are unavailable only when CloudFront replaces the user agent.

Backward compatibility

Both configurations are covered:

  • User-Agent: Amazon CloudFront + device headers (Unchanged)
  • Real browser User-Agent + device headers (Now detected)
  • No CloudFront headers (Falls back to user-agent parsing, as before)

Verification

pnpm dev:prepare
pnpm lint
pnpm test   # 28 passed

CloudFront only replaces the viewer's `User-Agent` with `Amazon CloudFront`
when the header is not part of the cache/origin request policy. When it is
forwarded (e.g. with an `AllViewer` policy), the module ignored the
`CloudFront-Is-*-Viewer` headers and fell back to user-agent parsing.

Detect the CloudFront device headers by presence instead, so detection works
regardless of the user agent. The headers are still prioritized over
user-agent parsing and the existing mobile/tablet/desktop precedence is
preserved.

Closes nuxt-modules#141
…er-detection

# Conflicts:
#	test/basic.test.ts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cloudfront detection shouldn't rely on user-agent

1 participant