-
Notifications
You must be signed in to change notification settings - Fork 6
Image error listener #709
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Image error listener #709
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| "use client"; | ||
|
|
||
| import { useEffect } from "react"; | ||
| import * as Sentry from "@sentry/nextjs"; | ||
|
|
||
| const IMAGE_HOST = "images.ecency.com"; | ||
| const REPORT_SAMPLE_RATE = 0.2; | ||
| const DEBOUNCE_MS = 5000; | ||
|
|
||
| const recentlyReported = new Set<string>(); | ||
|
|
||
| export function ImageFailureTracker() { | ||
| useEffect(() => { | ||
| const handler = (event: Event) => { | ||
| const el = event.target; | ||
| if (!(el instanceof HTMLImageElement)) return; | ||
|
|
||
| const src = el.src || el.currentSrc; | ||
| if (!src || !src.includes(IMAGE_HOST)) return; | ||
|
|
||
| // Deduplicate rapid failures for same URL before sampling | ||
| const key = src.slice(0, 200); | ||
| if (recentlyReported.has(key)) return; | ||
| recentlyReported.add(key); | ||
| setTimeout(() => recentlyReported.delete(key), DEBOUNCE_MS); | ||
|
|
||
| if (Math.random() > REPORT_SAMPLE_RATE) return; | ||
|
|
||
| Sentry.withScope((scope) => { | ||
| scope.setTag("failure_type", "image_load"); | ||
| scope.setTag("image_host", IMAGE_HOST); | ||
| scope.setLevel("warning"); | ||
| scope.setExtras({ | ||
| image_src: src, | ||
| page_url: window.location.href, | ||
| connection_type: (navigator as Navigator & { connection?: { effectiveType?: string } }).connection?.effectiveType, | ||
| online: navigator.onLine | ||
| }); | ||
| Sentry.captureMessage("Client image load failure: " + IMAGE_HOST); | ||
| }); | ||
| }; | ||
|
|
||
| // Capture phase to catch errors on <img> elements (they don't bubble) | ||
| document.addEventListener("error", handler, true); | ||
| return () => document.removeEventListener("error", handler, true); | ||
| }, []); | ||
|
|
||
| return null; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| export * from "./landing-page"; | ||
| export * from "./hiring-console-log"; | ||
| export * from "./image-failure-tracker"; | ||
| export * from "./community-list-item"; | ||
| export * from "./top-communities-widget"; | ||
| export * from "./my-favorites-widget"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
139 changes: 139 additions & 0 deletions
139
apps/web/src/specs/features/shared/image-failure-tracker.spec.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
| import { describe, it, expect, vi, beforeEach, afterEach } from "vitest"; | ||
| import { render, act } from "@testing-library/react"; | ||
| import * as Sentry from "@sentry/nextjs"; | ||
|
|
||
| vi.mock("@sentry/nextjs", () => ({ | ||
| withScope: vi.fn((cb) => cb({ setTag: vi.fn(), setLevel: vi.fn(), setExtras: vi.fn() })), | ||
| captureMessage: vi.fn() | ||
| })); | ||
|
|
||
| import { ImageFailureTracker } from "@/app/_components/image-failure-tracker"; | ||
|
|
||
| const IMAGE_HOST = "images.ecency.com"; | ||
| let urlCounter = 0; | ||
|
|
||
| function uniqueImageUrl() { | ||
| return `https://${IMAGE_HOST}/img/${++urlCounter}.png`; | ||
| } | ||
|
|
||
| function fireImageError(src: string) { | ||
| const img = document.createElement("img"); | ||
| Object.defineProperty(img, "src", { value: src, writable: false }); | ||
| const event = new Event("error", { bubbles: false }); | ||
| Object.defineProperty(event, "target", { value: img }); | ||
| document.dispatchEvent(event); | ||
| } | ||
|
|
||
| function fireNonImageError() { | ||
| const script = document.createElement("script"); | ||
| const event = new Event("error", { bubbles: false }); | ||
| Object.defineProperty(event, "target", { value: script }); | ||
| document.dispatchEvent(event); | ||
| } | ||
|
|
||
| describe("ImageFailureTracker", () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| vi.spyOn(Math, "random").mockReturnValue(0); // always below sample rate | ||
| vi.useFakeTimers(); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| vi.useRealTimers(); | ||
| vi.restoreAllMocks(); | ||
| }); | ||
|
|
||
| it("reports to Sentry when an image from IMAGE_HOST fails", () => { | ||
| render(<ImageFailureTracker />); | ||
|
|
||
| const src = uniqueImageUrl(); | ||
| act(() => { | ||
| fireImageError(src); | ||
| }); | ||
|
|
||
| expect(Sentry.withScope).toHaveBeenCalledOnce(); | ||
| expect(Sentry.captureMessage).toHaveBeenCalledWith( | ||
| expect.stringContaining(IMAGE_HOST) | ||
| ); | ||
|
|
||
| // Verify scope was configured with correct tags and extras | ||
| interface MockScope { | ||
| setTag: ReturnType<typeof vi.fn>; | ||
| setLevel: ReturnType<typeof vi.fn>; | ||
| setExtras: ReturnType<typeof vi.fn>; | ||
| } | ||
| const scopeCb = vi.mocked(Sentry.withScope).mock.calls[0][0] as unknown as (scope: MockScope) => void; | ||
| const mockScope: MockScope = { | ||
| setTag: vi.fn(), | ||
| setLevel: vi.fn(), | ||
| setExtras: vi.fn() | ||
| }; | ||
| scopeCb(mockScope); | ||
|
|
||
| expect(mockScope.setTag).toHaveBeenCalledWith("failure_type", "image_load"); | ||
| expect(mockScope.setTag).toHaveBeenCalledWith("image_host", IMAGE_HOST); | ||
| expect(mockScope.setExtras).toHaveBeenCalledWith( | ||
| expect.objectContaining({ | ||
| image_src: src, | ||
| page_url: expect.any(String), | ||
| online: expect.any(Boolean) | ||
| }) | ||
| ); | ||
| }); | ||
|
|
||
| it("ignores errors from non-HTMLImageElement targets", () => { | ||
| render(<ImageFailureTracker />); | ||
|
|
||
| act(() => { | ||
| fireNonImageError(); | ||
| }); | ||
|
|
||
| expect(Sentry.withScope).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("ignores image errors from non-matching hosts", () => { | ||
| render(<ImageFailureTracker />); | ||
|
|
||
| act(() => { | ||
| fireImageError("https://other-cdn.example.com/photo.jpg"); | ||
| }); | ||
|
|
||
| expect(Sentry.withScope).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("deduplicates rapid errors for the same src", () => { | ||
| render(<ImageFailureTracker />); | ||
|
|
||
| const src = uniqueImageUrl(); | ||
| act(() => { | ||
| fireImageError(src); | ||
| fireImageError(src); | ||
| }); | ||
|
|
||
| expect(Sentry.captureMessage).toHaveBeenCalledTimes(1); | ||
|
|
||
| // After DEBOUNCE_MS, the same URL can report again | ||
| act(() => { | ||
| vi.advanceTimersByTime(5000); | ||
| }); | ||
|
|
||
| act(() => { | ||
| fireImageError(src); | ||
| }); | ||
|
|
||
| expect(Sentry.captureMessage).toHaveBeenCalledTimes(2); | ||
| }); | ||
|
|
||
| it("respects sampling rate", () => { | ||
| render(<ImageFailureTracker />); | ||
|
|
||
| // random() returns 0.5, which is > REPORT_SAMPLE_RATE (0.2) — should skip | ||
| vi.mocked(Math.random).mockReturnValue(0.5); | ||
|
|
||
| act(() => { | ||
| fireImageError(uniqueImageUrl()); | ||
| }); | ||
|
|
||
| expect(Sentry.captureMessage).not.toHaveBeenCalled(); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: ecency/vision-next
Length of output: 99
🏁 Script executed:
Repository: ecency/vision-next
Length of output: 92
🏁 Script executed:
Repository: ecency/vision-next
Length of output: 286
🏁 Script executed:
Repository: ecency/vision-next
Length of output: 82
🏁 Script executed:
Repository: ecency/vision-next
Length of output: 1076
🏁 Script executed:
Repository: ecency/vision-next
Length of output: 116
Missing test coverage for new feature.
All new features in
@ecency/webrequire tests. Add a test file insrc/specs/that verifies:images.ecency.comURLs🤖 Prompt for AI Agents