Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 21 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,32 +149,31 @@ The read-only benchmark is available in `server/scripts/prefix-search-perf.sql`.

We consider sustainability through design choices that reduce unnecessary data transfer, processing and resource use, while acknowledging performance tradeoffs.

| Measure | Resource consideration | Effect |
| ------------------------------- | -------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- |
| Plain CSS, no UI framework | No component-library runtime to download or parse | 0 UI dependencies in the bundle |
| `prefers-color-scheme` | OLED screens draw less power on a dark pixel | Dark theme when the OS asks for it |
| `prefers-reduced-motion` | Animations cost frames and battery | Collapsed to near-zero when the OS asks for it |
| Nothing until Search is pressed | Typing would otherwise cost one request per change | `skipToken` holds the query back, so typing is free |
| `cache-first` fetch policy | A repeated search is already in the cache | 0 requests when the answer is cached |
| Code-split album route | The detail page is not needed to browse search results | Separate 3.73 kB chunk, kept out of the main bundle |
| Bundle budget in CI | CI fails before a dependency quietly doubles the payload | 512 000 bytes per JS chunk |
| Indexed prefix search | Uses the existing GIN index, but broad prefixes require more database processing | Better search flexibility, but potentially higher query costs for broad prefixes |

**Bundle size**, production build measured 21 October 2026:
| Measure | Resource consideration | Effect |
| ---------------------------------- | -------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- |
| Plain CSS, no UI framework | No component-library runtime to download or parse | 0 UI dependencies in the bundle |
| `prefers-color-scheme` | OLED screens draw less power on a dark pixel | Dark theme when the OS asks for it |
| `prefers-reduced-motion` | Animations cost frames and battery | Collapsed to near-zero when the OS asks for it |
| Nothing until Search is pressed | Typing would otherwise cost one request per change | `skipToken` holds the query back, so typing is free |
| `cache-first` fetch policy | A repeated search is already in the cache | 0 requests when the answer is cached |
| Cursor pagination and cached pages | Load only the next batch and reuse pages already fetched | 20 albums per page; 0 requests to reveal cached pages |
| Code-split album route | The detail page is not needed to browse search results | Separate 3.73 kB chunk, kept out of the main bundle |
| Bundle budget in CI | CI fails before a dependency quietly doubles the payload | 512 000 bytes per JS chunk |
| Indexed prefix search | Uses the existing GIN index, but broad prefixes require more database processing | Better search flexibility, but potentially higher query costs for broad prefixes |

**Bundle size**, production build measured 26 October 2026:

| File | Raw | Gzip |
| ----------------------- | --------: | --------: |
| `index-*.js` | 425.02 kB | 129.12 kB |
| `index-*.css` | 4.85 kB | 1.73 kB |
| `AlbumDetailPage-*.js` | 3.73 kB | 1.30 kB |
| `AlbumDetailPage-*.css` | 1.26 kB | 0.53 kB |
| `index-*.js` | 430.26 kB | 131.10 kB |
| `index-*.css` | 4.99 kB | 1.77 kB |
| `AlbumDetailPage-*.js` | 8.29 kB | 2.88 kB |
| `AlbumDetailPage-*.css` | 1.94 kB | 0.71 kB |
| `index.html` | 0.51 kB | 0.31 kB |

The two `AlbumDetailPage` rows are the chunk added by the album detail route (#11) and are not part of the search page payload. Note that the album link is a full page load today, so opening an album re-downloads the main bundle as well; the split still keeps the detail page out of the initial payload.
The two `AlbumDetailPage` rows represent the separate chunks for the album detail route and are not part of the search page payload. The album link currently causes a full page load, so opening an album also reloads the main bundle.

**Requests for a typical search: one.** The client has exactly one `useQuery`, so a search is a single `searchAlbums` request. Nothing is sent while the page is idle and nothing while the user types. Searching again for something already in the cache sends nothing. Opening an album is one further `album(id:)` request.

**Accessibility and bærekraft pull in the same direction here.** No request until Search is pressed is also why the result count is announced once instead of on every keystroke, and `prefers-reduced-motion` costs nothing for anyone who does not ask for it.
**Requests for a typical search:** An uncached search initially fetches up to 20 albums in one `searchAlbums` request. Infinite scroll loads up to two additional pages automatically, while further pages are loaded using the Load more button. Previously fetched pages are reused from Apollo's cache without new requests. No search requests are sent while the user types.

## Project status

Expand All @@ -196,9 +195,9 @@ The two `AlbumDetailPage` rows are the chunk added by the album detail route (#1

**Implemented:** The PostgreSQL database includes albums, artists, genres, tracks, users, reviews and crate items. The GraphQL backend supports album and artist search, genre and decade filters, sorting and cursor-based pagination. Supported sorts are `RELEVANCE`, `TITLE_ASC` and `RELEASE_YEAR_DESC`. `AVERAGE_RATING_DESC` is defined in the schema but currently returns an error because reviews are not implemented yet.

The frontend supports searching by album title or artist, committed on Search or Enter rather than while typing, with loading and error feedback, and a no-results message. It currently shows the first 20 matches ordered by relevance.
The frontend supports searching by album title or artist, committed on Search or Enter rather than while typing, with loading and error feedback, and a no-results message. Searches start with 20 visible matches ordered by relevance. Infinite scroll reveals up to 60 matches, then Load more reveals subsequent pages. Returning to a search starts with 20 visible matches and reuses cached pages before requesting more.

**Planned:** Frontend filters, sorting and pagination controls; album details; reviews, ratings and wantlist functionality; and Playwright end-to-end tests (M4). The database tables for reviews and crate items exist, but their GraphQL functionality is still planned. `X-User-Id` is also defined but not yet handled at runtime. Accessibility and sustainability are M3 work and are covered under [Sustainability measures](#sustainability-measures) and the accessibility pass in the issue tracker.
**Planned:** Frontend filters and sorting controls; album details; reviews, ratings and wantlist functionality; and Playwright end-to-end tests (M4). The database tables for reviews and crate items exist, but their GraphQL functionality is still planned. `X-User-Id` is also defined but not yet handled at runtime. Accessibility and sustainability are M3 work and are covered under [Sustainability measures](#sustainability-measures) and the accessibility pass in the issue tracker.

### Backend search behavior

Expand Down
13 changes: 10 additions & 3 deletions client/src/App.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ describe('App search', () => {
expect(result).toHaveBeenCalledTimes(1);
});

it('starts with labelled search and accessible landmarks, without unrelated controls', () => {
it('starts with labelled search and accessible landmarks, hiding pagination until results exist', () => {
renderApp();
expect(
screen.getByRole('searchbox', { name: 'Search by album or artist' }),
Expand Down Expand Up @@ -179,9 +179,16 @@ describe('App search', () => {
expect(window.location.search).toBe('?q=Abbey');
});

it('returns to an empty discovery page from the main navigation', () => {
it('returns to an empty discovery page from the main navigation', async () => {
window.history.replaceState({}, '', '/?q=Abbey&sort=TITLE_ASC');
renderApp();
const mock = mockSearch('Abbey');
mock.request.variables = {
input: { query: 'Abbey', sort: 'TITLE_ASC' },
first: 20,
after: null,
};
renderApp([mock]);
await screen.findByRole('article', { name: 'Abbey Road' });

expect(screen.getByRole('link', { name: 'Discover' })).toHaveAttribute(
'href',
Expand Down
6 changes: 6 additions & 0 deletions client/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ function DiscoveryPage() {
totalCount={search.totalCount}
hasQuery={search.hasQuery}
loading={search.loading}
loadingMore={search.loadingMore}
pageError={search.pageError}
hasNextPage={search.hasNextPage}
onLoadMore={search.loadMore}
canLoadAutomatically={search.canLoadAutomatically}
onLoadMoreAutomatically={search.loadMoreAutomatically}
retrying={search.retrying}
error={search.error}
onRetry={search.retry}
Expand Down
6 changes: 6 additions & 0 deletions client/src/components/ResultsLayout.css
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
.results {
padding-bottom: 2.5rem;
}

.pagination {
text-align: center;
margin-top: 1.5rem;
}

.album-grid {
list-style: none;
margin: 0;
Expand Down
87 changes: 84 additions & 3 deletions client/src/components/ResultsLayout.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import './ResultsLayout.css';

import { useEffect, useRef } from 'react';
import AlbumCard from './AlbumCard.js';
import type { AlbumSummary } from '../api/searchAlbums.js';

Expand All @@ -8,18 +9,76 @@ interface Props {
totalCount: number;
hasQuery: boolean;
loading: boolean;
loadingMore: boolean;
pageError: boolean;
hasNextPage: boolean;
onLoadMore: () => void;
canLoadAutomatically: boolean;
onLoadMoreAutomatically: () => void;
retrying: boolean;
error: boolean;
onRetry: () => void;
onReset: () => void;
}

export default function ResultsLayout(props: Props) {
const sentinel = useRef<HTMLDivElement>(null);
const {
hasNextPage,
loading,
loadingMore,
pageError,
canLoadAutomatically,
onLoadMoreAutomatically,
} = props;

useEffect(() => {
if (
!hasNextPage ||
!canLoadAutomatically ||
loading ||
loadingMore ||
pageError ||
!sentinel.current ||
typeof IntersectionObserver === 'undefined'
) {
return;
}

let active = true;
const observer = new IntersectionObserver(
(entries) => {
// Focus can scroll the button into view; let the user activate it.
if (
active &&
!sentinel.current?.contains(document.activeElement) &&
entries.some((entry) => entry.isIntersecting)
) {
onLoadMoreAutomatically();
}
},
{ rootMargin: '300px' },
);

observer.observe(sentinel.current);
return () => {
active = false;
observer.disconnect();
};
}, [
hasNextPage,
loading,
loadingMore,
pageError,
canLoadAutomatically,
onLoadMoreAutomatically,
]);

return (
<section
className="results"
aria-labelledby="results-heading"
aria-busy={props.loading}
aria-busy={props.loading || props.loadingMore}
>
<h2 id="results-heading">Search results</h2>
<p role="status" aria-live="polite" className="muted">
Expand All @@ -31,7 +90,9 @@ export default function ResultsLayout(props: Props) {
: 'Searching albums…'
: props.error
? 'Search failed.'
: `Showing ${props.albums.length} of ${props.totalCount} albums.`}
: props.loadingMore
? 'Loading more albums…'
: `Showing ${props.albums.length} of ${props.totalCount} albums.`}
</p>
{props.hasQuery &&
(props.error || props.retrying ? (
Expand All @@ -44,7 +105,9 @@ export default function ResultsLayout(props: Props) {
className="primary-button"
aria-disabled={props.loading}
onClick={() => {
if (!props.loading) props.onRetry();
if (!props.loading) {
props.onRetry();
}
}}
>
Retry search
Expand All @@ -67,6 +130,24 @@ export default function ResultsLayout(props: Props) {
))}
</ul>
))}
{props.hasQuery && !props.loading && !props.error && props.hasNextPage && (
<div className="pagination" ref={sentinel}>
{props.pageError && (
<p role="alert">Could not load more albums. Please try again.</p>
)}
<button
className="primary-button"
aria-disabled={props.loadingMore}
onClick={() => {
if (!props.loadingMore) {
props.onLoadMore();
}
}}
>
Load more
</button>
</div>
)}
</section>
);
}
Loading