Skip to content

Commit a0ba837

Browse files
committed
feat: page speed fetch request
Update analyze button so it makes the the page speed fetch request to the testing URL. Updates async handling of the button and includes both console and a UI facing error message.
1 parent 15157c7 commit a0ba837

File tree

3 files changed

+43
-12
lines changed

3 files changed

+43
-12
lines changed

src/core/PageSpeedService.js

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,35 @@
88
*
99
* @param {string} urlToMeasure - URL to measure the performance of.
1010
* @param {bool} logDebug - Whether to log results of request to the console for debugging.
11-
* @returns {JSON} @see https://developers.google.com/speed/docs/insights/v5/reference/pagespeedapi/runpagespeed#response
11+
* @returns {Promise<JSON>|Promise<null>} @see https://developers.google.com/speed/docs/insights/v5/reference/pagespeedapi/runpagespeed#response
1212
*/
1313
export const makePageSpeedAPIRequest = async (urlToMeasure, logDebug = false) => {
1414
// Proxy URL using serverless function that will return data.
15-
const proxyRequestUrl = ``;
15+
// TODO: Replace testing environment URL with actual URL.
16+
const proxyRequestUrl = `https://deploy-preview-2--carbon-calculator-proxy.netlify.app/.netlify/functions/page-speed?url=${encodeURIComponent(urlToMeasure)}`;
1617

18+
// Debug: log intro to console.
1719
if (logDebug) {
1820
console.log(`~~~~~~ Google PageSpeed Results for: "${urlToMeasure}" ~~~~~~`);
1921
console.log(`Making request to "${proxyRequestUrl}" ...`);
2022
}
21-
};
2223

24+
// Make request.
25+
try {
26+
const response = await fetch(proxyRequestUrl);
27+
if (!response.ok) {
28+
throw new Error(`Fetch error encountered. Response status: ${response.status}`);
29+
}
30+
31+
const result = await response.json();
32+
33+
// Debug: log results to console.
34+
if (logDebug) {
35+
console.log(`Results JSON response:`, result);
36+
}
37+
return result;
38+
} catch (error) {
39+
console.error("Error with Page Speed API request:", error.message);
40+
return null;
41+
}
42+
};

src/panels/welcome/welcome.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ <h2 class="cv-welcome__title">Welcome to Carbon Visualizer</h2>
3838
<button class="cv-btn cv-btn--primary" id="analyze-page-btn">
3939
Analyze This Page
4040
</button>
41+
<p class="message message--negative" id="analyze-page-error"></p>
4142
</div>
4243
</main>
4344
</div>

src/panels/welcome/welcome.js

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,35 @@ export function initializePanel(panelType, data) {
1111

1212
// Get analyze button from within the container
1313
const analyzeBtn = container.querySelector('#analyze-page-btn');
14+
const analyzeErrorMessage = container.querySelector('#analyze-page-error');
1415

1516
// Check if button exists before adding event listener
1617
if (!analyzeBtn) {
1718
return;
1819
}
1920

2021
// Event listener for analyze button
21-
analyzeBtn.addEventListener('click', () => {
22-
// For now, just show a simple message
22+
analyzeBtn.addEventListener('click', async () => {
23+
// For now, just show a simple message within the button.
2324
analyzeBtn.disabled = true;
24-
analyzeBtn.textContent = 'Test: logging to console...';
25+
analyzeBtn.textContent = 'Test: Making request and logging to console...';
26+
if (analyzeErrorMessage) {
27+
analyzeErrorMessage.textContent = '';
28+
}
2529

2630
// Make API request.
2731
const currentPageURL = window.location.toString();
28-
const pageSpeedResults = makePageSpeedAPIRequest(currentPageURL, true);
29-
30-
setTimeout(() => {
31-
analyzeBtn.disabled = false;
32-
analyzeBtn.textContent = 'Analyze This Page';
33-
}, 2000);
32+
const pageSpeedResults = await makePageSpeedAPIRequest(currentPageURL, true);
33+
34+
// Display error message in UI if there was an error or nothing was returned.
35+
if (!pageSpeedResults || Object.keys(pageSpeedResults).length === 0) {
36+
if (analyzeErrorMessage) {
37+
analyzeErrorMessage.textContent = 'Sorry! An error occurred when making the API request that tests the page. Please try again a little later. If this continues to happen, please let us know!';
38+
}
39+
}
40+
41+
// Reset button back to usable state.
42+
analyzeBtn.textContent = 'Analyze This Page';
43+
analyzeBtn.disabled = false;
3444
});
3545
}

0 commit comments

Comments
 (0)