fix(v2): match the cover extension before the cache-busting query

`path_cover_large` and friends arrive as `.../cover/big.png?ts=<updated_at>`,
so the end-anchored `/\.(png|jpe?g)$/` never matched one and no v2 surface
rewrote a real cover URL even with the flag read correctly. v1's regex is
unanchored by accident, which is why it worked there.

Give the rewrite one home in `useWebpSupport`, matching the extension at the
end of the path rather than of the URL, and have the three copies use it.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Georges-Antoine Assi
2026-09-09 21:36:51 -04:00
parent 7c088e8046
commit 5946b3c0c8
6 changed files with 58 additions and 22 deletions

View File

@@ -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);
}

View File

@@ -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);

View File

@@ -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<BoxartStyle, number> = {
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);

View File

@@ -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=<updated_at>`.
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", {

View File

@@ -7,11 +7,22 @@
//
// const { supportsWebp, toWebp } = useWebpSupport();
// <img :src="toWebp(rom.path_cover_large)" />
//
// 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=<updated_at>`, 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<boolean>;
@@ -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 };

View File

@@ -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=<updated_at>` 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);
});
});