Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix(loras): some Civitai preview images failed to load

Adam Eivy7042f567e827
diff --git a/client/src/pages/Loras.jsx b/client/src/pages/Loras.jsx
index 38edbe79ac..187fca66fa 100644
--- a/client/src/pages/Loras.jsx
+++ b/client/src/pages/Loras.jsx
@@ -296,7 +296,7 @@ function SuggestionCard({ card, installed, installing, onInstall }) {
return (
<div className="bg-port-card border border-port-border rounded-lg overflow-hidden flex flex-col">
{card.previewImageUrl ? (
- <img src={card.previewImageUrl} alt="" className="w-full h-56 object-cover bg-port-bg" loading="lazy" />
+ <img src={card.previewImageUrl} alt="" className="w-full h-56 object-cover bg-port-bg" loading="lazy" referrerPolicy="no-referrer" />
) : (
<div className="w-full h-56 bg-port-bg flex items-center justify-center text-gray-700">
<Sparkles size={32} />
@@ -503,6 +503,7 @@ function LoraCard({ lora, onDelete, deleting }) {
alt=""
className="w-full h-48 object-cover bg-port-bg"
loading="lazy"
+ referrerPolicy="no-referrer"
/>
) : (
<div className="w-full h-48 bg-port-bg flex items-center justify-center text-gray-700">
diff --git a/server/lib/civitai.js b/server/lib/civitai.js
index fc02424715..67ec73a967 100644
--- a/server/lib/civitai.js
+++ b/server/lib/civitai.js
@@ -151,6 +151,28 @@ export const pickPreviewImage = (version) => {
return sorted[0] || null;
};
+// Normalize a Civitai CDN image URL so it's bounded-width and reliably
+// served. Civitai's CDN encodes its image transform as a path segment:
+// https://image.civitai.com/<hash>/<uuid>/<transform>/<filename>.<ext>
+// Common transforms are `original=true`, `width=450`, `fit=crop,width=...`.
+// The `original=true` variant occasionally returns 401/403 (auth-gated for
+// NSFW or simply blocked by hotlink protection) where smaller width-bounded
+// variants are public. Replacing the transform with `width=512` gives a
+// snappier, more-likely-to-render thumbnail. Non-Civitai URLs and URLs
+// without a recognizable transform segment pass through unchanged.
+export const normalizeCivitaiImageUrl = (url, width = 512) => {
+ if (typeof url !== 'string' || !url) return url;
+ if (!/^https?:\/\/(image\.civitai\.com|civitai\.com)\//i.test(url)) return url;
+ // Find the path segment immediately before the trailing /<filename>.<ext>
+ // and replace it with `width=N` if it looks like a transform spec
+ // (contains `=`). Doesn't touch the filename or query string.
+ const match = url.match(/^(.*\/)([^/]+)(\/[^/]+\.(?:jpeg|jpg|png|webp|gif|avif))(\?.*)?$/i);
+ if (!match) return url;
+ const [, prefix, transform, file, query = ''] = match;
+ if (!transform.includes('=')) return url; // not a Civitai transform segment
+ return `${prefix}width=${width}${file}${query}`;
+};
+
// Build the auth header for HTTP requests when an API key is set.
// Civitai's API also accepts the token as a `?token=` query param on
// download URLs, but Authorization: Bearer is documented for /api/v1 calls.
@@ -223,7 +245,7 @@ export const buildSidecar = ({ model, version, file, filename }) => {
hashes: file?.hashes || {},
downloadUrl: file?.downloadUrl || null,
},
- previewImageUrl: previewImage?.url || null,
+ previewImageUrl: normalizeCivitaiImageUrl(previewImage?.url) || null,
installedAt: new Date().toISOString(),
};
};
diff --git a/server/lib/civitai.test.js b/server/lib/civitai.test.js
index 1b31e01c77..dce5afe573 100644
--- a/server/lib/civitai.test.js
+++ b/server/lib/civitai.test.js
@@ -5,6 +5,7 @@ import {
buildAuthHeaders,
buildSidecar,
fetchCivitaiModel,
+ normalizeCivitaiImageUrl,
parseCivitaiUrl,
pickPrimaryFile,
pickPreviewImage,
@@ -153,6 +154,39 @@ describe('buildAuthHeaders / applyDownloadToken', () => {
});
});
+describe('normalizeCivitaiImageUrl', () => {
+ it('rewrites the original=true transform to a width-bounded variant', () => {
+ const original = 'https://image.civitai.com/xG1nkqKTMzGDvpLrqFT7WA/1f686daa-ff68-4830-ae94-c318e8f9ee30/original=true/129269017.jpeg';
+ const out = normalizeCivitaiImageUrl(original);
+ expect(out).toBe('https://image.civitai.com/xG1nkqKTMzGDvpLrqFT7WA/1f686daa-ff68-4830-ae94-c318e8f9ee30/width=512/129269017.jpeg');
+ });
+ it('rewrites existing width= transforms to the requested size', () => {
+ expect(normalizeCivitaiImageUrl('https://image.civitai.com/abc/uuid/width=200/file.jpeg', 800))
+ .toBe('https://image.civitai.com/abc/uuid/width=800/file.jpeg');
+ });
+ it('rewrites compound transforms (fit=crop,width=N,height=N)', () => {
+ expect(normalizeCivitaiImageUrl('https://image.civitai.com/h/u/fit=crop,width=300,height=300/x.jpeg'))
+ .toBe('https://image.civitai.com/h/u/width=512/x.jpeg');
+ });
+ it('preserves a query string if present', () => {
+ expect(normalizeCivitaiImageUrl('https://image.civitai.com/h/u/original=true/x.jpeg?v=2'))
+ .toBe('https://image.civitai.com/h/u/width=512/x.jpeg?v=2');
+ });
+ it('passes through non-Civitai URLs unchanged', () => {
+ const url = 'https://huggingface.co/datasets/x/resolve/main/preview.png';
+ expect(normalizeCivitaiImageUrl(url)).toBe(url);
+ });
+ it('passes through URLs with no transform segment', () => {
+ const url = 'https://image.civitai.com/foo.jpeg';
+ expect(normalizeCivitaiImageUrl(url)).toBe(url);
+ });
+ it('handles null / non-string inputs without throwing', () => {
+ expect(normalizeCivitaiImageUrl(null)).toBe(null);
+ expect(normalizeCivitaiImageUrl(undefined)).toBe(undefined);
+ expect(normalizeCivitaiImageUrl('')).toBe('');
+ });
+});
+
describe('slugifyForFilename', () => {
it('strips path-unsafe chars', () => {
expect(slugifyForFilename('Real / Stagram')).toBe('real-stagram');
diff --git a/server/services/civitaiSuggestions.js b/server/services/civitaiSuggestions.js
index aefa10986b..993526ea24 100644
--- a/server/services/civitaiSuggestions.js
+++ b/server/services/civitaiSuggestions.js
@@ -12,6 +12,7 @@
import {
baseModelToRunner,
fetchCivitaiModel,
+ normalizeCivitaiImageUrl,
pickPreviewImage,
pickVersion,
searchCivitaiLoras,
@@ -64,7 +65,7 @@ const buildCard = (model) => {
runnerFamily: baseModelToRunner(baseModel),
triggerWords: Array.isArray(version.trainedWords) ? version.trainedWords : [],
samplePrompt: extractSamplePrompt(version),
- previewImageUrl: previewImage?.url || null,
+ previewImageUrl: normalizeCivitaiImageUrl(previewImage?.url) || null,
downloads: model.stats?.downloadCount ?? null,
rating: model.stats?.rating ?? null,
nsfw: !!model.nsfw,
diff --git a/server/services/loras.js b/server/services/loras.js
index 9861bc45d5..9ec43d7eac 100644
--- a/server/services/loras.js
+++ b/server/services/loras.js
@@ -28,6 +28,7 @@ import {
applyDownloadToken,
buildSidecar,
fetchCivitaiModel,
+ normalizeCivitaiImageUrl,
parseCivitaiUrl,
pickPrimaryFile,
pickVersion,
@@ -103,7 +104,9 @@ export const listLoras = async () => {
// Coerce non-finite values (NaN, Infinity, missing/malformed sidecar
// fields) to the default — `?? 1.0` alone wouldn't catch NaN.
recommendedScale: Number.isFinite(sidecar?.recommendedScale) ? sidecar.recommendedScale : 1.0,
- previewImageUrl: sidecar?.previewImageUrl || null,
+ // Normalize on read so already-installed LoRAs (sidecars written
+ // before the URL-normalize fix) also benefit without a reinstall.
+ previewImageUrl: normalizeCivitaiImageUrl(sidecar?.previewImageUrl) || null,
description: sidecar?.description || '',
};
}));