Skip to content

Commit d89af16

Browse files
test using file
1 parent ef44bb6 commit d89af16

2 files changed

Lines changed: 41 additions & 20 deletions

File tree

src/tasks/cwv-demo-suggestions-processor/handler.js

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
*/
1212

1313
import { isNonEmptyArray } from '@adobe/spacecat-shared-utils';
14+
15+
import fs from 'fs';
16+
import path from 'path';
17+
import { fileURLToPath } from 'url';
1418
import { say } from '../../utils/slack-utils.js';
1519

1620
const TASK_TYPE = 'cwv-demo-suggestions-processor';
@@ -20,24 +24,8 @@ const INP = 'inp';
2024
const DEMO = 'demo';
2125
const MAX_CWV_DEMO_SUGGESTIONS = 2;
2226

23-
// CWV reference suggestions defined as global variable to avoid file loading issues
24-
const cwvReferenceSuggestions = {
25-
lcp: [
26-
'## **Optimize Image Delivery:**\n• Use next-gen formats (WebP, AVIF)\n• Implement lazy loading\n• Compress images appropriately\n• Use responsive images with srcset\n• Consider image sprites for icons\n• Preload critical above-the-fold images',
27-
'## **Optimize CSS Loading:**\n• Inline critical CSS\n• Defer non-critical CSS\n• Use media queries conditionally\n• Minify CSS files\n• Consider CSS-in-JS for dynamic styling\n• Implement CSS containment',
28-
'## **Optimize Server & Network:**\n• Use CDN for global distribution\n• Enable GZIP/Brotli compression\n• Implement HTTP/2 or HTTP/3\n• Use edge caching strategies\n• Consider service workers\n• Use resource hints (preconnect, dns-prefetch)',
29-
],
30-
cls: [
31-
'## **Prevent Layout Shifts:**\n• Set explicit width/height on images/videos\n• Use aspect-ratio CSS property\n• Implement skeleton screens\n• Reserve space for dynamic content\n• Avoid inserting content above existing content\n• Use CSS Grid/Flexbox for predictable layouts',
32-
'## **Optimize Animations:**\n• Use CSS transforms and opacity\n• Implement will-change property\n• Use transform3d for stacking context\n• Avoid changing layout properties during animations\n• Use requestAnimationFrame for smooth animations\n• Consider CSS transitions over JavaScript animations',
33-
'## **Maintain Visual Stability:**\n• Implement skeleton screens\n• Reserve space for dynamic content\n• Use content-visibility CSS property\n• Implement CSS containment\n• Use CSS Grid and Flexbox\n• Avoid cumulative layout shifts',
34-
],
35-
inp: [
36-
'## **Break Up JavaScript Tasks:**\n• Use setTimeout or requestIdleCallback\n• Implement web workers for CPU-intensive operations\n• Use requestAnimationFrame for visual updates\n• Consider Intersection Observer API\n• Implement virtual scrolling for long lists\n• Use microtasks and macrotasks appropriately',
37-
'## **Optimize Event Handling:**\n• Debounce input events\n• Use passive event listeners\n• Implement event delegation\n• Use AbortController to cancel operations\n• Consider ResizeObserver and MutationObserver\n• Optimize event listener registration',
38-
'## **Optimize DOM Manipulation:**\n• Implement virtual scrolling\n• Use DocumentFragment for batch updates\n• Implement object pooling\n• Use Web Components for encapsulation\n• Implement progressive enhancement\n• Minimize DOM queries and mutations',
39-
],
40-
};
27+
const filename = fileURLToPath(import.meta.url);
28+
const dirname = path.dirname(filename);
4129

4230
/**
4331
* CWV thresholds for determining if metrics have issues
@@ -86,7 +74,7 @@ function hasExistingIssues(suggestion) {
8674
* @param {string} issueType - The type of issue (lcp, cls, inp)
8775
* @returns {string|null} A random suggestion or null if none available
8876
*/
89-
function getRandomSuggestion(issueType) {
77+
function getRandomSuggestion(issueType, cwvReferenceSuggestions) {
9078
const suggestions = cwvReferenceSuggestions[issueType];
9179
if (!isNonEmptyArray(suggestions)) {
9280
return null;
@@ -109,9 +97,23 @@ async function updateSuggestionWithGenericIssues(
10997
suggestion,
11098
metricIssues,
11199
logger,
100+
env,
101+
slackContext,
112102
) {
113103
let issuesAdded = 0;
114104

105+
let cwvReferenceSuggestions = { lcp: [], cls: [], inp: [] };
106+
try {
107+
const jsonPath = path.resolve(dirname, '../../../static/aem-best-practices.json');
108+
logger.info(`Loading CWV reference suggestions from: ${filename}, ${dirname}, ${jsonPath}`);
109+
const rawData = fs.readFileSync(jsonPath, 'utf-8');
110+
cwvReferenceSuggestions = JSON.parse(rawData);
111+
} catch (error) {
112+
logger.error(`Error loading CWV reference suggestions: ${error.message}`);
113+
await say(env, logger, slackContext, `Failed to load CWV reference suggestions: ${error.message}`);
114+
return 0;
115+
}
116+
115117
try {
116118
const data = suggestion.getData();
117119

@@ -120,7 +122,7 @@ async function updateSuggestionWithGenericIssues(
120122
}
121123

122124
for (const issueType of metricIssues) {
123-
const randomSuggestion = getRandomSuggestion(issueType);
125+
const randomSuggestion = getRandomSuggestion(issueType, cwvReferenceSuggestions);
124126
if (randomSuggestion) {
125127
const genericIssue = {
126128
type: issueType,
@@ -204,6 +206,8 @@ async function processCWVOpportunity(opportunity, logger, env, slackContext) {
204206
suggestion,
205207
metricIssues,
206208
logger,
209+
env,
210+
slackContext,
207211
);
208212
return issuesAdded;
209213
});

static/aem-best-practices.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"lcp": [
3+
"## **Optimize Image Delivery:**\n• Use next-gen formats (WebP, AVIF)\n• Implement lazy loading\n• Compress images appropriately\n• Use responsive images with srcset\n• Consider image sprites for icons\n• Preload critical above-the-fold images",
4+
"## **Optimize CSS Loading:**\n• Inline critical CSS\n• Defer non-critical CSS\n• Use media queries conditionally\n• Minify CSS files\n• Consider CSS-in-JS for dynamic styling\n• Implement CSS containment",
5+
"## **Optimize Server & Network:**\n• Use CDN for global distribution\n• Enable GZIP/Brotli compression\n• Implement HTTP/2 or HTTP/3\n• Use edge caching strategies\n• Consider service workers\n• Use resource hints (preconnect, dns-prefetch)"
6+
],
7+
"cls": [
8+
"## **Prevent Layout Shifts:**\n• Set explicit width/height on images/videos\n• Use aspect-ratio CSS property\n• Implement skeleton screens\n• Reserve space for dynamic content\n• Avoid inserting content above existing content\n• Use CSS Grid/Flexbox for predictable layouts",
9+
"## **Optimize Animations:**\n• Use CSS transforms and opacity\n• Implement will-change property\n• Use transform3d for stacking context\n• Avoid changing layout properties during animations\n• Use requestAnimationFrame for smooth animations\n• Consider CSS transitions over JavaScript animations",
10+
"## **Maintain Visual Stability:**\n• Implement skeleton screens\n• Reserve space for dynamic content\n• Use content-visibility CSS property\n• Implement CSS containment\n• Use CSS Grid and Flexbox\n• Avoid cumulative layout shifts"
11+
],
12+
"inp": [
13+
"## **Break Up JavaScript Tasks:**\n• Use setTimeout or requestIdleCallback\n• Implement web workers for CPU-intensive operations\n• Use requestAnimationFrame for visual updates\n• Consider Intersection Observer API\n• Implement virtual scrolling for long lists\n• Use microtasks and macrotasks appropriately",
14+
"## **Optimize Event Handling:**\n• Debounce input events\n• Use passive event listeners\n• Implement event delegation\n• Use AbortController to cancel operations\n• Consider ResizeObserver and MutationObserver\n• Optimize event listener registration",
15+
"## **Optimize DOM Manipulation:**\n• Implement virtual scrolling\n• Use DocumentFragment for batch updates\n• Implement object pooling\n• Use Web Components for encapsulation\n• Implement progressive enhancement\n• Minimize DOM queries and mutations"
16+
]
17+
}

0 commit comments

Comments
 (0)