File size: 7,207 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import { Metrics, PerformanceMetricsItemQueryResponse } from 'calypso/data/site-profiler/types';
import { Valuation } from '../types/performance-metrics';

export const getMetricsNames = ( translate: ( text: string ) => string ) => ( {
	fcp: { name: translate( 'First Contentful Paint' ) },
	lcp: { name: translate( 'Largest Contentful Paint' ) },
	cls: { name: translate( 'Cumulative Layout Shift' ) },
	inp: { name: translate( 'Interaction to Next Paint' ) },
	ttfb: { name: translate( 'Time to First Byte' ) },
	tbt: { name: translate( 'Total Blocking Time' ) },
	overall: { name: translate( 'Performance Score' ) },
} );

export const getMetricValuations = ( translate: ( text: string ) => string ) => ( {
	fcp: {
		good: translate( 'Your site‘s First Contentful Paint is excellent' ),
		needsImprovement: translate( 'Your site‘s First Contentful Paint needs improvement' ),
		bad: translate( 'Your site‘s First Contentful Paint is poor' ),
		heading: translate( 'What is First Contentful Paint?' ),
		aka: translate( '(FCP)' ),
		explanation: translate(
			'First Contentful Paint reflects the time it takes to display the first text or image to visitors. The best sites load in under 1.8 seconds.'
		),
		docsUrl:
			'https://developer.wordpress.com/docs/site-performance/speed-test/#first-contentful-paint-fcp-',
	},
	lcp: {
		good: translate( 'Your site‘s Largest Contentful Paint is excellent' ),
		needsImprovement: translate( 'Your site‘s Largest Contentful Paint needs improvement' ),
		bad: translate( 'Your site‘s Largest Contentful Paint is poor' ),
		heading: translate( 'What is Largest Contentful Paint?' ),
		aka: translate( '(LCP)' ),
		explanation: translate(
			'Largest Contentful Paint measures the time it takes for the largest visible element (like an image or text block) on a page to load. The best sites load in under 2.5 seconds.'
		),
		docsUrl:
			'https://developer.wordpress.com/docs/site-performance/speed-test/#largest-contentful-paint-lcp-',
	},
	cls: {
		good: translate( 'Your site‘s Cumulative Layout Shift is excellent' ),
		needsImprovement: translate( 'Your site‘s Cumulative Layout Shift needs improvement' ),
		bad: translate( 'Your site‘s Cumulative Layout Shift is poor' ),
		heading: translate( 'What is Cumulative Layout Shift?' ),
		aka: translate( '(CLS)' ),
		explanation: translate(
			'Cumulative Layout Shift is assessed by measuring how often content moves unexpectedly during loading. The best sites have a score of 0.1 or lower.'
		),
		docsUrl:
			'https://developer.wordpress.com/docs/site-performance/speed-test/#cumulative-layout-shift-cls-',
	},
	inp: {
		good: translate( 'Your site‘s Interaction to Next Paint is excellent' ),
		needsImprovement: translate( 'Your site‘s Interaction to Next Paint needs improvement' ),
		bad: translate( 'Your site‘s Interaction to Next Paint is poor' ),
		heading: translate( 'What is Interaction to Next Paint?' ),
		aka: translate( '(INP)' ),
		explanation: translate(
			'Interaction to Next Paint measures the overall responsiveness of a webpage by evaluating how quickly it reacts to user interactions. A good score is 200 milliseconds or less, indicating that the page responds swiftly to user inputs.'
		),
		docsUrl:
			'https://developer.wordpress.com/docs/site-performance/speed-test/#interaction-to-next-paint-inp-',
	},
	ttfb: {
		good: translate( 'Your site‘s Time to First Byte is excellent' ),
		needsImprovement: translate( 'Your site‘s Time to First Byte needs improvement' ),
		bad: translate( 'Your site‘s Time to First Byte is poor' ),
		heading: translate( 'What is Time to First Byte?' ),
		aka: translate( '(TTFB)' ),
		explanation: translate(
			'Time to First Byte reflects the time taken for a user‘s browser to receive the first byte of data from the server after making a request. The best sites load around 800 milliseconds or less.'
		),
		docsUrl:
			'https://developer.wordpress.com/docs/site-performance/speed-test/#time-to-first-byte-ttfb-',
	},
	tbt: {
		good: translate( 'Your site‘s Total Blocking Time is excellent' ),
		needsImprovement: translate( 'Your site‘s Total Blocking Time needs improvement' ),
		bad: translate( 'Your site‘s Total Blocking Time is poor' ),
		heading: translate( 'What is Total Blocking Time?' ),
		aka: translate( '(TBT)' ),
		explanation: translate(
			'Total Blocking Time measures the total amount of time that a page is blocked from responding to user input, such as mouse clicks, screen taps, or keyboard presses. The best sites have a wait time of less than 200 milliseconds.'
		),
		docsUrl:
			'https://developer.wordpress.com/docs/site-performance/speed-test/#total-blocking-time-tbt-',
	},
	overall: {
		good: translate( 'Your site‘s Performance Score is excellent' ),
		needsImprovement: translate( 'Your site‘s Performance Score needs improvement' ),
		bad: translate( 'Your site‘s Performance Score is poor' ),
		heading: translate( 'What is Performance Score?' ),
		aka: translate( '(PS)' ),
		explanation: translate(
			'The performance score is a combined representation of your site‘s individual speed metrics.'
		),
		docsUrl: 'https://developer.wordpress.com/docs/site-performance/speed-test/#performance-score',
	},
} );

// bad values are only needed as a maximum value on the scales
export const metricsThresholds = {
	lcp: {
		good: 2500,
		needsImprovement: 4000,
		bad: 6000,
	},
	cls: {
		good: 0.1,
		needsImprovement: 0.25,
		bad: 0.4,
	},
	fcp: {
		good: 1800,
		needsImprovement: 3000,
		bad: 5000,
	},
	ttfb: {
		good: 800,
		needsImprovement: 1800,
		bad: 3000,
	},
	inp: {
		good: 200,
		needsImprovement: 500,
		bad: 1000,
	},
	tbt: {
		good: 200,
		needsImprovement: 600,
		bad: 1000,
	},
	overall: {
		good: 100,
		needsImprovement: 89,
		bad: 49,
	},
};

export const getPerformanceStatus = ( value: number ) => {
	if ( value <= 49 ) {
		return 'bad';
	} else if ( value > 49 && value < 90 ) {
		return 'needsImprovement';
	}
	return 'good';
};

export const mapThresholdsToStatus = ( metric: Metrics, value: number ): Valuation => {
	const { good, needsImprovement } = metricsThresholds[ metric ];

	if ( metric === 'overall' ) {
		return getPerformanceStatus( value );
	}
	if ( value <= good ) {
		return 'good';
	}

	if ( value <= needsImprovement ) {
		return 'needsImprovement';
	}

	return 'bad';
};

export const max2Decimals = ( val: number ) => +Number( val ).toFixed( 2 );

export const displayValue = ( metric: Metrics, value: number ): string => {
	if ( value === null || value === undefined ) {
		return '';
	}

	if ( [ 'lcp', 'fcp', 'ttfb', 'inp', 'fid', 'tbt' ].includes( metric ) ) {
		return `${ max2Decimals( value / 1000 ) }s`;
	}

	return `${ max2Decimals( value ) }`;
};

export const filterRecommendations = (
	selectedFilter: string,
	audit?: PerformanceMetricsItemQueryResponse
) => {
	return (
		selectedFilter === 'all' || audit?.metricSavings?.hasOwnProperty( selectedFilter.toUpperCase() )
	);
};

export const highImpactAudits = [
	'render-blocking-resources',
	'uses-responsive-images',
	'uses-optimized-images',
	'offscreen-images',
	'server-response-time',
	'mainthread-work-breakdown',
	'largest-contentful-paint-element',
];