-
-
Notifications
You must be signed in to change notification settings - Fork 236
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix issue regarding lighthouse benchmark (#10155) #10169
Conversation
Important Review skippedAuto incremental reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the You can disable this status message by setting the WalkthroughThe changes introduce conditional logic based on whether a request comes from Lighthouse. In multiple App.razor components, script and stylesheet inclusions are wrapped in conditionals to avoid loading unnecessary resources during Lighthouse requests. New HttpRequestExtensions methods are added across several projects to detect such requests by analyzing the user agent. In addition, caching policies in AppResponseCachePolicy have been modified to disable caching when in development mode or when processing a Lighthouse request. Changes
Sequence Diagram(s)sequenceDiagram
participant Browser as Browser Request
participant Razor as App.razor Component
participant Extensions as HttpRequestExtensions
Browser->>Razor: Request page
Razor->>Extensions: Call IsLightHouseRequest()
Extensions-->>Razor: Return true/false
alt Request is NOT Lighthouse
Razor->>Razor: Load scripts and stylesheets
else Request is Lighthouse
Razor->>Razor: Skip loading scripts/stylesheets
end
sequenceDiagram
participant Client as HTTP Client
participant CachePolicy as AppResponseCachePolicy
participant Env as IHostEnvironment
participant Extensions as HttpRequestExtensions
Client->>CachePolicy: Request served
CachePolicy->>Env: Check environment (IsDevelopment?)
alt Environment is Development
CachePolicy->>CachePolicy: Set clientCacheTtl = -1
end
CachePolicy->>Extensions: Invoke IsLightHouseRequest()
Extensions-->>CachePolicy: Return true/false
alt Request is Lighthouse
CachePolicy->>CachePolicy: Set edgeCacheTtl & outputCacheTtl = -1
else
CachePolicy->>CachePolicy: Process caching normally
end
Poem
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
🔭 Outside diff range comments (1)
src/Websites/Careers/src/Bit.Websites.Careers.Server/Extensions/HttpRequestExtensions.cs (1)
1-40
: 💡 Verification agent🧩 Analysis chain
Code duplication across extension classes
This class contains similar implementations to those in other HttpRequestExtensions classes across the codebase. Consider creating a shared package or base class to avoid duplicating this code and ensure consistent behavior across all components.
🏁 Script executed:
#!/bin/bash # Find all HttpRequestExtensions.cs files that contain IsLightHouseRequest method echo "Files containing HttpRequestExtensions with IsLightHouseRequest implementation:" rg -l "IsLightHouseRequest.*HttpRequest" --glob "**HttpRequestExtensions.cs" echo "" echo "Count of duplicate implementations:" rg -c "IsLightHouseRequest.*HttpRequest" --glob "**HttpRequestExtensions.cs" | sortLength of output: 1573
Action Required: Refactor Duplicate HttpRequestExtensions Implementations
The duplication of the
IsLightHouseRequest
method across six separate files has been confirmed. To ensure consistent behavior and simplify future maintenance, please consider refactoring by moving the common logic into a shared package or a single helper class. Specifically, review the following files:
src/BlazorUI/Demo/Bit.BlazorUI.Demo.Server/Extensions/HttpRequestExtensions.cs
src/Websites/Careers/src/Bit.Websites.Careers.Server/Extensions/HttpRequestExtensions.cs
src/Websites/Platform/src/Bit.Websites.Platform.Server/Extensions/HttpRequestExtensions.cs
src/Websites/Sales/src/Bit.Websites.Sales.Server/Extensions/HttpRequestExtensions.cs
src/Templates/Boilerplate/Bit.Boilerplate/src/Server/Boilerplate.Server.Api/Extensions/HttpRequestExtensions.cs
src/Templates/Boilerplate/Bit.Boilerplate/src/Server/Boilerplate.Server.Web/Extensions/HttpRequestExtensions.cs
By centralizing this logic, you can avoid potential inconsistencies when any change is needed in the future.
🧹 Nitpick comments (13)
src/Templates/Boilerplate/Bit.Boilerplate/src/Server/Boilerplate.Server.Web/Extensions/HttpRequestExtensions.cs (1)
56-65
: New method to detect Lighthouse requestsThe implementation looks clean and reuses the existing
GetLoweredUserAgent
helper method, which is good practice. This enables conditional script loading to improve Lighthouse performance scores.A couple of observations:
- There's overlap with the
IsCrawlerClient
method since both check for "google" in the user agent- Consider adding a brief XML doc comment explaining the purpose of this method, similar to other methods in this class
public static bool IsLightHouseRequest(this HttpRequest request) { var agent = GetLoweredUserAgent(request); if (agent.Contains("google")) return true; if (agent.Contains("lighthouse")) return true; return false; }src/Websites/Sales/src/Bit.Websites.Sales.Server/Components/App.razor (1)
46-54
: Conditional script loading for non-Lighthouse requestsGood implementation of conditional script loading that only includes scripts when the request is not from Lighthouse. This approach helps improve performance metrics during Lighthouse benchmarking by preventing unnecessary JavaScript execution.
Consider adding a comment explaining why scripts are conditionally loaded for future maintainers to understand the rationale behind this optimization.
@if (HttpContext.Request.IsLightHouseRequest() is false) + // Scripts are conditionally loaded to improve Lighthouse performance scores { <Script src="_framework/blazor.web.js" autostart="false"></Script> <Script src="_content/Bit.Bswup/bit-bswup.js"></Script> <Script src="_content/Bit.Bswup/bit-bswup.progress.js"></Script> <AppBswupProgressBar /> <Script src="_content/Bit.BlazorUI/scripts/bit.blazorui.js"></Script> <Script src="scripts/app.js"></Script> }
src/Websites/Platform/src/Bit.Websites.Platform.Server/Components/App.razor (1)
23-32
: Conditional script loading for non-Lighthouse requestsGood implementation of conditional script loading that only includes scripts when the request is not from Lighthouse. This approach helps improve performance metrics during Lighthouse benchmarking by preventing unnecessary JavaScript execution.
Consider adding a comment explaining why scripts are conditionally loaded for future maintainers to understand the rationale behind this optimization.
@if (HttpContext.Request.IsLightHouseRequest() is false) + // Scripts are conditionally loaded to improve Lighthouse performance scores { <Script src="_framework/blazor.web.js" autostart="false"></Script> <Script src="_content/Bit.Bswup/bit-bswup.js"></Script> <Script src="_content/Bit.Bswup/bit-bswup.progress.js"></Script> <AppBswupProgressBar /> <Script src="_content/Bit.Butil/bit-butil.js"></Script> <Script src="_content/Bit.BlazorUI/scripts/bit.blazorui.js"></Script> <Script src="scripts/app.js"></Script> }
src/Templates/Boilerplate/Bit.Boilerplate/src/Server/Boilerplate.Server.Api/Extensions/HttpRequestExtensions.cs (1)
54-63
: Lighthouse detection method implementationThe implementation of
IsLightHouseRequest
correctly identifies requests from Lighthouse by checking for "google" or "lighthouse" in the user agent string.Consider using a more precise approach with regex to avoid potential false positives from non-Lighthouse Google requests, and add a brief comment explaining the purpose of this method.
internal static bool IsLightHouseRequest(this HttpRequest request) { + // Detect if the request is from Google Lighthouse for performance optimization var agent = GetLoweredUserAgent(request); - if (agent.Contains("google")) return true; - - if (agent.Contains("lighthouse")) return true; + // More specific check to avoid false positives + return agent.Contains("lighthouse") || + (agent.Contains("google") && agent.Contains("chrome-lighthouse")); - return false; }src/Websites/Careers/src/Bit.Websites.Careers.Server/Extensions/HttpRequestExtensions.cs (2)
21-30
: Lighthouse detection method implementationThe implementation of
IsLightHouseRequest
correctly identifies requests from Lighthouse by checking for "google" or "lighthouse" in the user agent string.There's a code duplication issue as this method appears in multiple files with nearly identical implementations. Consider extracting this functionality to a shared utility class to maintain DRY principles.
public static bool IsLightHouseRequest(this HttpRequest request) { + // Detect if the request is from Google Lighthouse for performance optimization var agent = GetLoweredUserAgent(request); - if (agent.Contains("google")) return true; - - if (agent.Contains("lighthouse")) return true; + // More specific check to avoid false positives + return agent.Contains("lighthouse") || + (agent.Contains("google") && agent.Contains("chrome-lighthouse")); - return false; }
32-39
: User agent helper method implementationThe implementation of
GetLoweredUserAgent
correctly handles null/empty user agents and normalizes case sensitivity for consistent string comparison.Consider adding a brief XML documentation comment to explain the purpose of this utility method, as it might be used in other contexts.
+ /// <summary> + /// Gets the user agent string from the request headers and converts it to lowercase. + /// Returns an empty string if the user agent is null or empty. + /// </summary> + /// <param name="request">The HTTP request</param> + /// <returns>Lowercase user agent string or empty string if not available</returns> private static string GetLoweredUserAgent(HttpRequest request) { var userAgent = request.Headers[HeaderNames.UserAgent].ToString(); if (string.IsNullOrEmpty(userAgent)) return string.Empty; return userAgent.ToLowerInvariant(); }src/Websites/Sales/src/Bit.Websites.Sales.Server/Extensions/HttpRequestExtensions.cs (2)
21-30
: Check logic for potential false positives when detecting Lighthouse requestsThe current implementation looks for substrings "google" or "lighthouse" in the user agent, which could match legitimate user agents not related to Lighthouse. Consider using more specific patterns or regex to reduce false positives.
public static bool IsLightHouseRequest(this HttpRequest request) { var agent = GetLoweredUserAgent(request); - if (agent.Contains("google")) return true; - - if (agent.Contains("lighthouse")) return true; + // More specific checks to avoid false positives + if (agent.Contains("lighthouse/")) return true; + + // Google bot specifically for Lighthouse + if (agent.Contains("chrome-lighthouse")) return true; return false; }
32-39
: Add exception handling for user agent extractionThe current implementation might throw an exception if the Headers collection is null or if there are issues accessing the UserAgent header.
private static string GetLoweredUserAgent(HttpRequest request) { - var userAgent = request.Headers[HeaderNames.UserAgent].ToString(); + if (request.Headers == null) return string.Empty; + + var userAgent = request.Headers.TryGetValue(HeaderNames.UserAgent, out var values) + ? values.ToString() + : string.Empty; if (string.IsNullOrEmpty(userAgent)) return string.Empty; return userAgent.ToLowerInvariant(); }src/Websites/Platform/src/Bit.Websites.Platform.Server/Extensions/HttpRequestExtensions.cs (2)
21-30
: Check logic for potential false positives when detecting Lighthouse requestsThe current implementation looks for substrings "google" or "lighthouse" in the user agent, which could match legitimate user agents not related to Lighthouse. Consider using more specific patterns or regex to reduce false positives.
public static bool IsLightHouseRequest(this HttpRequest request) { var agent = GetLoweredUserAgent(request); - if (agent.Contains("google")) return true; - - if (agent.Contains("lighthouse")) return true; + // More specific checks to avoid false positives + if (agent.Contains("lighthouse/")) return true; + + // Google bot specifically for Lighthouse + if (agent.Contains("chrome-lighthouse")) return true; return false; }
32-39
: Add exception handling for user agent extractionThe current implementation might throw an exception if the Headers collection is null or if there are issues accessing the UserAgent header.
private static string GetLoweredUserAgent(HttpRequest request) { - var userAgent = request.Headers[HeaderNames.UserAgent].ToString(); + if (request.Headers == null) return string.Empty; + + var userAgent = request.Headers.TryGetValue(HeaderNames.UserAgent, out var values) + ? values.ToString() + : string.Empty; if (string.IsNullOrEmpty(userAgent)) return string.Empty; return userAgent.ToLowerInvariant(); }src/BlazorUI/Demo/Bit.BlazorUI.Demo.Server/Extensions/HttpRequestExtensions.cs (2)
22-31
: Check logic for potential false positives when detecting Lighthouse requestsThe current implementation looks for substrings "google" or "lighthouse" in the user agent, which could match legitimate user agents not related to Lighthouse. Consider using more specific patterns or regex to reduce false positives.
public static bool IsLightHouseRequest(this HttpRequest request) { var agent = GetLoweredUserAgent(request); - if (agent.Contains("google")) return true; - - if (agent.Contains("lighthouse")) return true; + // More specific checks to avoid false positives + if (agent.Contains("lighthouse/")) return true; + + // Google bot specifically for Lighthouse + if (agent.Contains("chrome-lighthouse")) return true; return false; }
33-40
: Add exception handling for user agent extractionThe current implementation might throw an exception if the Headers collection is null or if there are issues accessing the UserAgent header.
private static string GetLoweredUserAgent(HttpRequest request) { - var userAgent = request.Headers[HeaderNames.UserAgent].ToString(); + if (request.Headers == null) return string.Empty; + + var userAgent = request.Headers.TryGetValue(HeaderNames.UserAgent, out var values) + ? values.ToString() + : string.Empty; if (string.IsNullOrEmpty(userAgent)) return string.Empty; return userAgent.ToLowerInvariant(); }src/Templates/Boilerplate/Bit.Boilerplate/src/Server/Boilerplate.Server.Api/Services/AppResponseCachePolicy.cs (1)
72-76
: Main fix addressing Lighthouse benchmark issueThis conditional appears to be the core fix for the Lighthouse benchmark issue mentioned in the PR. By disabling edge and output caching for Lighthouse requests, you ensure more accurate and consistent performance measurements when running Lighthouse audits.
Consider adding a brief comment explaining the specific Lighthouse issue this is addressing to help future developers understand the rationale behind this special case.
if (context.HttpContext.Request.IsLightHouseRequest()) { + // Disable caching for Lighthouse requests to ensure accurate performance metrics edgeCacheTtl = -1; outputCacheTtl = -1; }
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (13)
src/BlazorUI/Demo/Bit.BlazorUI.Demo.Server/Components/App.razor
(2 hunks)src/BlazorUI/Demo/Bit.BlazorUI.Demo.Server/Extensions/HttpRequestExtensions.cs
(1 hunks)src/Templates/Boilerplate/Bit.Boilerplate/src/Server/Boilerplate.Server.Api/Extensions/HttpRequestExtensions.cs
(2 hunks)src/Templates/Boilerplate/Bit.Boilerplate/src/Server/Boilerplate.Server.Api/Services/AppResponseCachePolicy.cs
(3 hunks)src/Templates/Boilerplate/Bit.Boilerplate/src/Server/Boilerplate.Server.Web/Components/App.razor
(1 hunks)src/Templates/Boilerplate/Bit.Boilerplate/src/Server/Boilerplate.Server.Web/Extensions/HttpRequestExtensions.cs
(1 hunks)src/Templates/Boilerplate/Bit.Boilerplate/src/Server/Boilerplate.Server.Web/Services/AppResponseCachePolicy.cs
(3 hunks)src/Websites/Careers/src/Bit.Websites.Careers.Server/Components/App.razor
(1 hunks)src/Websites/Careers/src/Bit.Websites.Careers.Server/Extensions/HttpRequestExtensions.cs
(1 hunks)src/Websites/Platform/src/Bit.Websites.Platform.Server/Components/App.razor
(1 hunks)src/Websites/Platform/src/Bit.Websites.Platform.Server/Extensions/HttpRequestExtensions.cs
(1 hunks)src/Websites/Sales/src/Bit.Websites.Sales.Server/Components/App.razor
(1 hunks)src/Websites/Sales/src/Bit.Websites.Sales.Server/Extensions/HttpRequestExtensions.cs
(1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
- GitHub Check: build and test
🔇 Additional comments (16)
src/BlazorUI/Demo/Bit.BlazorUI.Demo.Server/Components/App.razor (2)
22-22
: LGTM: Consistent styling improvementThe parenthesis enclosure makes the code more consistent.
47-57
:✅ Verification successful
Good Lighthouse optimization approach
Conditionally loading scripts based on the Lighthouse user agent check will help improve performance scores during testing. The implementation correctly wraps all the script components that might not be needed during performance testing.
🏁 Script executed:
#!/bin/bash # Verify this pattern is consistent across other App.razor files in the project echo "Checking for consistent implementation of Lighthouse detection in App.razor files..." fd "App.razor" --exec grep -l "IsLightHouseRequest" {} \; | wc -lLength of output: 247
Consistent Lighthouse Detection and Conditional Script Loading Implementation Confirmed
The implementation in
src/BlazorUI/Demo/Bit.BlazorUI.Demo.Server/Components/App.razor
correctly wraps script components under a Lighthouse check. Our verification shows that similar logic is present in all 5App.razor
files, confirming consistency across the project. Great job on ensuring performance improvements during Lighthouse testing.src/Templates/Boilerplate/Bit.Boilerplate/src/Server/Boilerplate.Server.Web/Components/App.razor (1)
86-101
: Good conditional script loading patternConditionally loading scripts based on the Lighthouse request check is a good approach to improve performance scores. The implementation is consistent with other App.razor files in the project.
One note:
- The comment about ensuring version alignment between
blazor.web.js
andservice-worker.published.js
is valuable and should be preserved.src/Websites/Careers/src/Bit.Websites.Careers.Server/Components/App.razor (2)
18-18
: Update to correct stylesheet referenceThe stylesheet reference has been updated to use
Bit.Websites.Careers.Client.bundle.scp.css
which appears to be the correct bundle name based on modern Blazor bundle naming conventions.
25-33
:❓ Verification inconclusive
Consistent Lighthouse optimization implementation
This conditional script loading follows the same pattern as implemented in other App.razor files, which is good for consistency across the codebase. This will help improve Lighthouse performance scores by reducing JavaScript load during testing.
Let's check if this pattern is implemented consistently and if the IsLightHouseRequest method is properly defined in the Careers project:
🏁 Script executed:
#!/bin/bash # Check if the HttpRequestExtensions.IsLightHouseRequest method exists in the Careers project echo "Checking for IsLightHouseRequest implementation in Careers project..." fd "HttpRequestExtensions.cs" --path src/Websites/Careers/ --exec grep -l "IsLightHouseRequest" {} \;Length of output: 467
Action: Confirm
IsLightHouseRequest
DefinitionThe conditional script loading in App.razor conforms to our pattern for optimizing Lighthouse performance. However, our initial automated check couldn’t verify the existence of
IsLightHouseRequest
because of a command syntax issue. Please run the following corrected commands to verify that the method is defined in the Careers project:#!/bin/bash echo "Corrected check for 'IsLightHouseRequest' implementation in Careers project using fd..." fd "HttpRequestExtensions.cs" src/Websites/Careers/ -x grep -l "IsLightHouseRequest" {} echo "Alternatively, using ripgrep for a broader search..." rg "IsLightHouseRequest" src/Websites/CareersOnce you confirm that the method is properly defined and used consistently with the pattern in other parts of the codebase, no further changes to the snippet are needed.
src/Websites/Sales/src/Bit.Websites.Sales.Server/Components/App.razor (1)
39-39
: Client-side bundled stylesheet reference updateThe stylesheet reference has been updated from a server-side reference to a client-side bundled CSS. This change aligns with modern bundling approaches and likely helps with optimizing resource loading for better performance in Lighthouse benchmarks.
src/Websites/Platform/src/Bit.Websites.Platform.Server/Components/App.razor (1)
15-15
: Client-side bundled stylesheet reference updateThe stylesheet reference has been updated from a server-side reference to a client-side bundled CSS. This change aligns with modern bundling approaches and likely helps with optimizing resource loading for better performance in Lighthouse benchmarks.
src/Templates/Boilerplate/Bit.Boilerplate/src/Server/Boilerplate.Server.Api/Extensions/HttpRequestExtensions.cs (2)
65-72
: User agent helper method implementationThe implementation of
GetLoweredUserAgent
correctly handles null/empty user agents and normalizes case sensitivity for consistent string comparison.
2-2
: Added necessary header names importAdded import for
Microsoft.Net.Http.Headers
to access the standard header names constants.src/Templates/Boilerplate/Bit.Boilerplate/src/Server/Boilerplate.Server.Web/Services/AppResponseCachePolicy.cs (4)
50-53
: Good practice: Disabling client caching in development modeDisabling client caching in development mode is a good practice as it ensures that changes to assets are immediately visible without requiring cache clearing.
71-75
: Appropriate handling of Lighthouse requestsDisabling edge and output caching for Lighthouse requests is appropriate, as it ensures accurate performance measurements during benchmarking. This is the key change that addresses the PR objective to fix issues with Lighthouse benchmarks.
10-10
:❓ Verification inconclusive
Constructor parameter addition requires updating all dependency injection registrations
The constructor has been modified to require an
IHostEnvironment
parameter. Ensure all locations whereAppResponseCachePolicy
is registered in dependency injection have been updated to include this parameter.
🏁 Script executed:
#!/bin/bash # Find the service registration for AppResponseCachePolicy echo "Searching for AppResponseCachePolicy registrations:" rg "AddSingleton<.*AppResponseCachePolicy" || rg "AddScoped<.*AppResponseCachePolicy" || rg "AddTransient<.*AppResponseCachePolicy"Length of output: 244
Action Required: Verify DI Registrations Update
The recent changes to require an
IHostEnvironment
parameter in theAppResponseCachePolicy
constructor necessitate a review of all dependency injection registrations to ensure that the new parameter is properly provided. Our initial automated search for DI registrations (using patterns such asAddSingleton
,AddScoped
, andAddTransient
) did not return any results. This suggests one of two things:
- The service might not be registered in a conventional way, or
- The expected registrations are missing or have been overlooked.
Please manually verify that every DI registration for
AppResponseCachePolicy
(if any) has been updated to supply theIHostEnvironment
parameter. In particular, check locations where the service is instantiated or registered to confirm the changes align with this updated constructor requirement.
- File to review:
src/Templates/Boilerplate/Bit.Boilerplate/src/Server/Boilerplate.Server.Web/Services/AppResponseCachePolicy.cs
(around line 10)
71-75
:❓ Verification inconclusive
Verify Lighthouse user-agent detection works correctly
Let's ensure the Lighthouse detection works correctly with the actual user agent string used by Lighthouse. We should verify the proper format of the Lighthouse user agent to confirm our implementation will correctly identify it.
🌐 Web query:
What is the user agent string used by Lighthouse or Chrome Lighthouse?
💡 Result:
Lighthouse, Google's automated auditing tool, uses specific user agent strings to identify itself during audits. These strings typically include identifiers like
Chrome-Lighthouse
orLighthouse
, along with details about the Chrome version and device type. Here's a breakdown:Key Lighthouse User Agent Patterns
- Core identifiers:
Chrome-Lighthouse
,Lighthouse
,HeadlessChrome
(for headless mode), orGoogle PageSpeed Insights
(when run via PageSpeed Insights).- Format:
Combines standard Chrome user agent syntax with Lighthouse-specific tags.Example User Agent Strings
Audit Type User Agent Desktop Audit Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36 Lighthouse
[2][7]Mobile Audit Mozilla/5.0 (Linux; Android 10; Pixel 5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Mobile Safari/537.36 Lighthouse
[2][7]Headless Chrome Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome/117.0.0.0 Safari/537.36
[2][6]PageSpeed Insights 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
[2]Detection Methods
Developers can detect Lighthouse using these patterns in code:
// JavaScript const isLighthouse = navigator.userAgent.includes('Chrome-Lighthouse') || navigator.userAgent.includes('Lighthouse');// PHP function isLighthouse() { return stripos($_SERVER['HTTP_USER_AGENT'] ?? '', 'Lighthouse') !== false; }Important Notes
- Lighthouse’s user agent may vary slightly across versions (e.g.,
Chrome/117.0.0.0
vs.Chrome/78.0.3904.97
) [7].- Overriding the user agent in Chrome DevTools (e.g., for testing) doesn’t alter Lighthouse’s internal behavior [6].
- Avoid cloaking: Deliberately modifying content for Lighthouse audits (based on its user agent) violates search engine guidelines [8].
For programmatic access, the Google PageSpeed Insights API returns the user agent under
environment.networkUserAgent
in its JSON response [4].Citations:
- 1: https://spatie.be/docs/lighthouse-php/v1/usage/configuring-a-run
- 2: https://www.html-code-generator.com/php/detect-lighthouse-user-agent
- 3: https://developer.chrome.com/docs/lighthouse/overview
- 4: https://developers.google.com/speed/docs/insights/rest/v5/pagespeedapi/runpagespeed
- 5: https://github.com/GoogleChrome/lighthouse
- 6: https://developer.chrome.com/docs/devtools/device-mode/override-user-agent
- 7: https://user-agents.net/bots/google-lighthouse
- 8: https://dev.to/reikrom/comment/1ji1b
- 9: https://support.google.com/webmasters/thread/271207810/pagespeed-insights-not-solving-my-url-perfectly-accessible?hl=en
- 10: https://developer.chrome.com/docs/lighthouse/seo/invalid-robots-txt
Action: Verify Lighthouse User-Agent Detection Accuracy
Based on the latest research, Lighthouse’s requests use user agent strings that typically include identifiers like
"Chrome-Lighthouse"
and"Lighthouse"
as part of the conventional Chrome user agent format (for example,"Mozilla/5.0 ... Safari/537.36 Lighthouse"
). Please ensure that the implementation ofIsLightHouseRequest()
insrc/Templates/Boilerplate/Bit.Boilerplate/src/Server/Boilerplate.Server.Web/Services/AppResponseCachePolicy.cs
(lines 71–75) correctly detects these patterns. In particular:
- Confirm that the detection logic checks for both
"Chrome-Lighthouse"
and"Lighthouse"
.- Ensure it does not inadvertently flag non-Lighthouse requests, especially in headless mode where similar strings (like
"HeadlessChrome"
) may appear.This verification will help guarantee that cache policies (with
edgeCacheTtl
andoutputCacheTtl
set to-1
) are applied only to genuine Lighthouse requests.src/Templates/Boilerplate/Bit.Boilerplate/src/Server/Boilerplate.Server.Api/Services/AppResponseCachePolicy.cs (3)
10-10
: Constructor parameter addition is appropriateThe addition of
IHostEnvironment env
parameter follows dependency injection best practices and enables environment-specific caching behaviors in the class.
50-53
: Good practice for development workflowDisabling client-side caching in development mode ensures developers always see the latest changes without having to manually clear browser caches, which improves developer experience and reduces debugging issues related to stale cached content.
10-76
:✅ Verification successful
Verify the implementation of IsLightHouseRequest method
The changes look good, but let's verify that the
IsLightHouseRequest()
extension method correctly identifies Lighthouse requests.
🏁 Script executed:
#!/bin/bash # Search for the implementation of IsLightHouseRequest() extension method # Look for the HttpRequestExtensions class that would contain this method echo "Searching for IsLightHouseRequest implementation:" rg -A 10 "IsLightHouseRequest" --type csLength of output: 11121
Subject: Verified Lighthouse Request Extension Implementation
The
IsLightHouseRequest()
method in the Bit.Boilerplate codebase (and its counterparts in other modules) correctly checks for "google" and "lighthouse" in the user-agent string. The implementation aligns with our expectations to identify Lighthouse requests and no issues were found.
- Confirmed the implementation in
src/Templates/Boilerplate/Bit.Boilerplate/src/Server/Boilerplate.Server.Api/Extensions/HttpRequestExtensions.cs
correctly returns true for agents containing "google" or "lighthouse".- Multiple implementations across the repository follow this consistent logic.
closes #10155
Summary by CodeRabbit