Detect Lighthouse User Agent in PHP and JavaScript
Detecting the Lighthouse user agent can be essential for optimizing your website performance during audits. In this guide, we provide simple PHP and JavaScript snippets to check if a request is coming from Lighthouse. Use these methods to adjust responses, track performance audits, or tailor content for automated tools like Lighthouse.
Detect Lighthouse User Agent Using PHP
The PHP code below demonstrates how to check if the incoming request is from a Lighthouse user agent. This is useful for handling responses specifically during audits, ensuring a smooth experience for tools performing performance evaluations.
<?php
function detectLighthouse(): bool {
// Define known patterns associated with Lighthouse or similar tools
const LIGHTHOUSE_PATTERNS = [
'chrome-lighthouse',
'google/lighthouse',
'lighthouse',
'pagespeed',
'pagespeedinsights',
'googleads-lighthouse',
'headless',
'webdriver'
];
// Retrieve the User-Agent string from the request
$userAgent = $_SERVER['HTTP_USER_AGENT'] ?? null;
// If User-Agent is not present, return false early
if (!$userAgent) {
return false;
}
// Check if any pattern exists in the User-Agent string
foreach (LIGHTHOUSE_PATTERNS as $pattern) {
if (stripos($userAgent, $pattern) !== false) {
return true; // Match found
}
}
return false; // No matches found
}
Usage Example:
You can use this function in your application to log requests or adjust your response headers for Lighthouse audits
Detect Lighthouse User Agent Using JavaScript
The following JavaScript snippet enables detection of Lighthouse in the user's browser. This can be used on the client-side for debugging or adapting content during audits.
const isLighthouse = () => {
// Check if navigator and userAgent are available
if (!navigator || !navigator.userAgent) {
return false;
}
// Normalize the user agent to lowercase for robust matching
const userAgent = navigator.userAgent.toLowerCase();
// Comprehensive array of Lighthouse-related user agent patterns
const lighthousePatterns = [
'chrome-lighthouse',
'google/lighthouse',
'lighthouse',
'pagespeed',
'pagespeedinsights',
'googleads-lighthouse',
'headless',
'webdriver'
];
// Use some() to check if any pattern exists in the user agent
return lighthousePatterns.some(pattern =>
userAgent.includes(pattern)
);
};
Example usage:
Common Lighthouse User Agents
Here are some examples of user agents that Lighthouse may use:
Desktop Audits:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36 Lighthouse
Mobile Audits:
Mozilla/5.0 (Linux; Android 10; Pixel 5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Mobile Safari/537.36 Lighthouse
Google PageSpeed Insights (Lighthouse-based):
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko; compatible; Google PageSpeed Insights) Chrome/117.0.0.0 Safari/537.36
Crawler User Agent (Headless Chrome):
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome/117.0.0.0 Safari/537.36
Key Identifiers in the User Agent
- "Lighthouse": This is a specific keyword indicating the tool being used.
- "HeadlessChrome": Indicates Chrome running in headless mode (without a graphical interface), often used by automated tools like Lighthouse.
- "Google PageSpeed Insights": Indicates the request is part of a PageSpeed Insights audit, which uses Lighthouse under the hood.
What is Lighthouse User Agent?
Lighthouse is an open-source tool by Google used to audit and improve web performance, accessibility, SEO, and more. When Lighthouse runs a test, it identifies itself using a specific user-agent string. Detecting this user agent allows developers to understand when their site is being analyzed, helping to tailor responses or optimize audits.
Use Cases for Detecting Lighthouse User Agents:
Detecting Lighthouse user agents can help in performance monitoring, debugging issues during audits, tailoring content for improved scores, and ensuring optimized responses for better analysis results. Here are some potential use cases for detecting Lighthouse user agent
- Optimize performance for better Lighthouse scores.
- Tailor user experience during audits.
- Debug and test website performance.
- Monitor Lighthouse audits for insights.
- Ensure security and compliance.
- Optimize content for audits.
- Improve SEO for Lighthouse.