Skip to content

⭐️⭐️⭐️ Open-source, self-hosted alternative to Linear and Plane. Project management and issue tracking where teams and AI agents work side by side to plan and ship products.

fix(web): show SVG attachment thumbnails without the image optimizer (#661)

berceee253dcca524
diff --git a/apps/web/src/components/common/attachments/AttachmentThumb.test.tsx b/apps/web/src/components/common/attachments/AttachmentThumb.test.tsx
new file mode 100644
index 00000000..2900a8da
--- /dev/null
+++ b/apps/web/src/components/common/attachments/AttachmentThumb.test.tsx
@@ -0,0 +1,26 @@
+import assert from 'node:assert/strict';
+import { describe, it } from 'node:test';
+import { renderToStaticMarkup } from 'react-dom/server';
+import AttachmentThumb from './AttachmentThumb';
+
+const imgSrc = (contentType: string) =>
+ renderToStaticMarkup(
+ <AttachmentThumb
+ attachment={{ url: '/media/attachments/abc/raw', contentType, filename: 'logo' }}
+ />,
+ ).match(/<img[^>]* src="([^"]+)"/)?.[1];
+
+describe('AttachmentThumb', () => {
+ // The optimizer refuses SVG, so routing it there showed a broken thumbnail (#659).
+ it('shows an SVG from its own URL, not through the image optimizer', () => {
+ assert.equal(imgSrc('image/svg+xml'), '/media/attachments/abc/raw');
+ assert.equal(imgSrc('image/svg+xml; charset=utf-8'), '/media/attachments/abc/raw');
+ });
+
+ it('still sends other images through the optimizer', () => {
+ assert.match(
+ imgSrc('image/png') ?? '',
+ /^\/_next\/image\?url=%2Fmedia%2Fattachments%2Fabc%2Fraw/,
+ );
+ });
+});
diff --git a/apps/web/src/components/common/attachments/AttachmentThumb.tsx b/apps/web/src/components/common/attachments/AttachmentThumb.tsx
index 4f577d3c..b81a4157 100644
--- a/apps/web/src/components/common/attachments/AttachmentThumb.tsx
+++ b/apps/web/src/components/common/attachments/AttachmentThumb.tsx
@@ -1,6 +1,11 @@
import Image from 'next/image';
import { FileIcon } from 'lucide-react';
-import { isImage, isVideo, type Embeddable } from '@/components/common/editor/attachmentEmbed';
+import {
+ isImage,
+ isSvg,
+ isVideo,
+ type Embeddable,
+} from '@/components/common/editor/attachmentEmbed';
// An attachment's leading thumbnail: images and videos preview themselves,
// every other type falls back to a generic file glyph.
@@ -21,8 +26,8 @@ export default function AttachmentThumb({
fill
sizes={sizes}
// A file picked in the new issue modal is previewed from a local blob:
- // URL, which the image optimizer cannot fetch.
- unoptimized={attachment.url.startsWith('blob:')}
+ // URL, which the image optimizer cannot fetch; it refuses SVG.
+ unoptimized={attachment.url.startsWith('blob:') || isSvg(attachment)}
draggable={false}
className="object-cover"
/>
diff --git a/apps/web/src/components/common/editor/EditorImagePicker.tsx b/apps/web/src/components/common/editor/EditorImagePicker.tsx
index 2346c868..f22b2072 100644
--- a/apps/web/src/components/common/editor/EditorImagePicker.tsx
+++ b/apps/web/src/components/common/editor/EditorImagePicker.tsx
@@ -1,6 +1,6 @@
import Image from 'next/image';
import { Dialog, DialogContent, DialogHeader, DialogTitle } from '@/components/ui/dialog';
-import { type Embeddable } from '@/components/common/editor/attachmentEmbed';
+import { isSvg, type Embeddable } from '@/components/common/editor/attachmentEmbed';
import { useTranslations } from 'next-intl';
// Nothing here may take focus: blurring the editor saves the description, and the
@@ -48,6 +48,7 @@ export default function EditorImagePicker({
fill
// A grid cell is at most a third of the dialog's 32rem.
sizes="176px"
+ unoptimized={isSvg(a)}
className="object-cover"
/>
</button>
diff --git a/apps/web/src/components/common/editor/attachmentEmbed.ts b/apps/web/src/components/common/editor/attachmentEmbed.ts
index 827f9b2c..df70cb8a 100644
--- a/apps/web/src/components/common/editor/attachmentEmbed.ts
+++ b/apps/web/src/components/common/editor/attachmentEmbed.ts
@@ -6,6 +6,11 @@ export type Embeddable = { url: string; contentType: string; filename: string };
export const isImage = (a: Embeddable) => a.contentType.startsWith('image/');
export const isVideo = (a: Embeddable) => a.contentType.startsWith('video/');
+// next/image's optimizer refuses SVG (dangerouslyAllowSVG is off), and an
+// attachment URL (/media/.../raw) has no .svg extension for it to skip the
+// optimizer by, so an SVG has to be shown unoptimized. Shown in an <img>, it
+// runs no script.
+export const isSvg = (a: Embeddable) => a.contentType.split(';')[0].trim() === 'image/svg+xml';
// HTML handed to the tiptap editor (drag from the Attachments panel, file drop
// on the editor): images become an inline <img>, videos an inline <video>,