fix: support Immich v3 search/metadata cursor pagination

Immich v3 (server runs v3.1.0) returns search `nextPage` as an opaque cursor
string instead of a page number. The client schema validated `nextPage` as
`number`, so every album open failed schema validation and surfaced as
"Fotos momentan nicht erreichbar" (the catalog catch swallows the real
IMMICH_INVALID_RESPONSE into IMMICH_UNAVAILABLE).

Accept `string | number` for nextPage/previousPage in the schema and the
ImmichSearchPage type; we only use nextPage as a has-more-pages signal.
Pagination links are now derived from the current page number (currentPage ± 1)
instead of the returned nextPage value, since the v3 cursor can't be used as a
page number. Album page passes the normalized current page through.

Add a client test locking the v3 cursor-string response.

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Leon TonhäuserandClaude committed 2026-08-11 02:23:21 +02:00
1 parent 089f5f7035
commit 8dfaf8284e
6 files changed
+64 -13

No files matched your search

+3 -1
View File
@@ -1,7 +1,7 @@
import Link from "next/link"; import Link from "next/link";
import { notFound } from "next/navigation"; import { notFound } from "next/navigation";
import { AlbumCatalogError, albumCatalog } from "@/modules/albums/catalog"; import { AlbumCatalogError, albumCatalog, normalizePage } from "@/modules/albums/catalog";
import { PhotoContactSheet } from "@/modules/albums/catalog-content"; import { PhotoContactSheet } from "@/modules/albums/catalog-content";
import { loadEditorialDownloadOptions } from "@/modules/downloads/stream"; import { loadEditorialDownloadOptions } from "@/modules/downloads/stream";
@@ -19,6 +19,7 @@ export default async function AlbumPage({ params, searchParams }: AlbumPageProps
requireEditorial(), requireEditorial(),
]); ]);
const query = normalizedQuery(singleValue(rawSearchParams.suche)); const query = normalizedQuery(singleValue(rawSearchParams.suche));
const currentPage = normalizePage(singleValue(rawSearchParams.page));
const result = await loadAlbumPage({ const result = await loadAlbumPage({
editorialId: principal.editorialId, editorialId: principal.editorialId,
albumId, albumId,
@@ -45,6 +46,7 @@ export default async function AlbumPage({ params, searchParams }: AlbumPageProps
<PhotoContactSheet <PhotoContactSheet
albumId={result.album.id} albumId={result.album.id}
albumName={result.album.albumName} albumName={result.album.albumName}
currentPage={currentPage}
downloadOptions={result.downloadOptions} downloadOptions={result.downloadOptions}
page={result.page} page={result.page}
query={query} query={query}
+20 -7
View File
@@ -43,12 +43,14 @@ export function AlbumContactSheet({ albums }: Readonly<{ albums: VisibleAlbum[]
export function PhotoContactSheet({ export function PhotoContactSheet({
albumId, albumId,
albumName, albumName,
currentPage = 1,
downloadOptions, downloadOptions,
page, page,
query, query,
}: Readonly<{ }: Readonly<{
albumId?: string; albumId?: string;
albumName: string; albumName: string;
currentPage?: number;
downloadOptions?: EditorialDownloadOptions; downloadOptions?: EditorialDownloadOptions;
page: ImmichSearchPage; page: ImmichSearchPage;
query: string; query: string;
@@ -100,22 +102,33 @@ export function PhotoContactSheet({
</ul> </ul>
)} )}
{albumId ? <PhotoPagination albumId={albumId} page={page} query={query} /> : null} {albumId ? (
<PhotoPagination albumId={albumId} currentPage={currentPage} page={page} query={query} />
) : null}
</> </>
); );
} }
function PhotoPagination({ albumId, page, query }: Readonly<{ albumId: string; page: ImmichSearchPage; query: string }>) { // Immich v3 returns `nextPage` as an opaque cursor (or null), not the next
if (page.previousPage === null && page.nextPage === null) { // page number, so links are derived from the current page number instead:
// previous = currentPage - 1, next = currentPage + 1. `nextPage !== null`
// is the authoritative has-more-pages signal.
function PhotoPagination({
albumId,
currentPage,
page,
query,
}: Readonly<{ albumId: string; currentPage: number; page: ImmichSearchPage; query: string }>) {
const hasPrev = currentPage > 1;
const hasNext = page.nextPage !== null;
if (!hasPrev && !hasNext) {
return null; return null;
} }
return ( return (
<nav aria-label="Suchergebnisseiten" className="photo-pagination"> <nav aria-label="Suchergebnisseiten" className="photo-pagination">
{page.previousPage === null ? null : ( {hasPrev ? <Link href={pageHref(albumId, currentPage - 1, query)}>Vorherige Seite</Link> : null}
<Link href={pageHref(albumId, page.previousPage, query)}>Vorherige Seite</Link> {hasNext ? <Link href={pageHref(albumId, currentPage + 1, query)}>Nächste Seite</Link> : null}
)}
{page.nextPage === null ? null : <Link href={pageHref(albumId, page.nextPage, query)}>Nächste Seite</Link>}
</nav> </nav>
); );
} }
+1 -1
View File
@@ -92,7 +92,7 @@ function normalizeDescription(description: unknown): string | undefined {
return normalized || undefined; return normalized || undefined;
} }
function normalizePage(page: unknown): number { export function normalizePage(page: unknown): number {
const parsed = parsePage(page); const parsed = parsePage(page);
if (parsed === null) { if (parsed === null) {
return 1; return 1;
+22
View File
@@ -113,6 +113,28 @@ test("forces IMAGE and authorized album IDs in metadata search", async () => {
}); });
}); });
test("accepts an Immich v3 cursor-string nextPage without failing validation", async () => {
const v3Response = {
assets: {
items: [immichAsset],
total: 1,
count: 1,
facets: [],
nextPage: "eyJzZWNvbmRJZCI6IjEifQ==",
},
};
server.use(
http.post(`${baseUrl}/api/search/metadata`, () => HttpResponse.json(v3Response)),
);
await expect(client.searchPhotos({ albumIds: ["album-1"], page: 1, size: 60 })).resolves.toEqual({
items: [immichAsset],
total: 1,
nextPage: "eyJzZWNvbmRJZCI6IjEifQ==",
previousPage: null,
});
});
test("maps an Immich outage to IMMICH_UNAVAILABLE", async () => { test("maps an Immich outage to IMMICH_UNAVAILABLE", async () => {
server.use(http.get(`${baseUrl}/api/albums`, () => HttpResponse.error())); server.use(http.get(`${baseUrl}/api/albums`, () => HttpResponse.error()));
+14 -2
View File
@@ -55,8 +55,20 @@ const metadataSearchSchema = z
.object({ .object({
items: z.array(assetSchema), items: z.array(assetSchema),
total: z.number().int().nonnegative(), total: z.number().int().nonnegative(),
nextPage: z.number().int().nullable().optional().transform((value) => value ?? null), // Immich v3 returns `nextPage` as a cursor string (previously a page
previousPage: z.number().int().nullable().optional().transform((value) => value ?? null), // number); accept either so schema validation doesn't reject v3
// responses. We only use it as a has-more-pages signal, never as the
// page link target (links are currentPage ± 1).
nextPage: z
.union([z.string(), z.number()])
.nullable()
.optional()
.transform((value) => value ?? null),
previousPage: z
.union([z.string(), z.number()])
.nullable()
.optional()
.transform((value) => value ?? null),
}) })
.passthrough(), .passthrough(),
}) })
+4 -2
View File
@@ -23,8 +23,10 @@ export type ImmichAlbum = ImmichAlbumSummary & {
export type ImmichSearchPage = { export type ImmichSearchPage = {
items: ImmichAsset[]; items: ImmichAsset[];
total: number; total: number;
nextPage: number | null; // Immich v3 returns a cursor string; older versions returned a page number.
previousPage: number | null; // We only read nullability (has-more-pages), so accept either here.
nextPage: string | number | null;
previousPage: string | number | null;
}; };
export type ImmichBinary = { export type ImmichBinary = {