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
- Listing cards on the search results page show a 640px-wide thumbnail from our image service instead of the full-size upload. The image service serves only signed URLs (format in the context); a URL with a missing or wrong signature answers 403.
- The image signing key is a secret: whoever holds it can sign URLs for any source address and any width, and so make the image service fetch and resize arbitrary images on our account. Visitors of the site must not be able to obtain it.
- If a card's thumbnail cannot be produced, the card shows the original upload (`listing.photoUrl`) instead, so a configuration problem means slower photos, not missing ones.
- Unchanged: the card's title link to `/listings/<id>`, the city and the nightly price. The card has tests that run with the existing `npm test`.
Files touched
- .env.example
- src/listings/thumbnailUrl.js
- src/listings/ListingCard.jsx
- src/listings/ListingCard.test.jsx