diff --git a/frontend/src/v2/components/Gallery/GameListRow.vue b/frontend/src/v2/components/Gallery/GameListRow.vue index 53420260e..52aaa0e4c 100644 --- a/frontend/src/v2/components/Gallery/GameListRow.vue +++ b/frontend/src/v2/components/Gallery/GameListRow.vue @@ -31,6 +31,7 @@ import SiblingBadge from "@/v2/components/GameCard/SiblingBadge.vue"; import { useBackgroundArt } from "@/v2/composables/useBackgroundArt"; import { useGallerySelectionInput } from "@/v2/composables/useGallerySelectionInput"; import { useViewTransition } from "@/v2/composables/useViewTransition"; +import { toWebpUrl } from "@/v2/composables/useWebpSupport"; import storeGalleryRoms, { type SimpleRom } from "@/v2/stores/galleryRoms"; import storeGallerySelection from "@/v2/stores/gallerySelection"; import { activeProviders } from "@/v2/utils/metadataProviders"; @@ -93,8 +94,6 @@ const { morphTransition } = useViewTransition(); const setBgArt = useBackgroundArt(); const { locale } = useI18n(); -const EXTENSION_REGEX = /\.(png|jpg|jpeg)$/i; - const columns = computed(() => getListColumns(props.showPlatformColumn)); const listSkeletonColumns = columns; @@ -228,11 +227,7 @@ function onRowHighlight() { const item = rom.value; if (!item) return; const path = item.path_cover_large ?? item.path_cover_small ?? null; - const coverUrl = path - ? props.webp - ? path.replace(EXTENSION_REGEX, ".webp") - : path - : null; + const coverUrl = path ? toWebpUrl(path, !!props.webp) : null; if (coverUrl) setBgArt(coverUrl); else if (item.url_cover) setBgArt(item.url_cover); } diff --git a/frontend/src/v2/composables/useBoxFaces/index.ts b/frontend/src/v2/composables/useBoxFaces/index.ts index 850ec3361..5d1ce2564 100644 --- a/frontend/src/v2/composables/useBoxFaces/index.ts +++ b/frontend/src/v2/composables/useBoxFaces/index.ts @@ -23,7 +23,7 @@ import { } from "vue"; import type { SimpleRom } from "@/stores/roms"; import { FRONTEND_RESOURCES_PATH } from "@/utils"; -import { useWebpSupport } from "@/v2/composables/useWebpSupport"; +import { toWebpUrl, useWebpSupport } from "@/v2/composables/useWebpSupport"; /** The face-relevant slice of a rom — satisfied by both `SimpleRom` and * `DetailedRom`. */ @@ -40,8 +40,6 @@ export interface BoxFaces { complete: boolean; } -const RASTER_EXT = /\.(png|jpe?g)$/i; - function resourceUrl(path: string | null | undefined): string | null { return path ? `${FRONTEND_RESOURCES_PATH}/${path}` : null; } @@ -52,10 +50,7 @@ export function computeBoxFaces( supportsWebp: boolean, ): BoxFaces { const localCover = rom.path_cover_large ?? rom.path_cover_small ?? null; - const cover = - localCover && supportsWebp - ? localCover.replace(RASTER_EXT, ".webp") - : localCover; + const cover = localCover ? toWebpUrl(localCover, supportsWebp) : localCover; const front = resourceUrl(rom.ss_metadata?.box2d_path) ?? cover; const back = resourceUrl(rom.ss_metadata?.box2d_back_path); diff --git a/frontend/src/v2/composables/useCoverArt/index.ts b/frontend/src/v2/composables/useCoverArt/index.ts index 0337200d2..15109aa73 100644 --- a/frontend/src/v2/composables/useCoverArt/index.ts +++ b/frontend/src/v2/composables/useCoverArt/index.ts @@ -40,7 +40,7 @@ import { isCDBasedSystem, isArcadeSystem, } from "@/utils"; -import { useWebpSupport } from "@/v2/composables/useWebpSupport"; +import { toWebpUrl, useWebpSupport } from "@/v2/composables/useWebpSupport"; export type BoxartStyle = | "cover_path" @@ -85,8 +85,6 @@ export const COVER_RATIOS: Record = { miximage_v2_path: 1, }; -const RASTER_EXT = /\.(png|jpe?g)$/i; - export function isBoxartStyle(value: unknown): value is BoxartStyle { return ( value === "cover_path" || @@ -203,8 +201,7 @@ export function computeCoverArt( coverUrl = `${opts.resourcesPath}/${altPath}`; } else { const local = rom.path_cover_large ?? rom.path_cover_small ?? null; - coverUrl = - local && opts.supportsWebp ? local.replace(RASTER_EXT, ".webp") : local; + coverUrl = local ? toWebpUrl(local, opts.supportsWebp) : local; } const fallbackUrl = override != null ? null : (rom.url_cover ?? null); diff --git a/frontend/src/v2/composables/useCoverArt/useCoverArt.test.ts b/frontend/src/v2/composables/useCoverArt/useCoverArt.test.ts index ffca67b45..c5f94c620 100644 --- a/frontend/src/v2/composables/useCoverArt/useCoverArt.test.ts +++ b/frontend/src/v2/composables/useCoverArt/useCoverArt.test.ts @@ -144,6 +144,18 @@ describe("computeCoverArt — cover_path", () => { ); expect(d.coverUrl).toBe("covers/large.webp"); }); + it("rewrites a cover carrying the backend's cache-busting query", () => { + // The shape every rom actually arrives in — `?ts=`. + const d = computeCoverArt( + rom({ path_cover_large: "covers/large.png?ts=2026-09-09T00:00:00" }), + "cover_path", + { + resourcesPath: RES, + supportsWebp: true, + }, + ); + expect(d.coverUrl).toBe("covers/large.webp?ts=2026-09-09T00:00:00"); + }); it("exposes url_cover as the fallback and flags no artwork when empty", () => { expect( computeCoverArt(rom({ url_cover: "https://x/c.png" }), "cover_path", { diff --git a/frontend/src/v2/composables/useWebpSupport/index.ts b/frontend/src/v2/composables/useWebpSupport/index.ts index ca592505c..0db349010 100644 --- a/frontend/src/v2/composables/useWebpSupport/index.ts +++ b/frontend/src/v2/composables/useWebpSupport/index.ts @@ -7,11 +7,22 @@ // // const { supportsWebp, toWebp } = useWebpSupport(); // +// +// Code without a component instance (a pure resolver, a store) reaches for +// `toWebpUrl` and passes the flag in. import { storeToRefs } from "pinia"; import { computed, type ComputedRef } from "vue"; import storeHeartbeat from "@/stores/heartbeat"; -const RASTER_EXT = /\.(png|jpe?g)$/i; +// The extension ends the *path*, not the URL: a cover arrives from the backend +// as `.../cover/big.png?ts=`, so anchoring on the end of the string +// would never match one. +const RASTER_EXT = /\.(png|jpe?g)(?=$|[?#])/i; + +/** Point a cover URL at its converted sibling, when the server serves them. */ +export function toWebpUrl(url: string, supportsWebp: boolean): string { + return supportsWebp ? url.replace(RASTER_EXT, ".webp") : url; +} export function useWebpSupport(): { supportsWebp: ComputedRef; @@ -26,7 +37,7 @@ export function useWebpSupport(): { function toWebp(url: string | null | undefined): string { if (!url) return ""; - return supportsWebp.value ? url.replace(RASTER_EXT, ".webp") : url; + return toWebpUrl(url, supportsWebp.value); } return { supportsWebp, toWebp }; diff --git a/frontend/src/v2/composables/useWebpSupport/useWebpSupport.test.ts b/frontend/src/v2/composables/useWebpSupport/useWebpSupport.test.ts index 8cb55dae6..c8286259b 100644 --- a/frontend/src/v2/composables/useWebpSupport/useWebpSupport.test.ts +++ b/frontend/src/v2/composables/useWebpSupport/useWebpSupport.test.ts @@ -1,7 +1,7 @@ import { createPinia, setActivePinia } from "pinia"; import { beforeEach, describe, expect, it } from "vitest"; import storeHeartbeat from "@/stores/heartbeat"; -import { useWebpSupport } from "."; +import { toWebpUrl, useWebpSupport } from "."; function setWebpTask(enabled: boolean) { const heartbeat = storeHeartbeat(); @@ -45,3 +45,29 @@ describe("useWebpSupport", () => { expect(toWebp(null)).toBe(""); }); }); + +describe("toWebpUrl", () => { + // What the backend actually sends: `path_cover_large` and friends carry a + // `?ts=` cache-buster, so an end-anchored match never fires. + const COVER = "/assets/romm/resources/roms/1/2/cover/big.png?ts=2026-09-09"; + + it("rewrites the extension behind a cache-busting query", () => { + expect(toWebpUrl(COVER, true)).toBe( + "/assets/romm/resources/roms/1/2/cover/big.webp?ts=2026-09-09", + ); + }); + + it("leaves the query itself alone", () => { + expect(toWebpUrl("cover/big.jpg?v=a.png", true)).toBe( + "cover/big.webp?v=a.png", + ); + }); + + it("rewrites a bare path too", () => { + expect(toWebpUrl("cover/big.jpeg", true)).toBe("cover/big.webp"); + }); + + it("passes the URL through when the server serves no webp", () => { + expect(toWebpUrl(COVER, false)).toBe(COVER); + }); +});