refactor: Remove debug logging

This commit is contained in:
Emil Neander 2025-03-06 19:43:43 +01:00
parent ad7c61a019
commit d8355d6082

View File

@ -171,16 +171,10 @@ const extractAIOptimizedData = (
}; };
// Enhanced LCP element detection // Enhanced LCP element detection
console.log("DEBUG: Attempting to find LCP element information");
// 1. Try from largest-contentful-paint-element audit // 1. Try from largest-contentful-paint-element audit
if (lcpElement && lcpElement.details) { if (lcpElement && lcpElement.details) {
console.log("DEBUG: Found LCP element audit with details");
const lcpDetails = lcpElement.details as any; const lcpDetails = lcpElement.details as any;
console.log(
"DEBUG: LCP element details:",
JSON.stringify(lcpDetails).substring(0, 500)
);
// First attempt - try to get directly from items // First attempt - try to get directly from items
if ( if (
@ -189,26 +183,14 @@ const extractAIOptimizedData = (
lcpDetails.items.length > 0 lcpDetails.items.length > 0
) { ) {
const item = lcpDetails.items[0]; const item = lcpDetails.items[0];
console.log(
"DEBUG: Found LCP element item:",
JSON.stringify(item).substring(0, 500)
);
// For text elements in tables format // For text elements in tables format
if (item.type === "table" && item.items && item.items.length > 0) { if (item.type === "table" && item.items && item.items.length > 0) {
const firstTableItem = item.items[0]; const firstTableItem = item.items[0];
console.log(
"DEBUG: Found table format item:",
JSON.stringify(firstTableItem).substring(0, 500)
);
if (firstTableItem.node) { if (firstTableItem.node) {
if (firstTableItem.node.selector) { if (firstTableItem.node.selector) {
metric.element_selector = firstTableItem.node.selector; metric.element_selector = firstTableItem.node.selector;
console.log(
"DEBUG: Found LCP selector from table:",
metric.element_selector
);
} }
// Determine element type based on path or selector // Determine element type based on path or selector
@ -216,7 +198,6 @@ const extractAIOptimizedData = (
const selector = firstTableItem.node.selector || ""; const selector = firstTableItem.node.selector || "";
if (path) { if (path) {
console.log("DEBUG: Element path:", path);
if ( if (
selector.includes(" > img") || selector.includes(" > img") ||
selector.includes(" img") || selector.includes(" img") ||
@ -224,18 +205,11 @@ const extractAIOptimizedData = (
path.includes(",IMG") path.includes(",IMG")
) { ) {
metric.element_type = "image"; metric.element_type = "image";
console.log(
"DEBUG: Element type set to image based on path/selector"
);
// Try to extract image name from selector // Try to extract image name from selector
const imgMatch = selector.match(/img[.][^> ]+/); const imgMatch = selector.match(/img[.][^> ]+/);
if (imgMatch && !metric.element_url) { if (imgMatch && !metric.element_url) {
metric.element_url = imgMatch[0]; metric.element_url = imgMatch[0];
console.log(
"DEBUG: Extracted image class name as URL fallback:",
metric.element_url
);
} }
} else if ( } else if (
path.includes(",SPAN") || path.includes(",SPAN") ||
@ -243,13 +217,11 @@ const extractAIOptimizedData = (
path.includes(",H") path.includes(",H")
) { ) {
metric.element_type = "text"; metric.element_type = "text";
console.log("DEBUG: Element type set to text based on path");
} }
} }
// Try to extract text content if available // Try to extract text content if available
if (firstTableItem.node.nodeLabel) { if (firstTableItem.node.nodeLabel) {
console.log("DEBUG: Node label:", firstTableItem.node.nodeLabel);
metric.element_content = firstTableItem.node.nodeLabel.substring( metric.element_content = firstTableItem.node.nodeLabel.substring(
0, 0,
100 100
@ -259,7 +231,6 @@ const extractAIOptimizedData = (
} }
// Original handling for direct items // Original handling for direct items
else if (item.node?.nodeLabel) { else if (item.node?.nodeLabel) {
console.log("DEBUG: LCP element node label:", item.node.nodeLabel);
// Determine element type from node label // Determine element type from node label
if (item.node.nodeLabel.startsWith("<img")) { if (item.node.nodeLabel.startsWith("<img")) {
metric.element_type = "image"; metric.element_type = "image";
@ -267,10 +238,6 @@ const extractAIOptimizedData = (
const match = item.node.snippet?.match(/src="([^"]+)"/); const match = item.node.snippet?.match(/src="([^"]+)"/);
if (match && match[1]) { if (match && match[1]) {
metric.element_url = match[1]; metric.element_url = match[1];
console.log(
"DEBUG: Found LCP image URL from node label:",
metric.element_url
);
} }
} else if (item.node.nodeLabel.startsWith("<video")) { } else if (item.node.nodeLabel.startsWith("<video")) {
metric.element_type = "video"; metric.element_type = "video";
@ -282,21 +249,14 @@ const extractAIOptimizedData = (
if (item.node?.selector) { if (item.node?.selector) {
metric.element_selector = item.node.selector; metric.element_selector = item.node.selector;
console.log(
"DEBUG: Found LCP element selector:",
metric.element_selector
);
} }
} }
} }
} else {
console.log("DEBUG: No LCP element audit with details found");
} }
// 2. Try from lcp-lazy-loaded audit // 2. Try from lcp-lazy-loaded audit
const lcpImageAudit = audits["lcp-lazy-loaded"]; const lcpImageAudit = audits["lcp-lazy-loaded"];
if (lcpImageAudit && lcpImageAudit.details) { if (lcpImageAudit && lcpImageAudit.details) {
console.log("DEBUG: Found LCP lazy-loaded audit with details");
const lcpImageDetails = lcpImageAudit.details as any; const lcpImageDetails = lcpImageAudit.details as any;
if ( if (
@ -305,40 +265,20 @@ const extractAIOptimizedData = (
lcpImageDetails.items.length > 0 lcpImageDetails.items.length > 0
) { ) {
const item = lcpImageDetails.items[0]; const item = lcpImageDetails.items[0];
console.log(
"DEBUG: LCP lazy-loaded item:",
JSON.stringify(item).substring(0, 500)
);
if (item.url) { if (item.url) {
metric.element_type = "image"; metric.element_type = "image";
metric.element_url = item.url; metric.element_url = item.url;
console.log(
"DEBUG: Found LCP image URL from lazy-loaded:",
metric.element_url
);
} }
} }
} else {
console.log("DEBUG: No LCP lazy-loaded audit with details found");
} }
// 3. Try directly from the LCP audit details // 3. Try directly from the LCP audit details
if (!metric.element_url && lcp.details) { if (!metric.element_url && lcp.details) {
console.log("DEBUG: Trying to extract from LCP audit details directly");
const lcpDirectDetails = lcp.details as any; const lcpDirectDetails = lcp.details as any;
console.log(
"DEBUG: LCP audit details:",
JSON.stringify(lcpDirectDetails).substring(0, 500)
);
if (lcpDirectDetails.items && Array.isArray(lcpDirectDetails.items)) { if (lcpDirectDetails.items && Array.isArray(lcpDirectDetails.items)) {
for (const item of lcpDirectDetails.items) { for (const item of lcpDirectDetails.items) {
console.log(
"DEBUG: LCP direct item:",
JSON.stringify(item).substring(0, 500)
);
if (item.url || (item.node && item.node.path)) { if (item.url || (item.node && item.node.path)) {
if (item.url) { if (item.url) {
metric.element_url = item.url; metric.element_url = item.url;
@ -347,17 +287,9 @@ const extractAIOptimizedData = (
) )
? "image" ? "image"
: "resource"; : "resource";
console.log(
"DEBUG: Found LCP URL from direct details:",
metric.element_url
);
} }
if (item.node && item.node.selector) { if (item.node && item.node.selector) {
metric.element_selector = item.node.selector; metric.element_selector = item.node.selector;
console.log(
"DEBUG: Found LCP selector from direct details:",
metric.element_selector
);
} }
break; break;
} }
@ -368,7 +300,6 @@ const extractAIOptimizedData = (
// 4. Check for specific audit that might contain image info // 4. Check for specific audit that might contain image info
const largestImageAudit = audits["largest-image-paint"]; const largestImageAudit = audits["largest-image-paint"];
if (largestImageAudit && largestImageAudit.details) { if (largestImageAudit && largestImageAudit.details) {
console.log("DEBUG: Found largest-image-paint audit");
const imageDetails = largestImageAudit.details as any; const imageDetails = largestImageAudit.details as any;
if ( if (
@ -377,26 +308,17 @@ const extractAIOptimizedData = (
imageDetails.items.length > 0 imageDetails.items.length > 0
) { ) {
const item = imageDetails.items[0]; const item = imageDetails.items[0];
console.log(
"DEBUG: Largest image item:",
JSON.stringify(item).substring(0, 500)
);
if (item.url) { if (item.url) {
// If we have a large image that's close in time to LCP, it's likely the LCP element // If we have a large image that's close in time to LCP, it's likely the LCP element
metric.element_type = "image"; metric.element_type = "image";
metric.element_url = item.url; metric.element_url = item.url;
console.log(
"DEBUG: Found image URL from largest-image-paint:",
metric.element_url
);
} }
} }
} }
// 5. Check for network requests audit to find image resources // 5. Check for network requests audit to find image resources
if (!metric.element_url) { if (!metric.element_url) {
console.log("DEBUG: Checking network requests for potential LCP images");
const networkRequests = audits["network-requests"]; const networkRequests = audits["network-requests"];
if (networkRequests && networkRequests.details) { if (networkRequests && networkRequests.details) {
@ -421,10 +343,6 @@ const extractAIOptimizedData = (
if (imageResources.length > 0) { if (imageResources.length > 0) {
const closestImage = imageResources[0]; const closestImage = imageResources[0];
console.log(
"DEBUG: Found potential LCP image from network:",
closestImage.url
);
if (!metric.element_type) { if (!metric.element_type) {
metric.element_type = "image"; metric.element_type = "image";
@ -435,13 +353,6 @@ const extractAIOptimizedData = (
} }
} }
console.log(
"DEBUG: Final LCP element info:",
metric.element_type || "unknown type",
metric.element_url || "no URL",
metric.element_selector || "no selector"
);
metrics.push(metric); metrics.push(metric);
} }