:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(desktop): serve GitHub comment images from the disk cache (#90685)
Charles Vien04bf0d56b2ba
diff --git a/products/desktop/packages/ui/src/features/editor/components/githubCommentImages.test.tsx b/products/desktop/packages/ui/src/features/editor/components/githubCommentImages.test.tsxnew file mode 100644index 000000000000..4efccfba6a1c--- /dev/null+++ b/products/desktop/packages/ui/src/features/editor/components/githubCommentImages.test.tsx@@ -0,0 +1,32 @@+import { fireEvent, render, screen } from "@testing-library/react";+import { describe, expect, it, vi } from "vitest";+import { githubCommentComponents } from "./githubCommentImages";+import { MarkdownRenderer } from "./MarkdownRenderer";++vi.mock("@posthog/ui/shell/cachedImageUrl", () => ({+ cachedImageUrl: (remoteUrl: string) =>+ `posthog-cache://images/?src=${encodeURIComponent(remoteUrl)}`,+}));++const SOURCE = "https://user-images.githubusercontent.com/1/example.png";++describe("githubCommentComponents", () => {+ it("falls back to the origin when the cache declines the image", () => {+ render(+ <MarkdownRenderer+ content={``}+ componentsOverride={githubCommentComponents}+ />,+ );++ const image = screen.getByAltText("Screenshot");+ expect(image).toHaveAttribute(+ "src",+ expect.stringContaining("posthog-cache://"),+ );++ fireEvent.error(image);++ expect(screen.getByAltText("Screenshot")).toHaveAttribute("src", SOURCE);+ });+});diff --git a/products/desktop/packages/ui/src/features/editor/components/githubCommentImages.tsx b/products/desktop/packages/ui/src/features/editor/components/githubCommentImages.tsxindex c04cab219d58..39d3f1de69ba 100644--- a/products/desktop/packages/ui/src/features/editor/components/githubCommentImages.tsx+++ b/products/desktop/packages/ui/src/features/editor/components/githubCommentImages.tsx@@ -1,3 +1,5 @@+import { cachedImageUrl } from "@posthog/ui/shell/cachedImageUrl";+import { useState } from "react";import type { Components } from "react-markdown";const GITHUB_IMAGE_HOSTS = new Set([@@ -9,7 +11,9 @@ const GITHUB_IMAGE_HOSTS = new Set(["user-images.githubusercontent.com",]);-export function isGitHubHostedImage(source: string | undefined): boolean {+export function isGitHubHostedImage(+ source: string | undefined,+): source is string {if (!source) return false;try {@@ -25,7 +29,28 @@ export function isGitHubHostedImage(source: string | undefined): boolean {}}+/**+ * A comment image, read from the disk cache with the origin as a fallback.+ *+ * The cache answers 404 for anything it declines to hold, and a screenshot or+ * a screen-recording GIF often passes its size limit. GitHub serves those+ * fine, so a decline must not leave the reader with a broken image.+ */+function CommentImage({ src, alt }: { src: string; alt: string }) {+ const [declinedSrc, setDeclinedSrc] = useState<string | null>(null);++ return (+ <img+ src={declinedSrc === src ? src : cachedImageUrl(src)}+ alt={alt}+ onError={() => setDeclinedSrc(src)}+ />+ );+}+export const githubCommentComponents: Partial<Components> = {img: ({ src, alt }) =>- isGitHubHostedImage(src) ? <img src={src} alt={alt ?? ""} /> : null,+ isGitHubHostedImage(src) ? (+ <CommentImage src={src} alt={alt ?? ""} />+ ) : null,};