Signed thumbnails on the listing cards

React · React · intermediate · modification

Search results load every listing's full-size upload, and uploads can be several megabytes, so the results page is slow on mobile. This PR switches the cards to 640px thumbnails from the image service. `src/listings/thumbnailUrl.js` (new) builds the signed thumbnail URL with Web Crypto (HMAC-SHA256 over the URL path, as the image service expects). `src/listings/ListingCard.jsx` builds it when the card mounts, keeps the empty photo box until it is ready, and falls back to the original upload if the URL cannot be built or the thumbnail fails to load. The signing key is read from `VITE_IMAGE_SIGNING_KEY`, which CI provides as a masked, protected CI/CD variable, so the key is not in the repository; `.env.example` documents it for local development. `src/listings/ListingCard.test.jsx` (new) covers the thumbnail URL, both fallbacks and the card's text.

Staybook, a holiday-rental site: a React 18.3 single-page app built with Vite 5.4 (`vite build` writes static files to `dist/`), function components with hooks. Tests run with `npm test`: Vitest 2 with @testing-library/react 16 in jsdom. CI runs `vite build` with the project's CI/CD variables in its environment, then deploys `dist/` together with the site's API server, `server/index.js` (Express 4, ESM, Node 22; existing and unchanged), which serves `dist/` as static files and runs with the same CI/CD variables in its environment. Existing and unchanged: `SearchResultsPage.jsx` loads the results once, shows its own loading, error and empty states, and renders one `<ListingCard key={listing.id} listing={listing} />` per result; a card's `listing` does not change while the page is open. `listing` is `{ id, title, city, nightlyPriceLabel, photoUrl }`: `id` a non-empty string, `title` and `city` non-empty strings, `nightlyPriceLabel` the price already formatted by the API (for example `€120 / night`), `photoUrl` an absolute URL under `https://uploads.staybook.example/listings/` (the API validates it). The image service: `https://img.staybook.example/<signature>/w/<width>/<source>`, where `<source>` is the original image URL encoded with `encodeURIComponent` and `<signature>` is the HMAC-SHA256 of the path from `/w/` onwards (for example `/w/640/https%3A%2F%2F...`), keyed with the signing key's UTF-8 bytes, in base64url without padding. It fetches `<source>` and returns it resized to `<width>` pixels wide. `ListingCard.css` (unchanged) gives `.listing-card__photo` a 4:3 box with a `var(--bg-subtle)` background. `.env.example` is committed; `.env.local` is gitignored.

Requirements

Files touched

Review this PR

React practice