Skip to content

Commit f233726

Browse files
use suggestions with in js file
1 parent d9ef7b4 commit f233726

1 file changed

Lines changed: 20 additions & 25 deletions

File tree

  • src/tasks/cwv-demo-suggestions-processor

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

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

1313
import { isNonEmptyArray } from '@adobe/spacecat-shared-utils';
14-
import { readFileSync } from 'fs';
15-
import { join } from 'path';
16-
import { fileURLToPath } from 'url';
1714
import { say } from '../../utils/slack-utils.js';
1815

1916
const TASK_TYPE = 'cwv-demo-suggestions-processor';
@@ -23,6 +20,25 @@ const INP = 'inp';
2320
const DEMO = 'demo';
2421
const MAX_CWV_DEMO_SUGGESTIONS = 2;
2522

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+
};
41+
2642
/**
2743
* CWV thresholds for determining if metrics have issues
2844
*/
@@ -68,10 +84,9 @@ function hasExistingIssues(suggestion) {
6884
/**
6985
* Gets a random suggestion from the available suggestions for a given issue type
7086
* @param {string} issueType - The type of issue (lcp, cls, inp)
71-
* @param {object} cwvReferenceSuggestions - The CWV reference suggestions object
7287
* @returns {string|null} A random suggestion or null if none available
7388
*/
74-
function getRandomSuggestion(issueType, cwvReferenceSuggestions) {
89+
function getRandomSuggestion(issueType) {
7590
const suggestions = cwvReferenceSuggestions[issueType];
7691
if (!isNonEmptyArray(suggestions)) {
7792
return null;
@@ -94,25 +109,7 @@ async function updateSuggestionWithGenericIssues(
94109
suggestion,
95110
metricIssues,
96111
logger,
97-
env,
98-
slackContext,
99112
) {
100-
let cwvReferenceSuggestions = {};
101-
102-
try {
103-
const filename = fileURLToPath(import.meta.url);
104-
logger.info('Debug - filename:', filename);
105-
const CWV_SUGGESTIONS_FILE_PATH = join(filename, 'aem-best-practices.json');
106-
logger.info(`Loading CWV suggestions from: ${CWV_SUGGESTIONS_FILE_PATH}`);
107-
108-
const jsonContent = readFileSync(CWV_SUGGESTIONS_FILE_PATH, 'utf8');
109-
cwvReferenceSuggestions = JSON.parse(jsonContent);
110-
logger.info(`Successfully loaded CWV suggestions with keys: ${Object.keys(cwvReferenceSuggestions).join(', ')}`);
111-
} catch (error) {
112-
logger.warn(`Failed to load CWV reference suggestions: ${error.message}`);
113-
await say(env, logger, slackContext, `Failed to load CWV reference suggestions: ${error.message}`);
114-
}
115-
116113
let issuesAdded = 0;
117114

118115
try {
@@ -207,8 +204,6 @@ async function processCWVOpportunity(opportunity, logger, env, slackContext) {
207204
suggestion,
208205
metricIssues,
209206
logger,
210-
env,
211-
slackContext,
212207
);
213208
return issuesAdded;
214209
});

0 commit comments

Comments
 (0)