From 7a96c96bb0d6472f0d8f7aa06940c1f4ddebfacd Mon Sep 17 00:00:00 2001 From: SivertEikrem Date: Mon, 23 Nov 2026 14:54:36 +0100 Subject: [PATCH] Measure sustainability, and fix the layout shift on page load (#101) --- README.md | 60 ++++++++++++++----- .../components/layout/AppLayout.module.css | 7 +++ 2 files changed, 53 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 60d65f8..ff5a395 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Kork & Krone -**Last updated:** 19 November 2026 +**Last updated:** 23 November 2026 **Submission:** 4 (working system, ready for feedback on every aspect) **Status:** In progress. See [Status for submission 4](#status-for-submission-4). @@ -285,26 +285,58 @@ We follow the principle from the course: measure, set a budget and show the numb - Search, filtering, sorting and pagination run in the database. A page returns at most 50 wines. - The rating average is stored on the wine, so no query counts millions of rows. - DataLoader batches the lookups a page needs, so a page of wines runs one query per field instead of one per wine. -- The Apollo cache, so we do not fetch the same data twice. +- The Apollo cache, so we do not fetch the same data twice. Going back to a wine the user has already seen sends no requests at all. - Debounce on the search field (300 ms). An end-to-end test checks that a whole word sends only one search. - Apache compresses HTML, CSS and JavaScript with gzip. The main JavaScript file goes from 435 kB to 136 kB. +- The main area of every page is at least one screen tall, so the footer does not jump while a page's code loads (see the layout shift below). **Planned:** -- Cache headers in Apache. +- Compress the API's JSON responses in Apache. Today they are sent uncompressed. +- Cache headers in Apache for the built files. Their names contain a hash of the content, so they can be cached for a long time. Today Apache sends only an `ETag`, so the browser has to guess how long to keep them or ask again. ### Budget and measurements +Measured on 23 November 2026 on the VM, with the Full dataset (100 646 wines). The bytes are what +actually went over the network (`transferSize` from the browser's Resource Timing API), so compressed +files count at their compressed size. A first visit is with an empty cache, a repeat visit is a reload +afterwards. + | Metric | Budget | Measured | Date | How we measure | |---|---|---|---|---| -| JavaScript on the landing page (gzip) | Less than 150 kB | 155,05 kB (over budget after adding Apollo Client) | 7 Nov 2026 | Sum of the gzip sizes from `npm run build` for the files the landing page loads | -| Data transferred when loading the landing page | Less than 300 kB | Not measured yet | | Chrome DevTools, Network tab, cache disabled, on the VM | -| Data transferred in a fixed search scenario | Less than 500 kB | Not measured yet | | Same method. Needs the search page | -| Lighthouse (mobile) | Performance at least 90, accessibility 100 | Performance 39, accessibility 100, best practices 100, SEO 100 | 30 Oct 2026 | Lighthouse in Chrome DevTools, incognito, against the VM | -| CO2 per visit | Not decided | Not measured yet | | CO2.js (Sustainable Web Design model) with the transferred bytes | - -The search scenario is: open the landing page, search for "riesling", filter on red wine, and load one more page of results. -Lighthouse performance was low mainly because Apache did not compress files or set cache headers at the time, -and because the test was run over VPN. These are the "before" numbers. Compression is now on, and we will measure again. +| JavaScript on the landing page (gzip) | Less than 150 kB | 163.6 kB (over budget) | 23 Nov 2026 | Transferred JavaScript on the VM, from Resource Timing | +| Data transferred, landing page | Less than 300 kB | 172.5 kB in 20 requests on the first visit, 6.6 kB on a repeat visit | 23 Nov 2026 | Resource Timing on the VM | +| Data transferred, wine page | Less than 300 kB | 173.8 kB on the first visit, 14.6 kB on a repeat visit, 0 kB back to the same wine within the app | 23 Nov 2026 | Same method | +| Data transferred in a fixed search scenario | Less than 500 kB | 335.3 kB: 289.8 kB of code and styles, and 45.5 kB from the API in 7 requests | 23 Nov 2026 | Same method, through the whole scenario | +| Lighthouse (mobile) | Performance at least 90, accessibility 100 | On the VM: performance 78, accessibility 100, best practices 74, SEO 100 on the landing page, and 79 / 100 / 74 / 100 on the search page | 23 Nov 2026 | Lighthouse in Chrome DevTools, incognito, against the VM | +| Cumulative Layout Shift (CLS) | Less than 0.1 | 0.356 on the VM before the fix. 0.021 on the landing page and 0.005 on the search page after it, measured locally | 23 Nov 2026 | Lighthouse, as above | +| CO2 per visit | Less than 0.1 g | Landing page 0.026 g (0.001 g repeat visit), wine page 0.026 g (0.002 g repeat visit), the whole search scenario 0.051 g | 23 Nov 2026 | CO2.js 0.16, Sustainable Web Design model v4 (`perVisit`), with the transferred bytes | + +The search scenario is: open the landing page, search for "riesling", filter on white wine, and load one +more page of results. We first planned red wine, but there are only 7 red rieslings, too few for a second +page, so we changed it to white wine. + +**What the numbers show.** A first visit is mostly code: 163.6 of the 172.5 kB on the landing page is +JavaScript. After that, the browser keeps the code and Apollo keeps the data, so a repeat visit costs +6.6 kB, and moving around inside the app costs only the data for what is new. The whole search scenario, +with two pages of results, stays well under its budget. + +**JavaScript over budget.** The landing page has been over the 150 kB budget since we added Apollo +Client. Lighthouse reports 46 kB of JavaScript that the landing page loads without using. We keep the +budget rather than raising it, and look at splitting out more of the code that only other pages need. + +**Layout shift.** Lighthouse showed that the low performance score came mainly from Cumulative Layout +Shift: 0.356, where under 0.1 is good. While a page's code loaded, only «Laster …» was in the main area, +so the footer sat high up and was pushed down when the page came. We gave the main area a minimum height +of one screen, and CLS went down to 0.021 on the landing page and 0.005 on the search page. These after +numbers are from a local build (`npm run build` and `npm run preview`), because the fix was not on the VM +yet. Local runs have no network between the browser and the server, so we only compare CLS from them, +not the other numbers. + +**Best practices 74.** The VM serves the site over HTTP, not HTTPS, which Lighthouse marks down. The same +build gets 96 locally, where Chrome treats `localhost` as secure. + +**Earlier numbers.** On 30 October, Lighthouse performance was 39, mainly because Apache did not +compress files at the time and the test was run over VPN. Turning on compression took it to 78. ## Testing @@ -409,7 +441,8 @@ Claude and ChatGPT are the main models used. The usecases for the AI tools are a - [x] Wishlist stored in the backend, so it follows the user between devices. - [ ] Calmer landing page: less text, a separate «Om» page, and a burger menu on mobile. - [ ] CI, an enforced coverage threshold, and eslint-plugin-jsx-a11y. -- [ ] Measure sustainability and fill in the budget table. +- [x] Measure sustainability and fill in the budget table. +- [ ] Compress API responses and set cache header in Apache. - [ ] Screen reader test and a written reflection on the use of AI. - [ ] Prettier and ESLint in the backend, like in the frontend. @@ -430,7 +463,6 @@ Claude and ChatGPT are the main models used. The usecases for the AI tools are a - The cursors in the API hold an offset, which is slower deep into large results. To be solved together with the larger dataset. - The database and its user are still called `vinmonopolet` and `vinmonopolet_app`, from before we switched dataset. Renaming would mean everyone rebuilding their database, so we kept the names. - Reviews from before login have no account and are shown as «Anonym student». The old `student` table they belong to will be removed later. -- One e2e test, checking «Rødvin» in the filter dialog on mobile, fails since the error handling changes. Tracked in its own issue.