Skip to content

Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.

fix(news): stop logging a console error for an article that simply has no picture

nirholasf32c2987fd1c
diff --git a/api/news/image.js b/api/news/image.js
index 6f06d9e31..15553ebfa 100644
--- a/api/news/image.js
+++ b/api/news/image.js
@@ -14,8 +14,13 @@
// scheme allowlist, DNS + private-IP blocklist, byte cap, timeout),
// extracts its og:image / twitter:image, and 302s to /api/img — which
// serves the bytes same-origin, immune to hotlink-referrer blocks.
-// 4. If the page has no usable preview image, answers 404 (negatively
-// cached) and the card keeps its designed fallback tile.
+// 4. If the page has no usable preview image, answers 204 No Content
+// (negatively cached) and the card keeps its designed fallback tile. 204
+// rather than 404 because "this article has no picture" is an ordinary,
+// expected outcome for a fifth of the feed: a 404 makes the browser log a
+// failed resource in every reader's console for a case the design already
+// handles. A link we never served is still a 404, because that IS an
+// error.
//
// Both outcomes are cached: in-process per article link, and at the CDN via
// cache-control on the redirect/404 — one resolution per article, ever.
@@ -87,11 +92,12 @@ export default wrap(async (req, res) => {
const image = article.image || (await resolvePreviewImage(article));
if (!image) {
- return json(
- res, 404,
- { error: 'no_preview_image', error_description: 'the publisher page carries no preview image' },
- { 'cache-control': 'public, max-age=1800, s-maxage=3600' },
- );
+ // No body: 204 is the whole answer, and it stays cacheable so an
+ // imageless article costs one resolution rather than one per render.
+ res.statusCode = 204;
+ res.setHeader('cache-control', 'public, max-age=1800, s-maxage=3600');
+ res.end();
+ return;
}
res.statusCode = 302;
diff --git a/src/shared/news-render.js b/src/shared/news-render.js
index 07ff5037a..e22082a3d 100644
--- a/src/shared/news-render.js
+++ b/src/shared/news-render.js
@@ -145,7 +145,10 @@ function pumpResolveQueue() {
async function resolveMedia(el, link) {
try {
const res = await fetch(`/api/news/image?url=${encodeURIComponent(link)}`);
- if (!res.ok) return; // no preview exists — the tile IS the design
+ // 204 is the resolver saying this article has no picture, which is an
+ // ordinary answer for about a fifth of the feed: the fallback tile IS the
+ // design. Any other non-ok status is a genuine miss and ends the same way.
+ if (res.status === 204 || !res.ok) return;
const blob = await res.blob();
if (!blob.type.startsWith('image/')) return;
const tile = el.querySelector('.nw-fallback');
diff --git a/tests/news-image-resolver.test.js b/tests/news-image-resolver.test.js
new file mode 100644
index 000000000..eb694ffa3
--- /dev/null
+++ b/tests/news-image-resolver.test.js
@@ -0,0 +1,112 @@
+// Coverage for api/news/image.js, the preview-image resolver behind every news
+// card.
+//
+// About a fifth of the live feed publishes text-only RSS, so "this article has
+// no picture" is an ordinary answer, not a failure. The endpoint used to say it
+// with a 404, which the browser logs as a failed resource in the reader's
+// console on a surface whose bar is a clean console, and which reads as broken
+// to anyone watching the network tab. It now answers 204 for that case and
+// keeps 404 for the case that IS an error: a link the aggregator never served,
+// which is a probe against an endpoint that must never become an open resolver.
+//
+// Both answers stay cacheable, because the whole point is that one article
+// resolves once rather than once per render.
+
+import { describe, it, expect, vi, beforeEach } from 'vitest';
+
+vi.mock('../api/_lib/http.js', () => ({
+ wrap: (fn) => fn,
+ cors: () => false,
+ method: () => true,
+ rateLimited: (res) => {
+ res._json = { status: 429, body: { error: 'rate_limited' } };
+ return res;
+ },
+ json: (res, status, body, headers = {}) => {
+ res._json = { status, body, headers };
+ return res;
+ },
+ error: (res, status, code, message) => {
+ res._json = { status, body: { error: code, message } };
+ return res;
+ },
+}));
+vi.mock('../api/_lib/rate-limit.js', () => ({
+ limits: { imgProxyIp: vi.fn(async () => ({ success: true })) },
+ clientIp: () => '1.2.3.4',
+}));
+
+const findArticle = vi.fn(async () => null);
+const extractOgImage = vi.fn(() => null);
+vi.mock('../api/_lib/news.js', () => ({
+ findArticle: (...a) => findArticle(...a),
+ extractOgImage: (...a) => extractOgImage(...a),
+}));
+vi.mock('../api/_lib/fetch-model.js', () => ({ fetchModel: vi.fn(async () => ({ bytes: Buffer.from('') })) }));
+
+const { default: handler } = await import('../api/news/image.js');
+
+function makeRes() {
+ return {
+ statusCode: 200,
+ headers: {},
+ ended: false,
+ body: undefined,
+ setHeader(k, v) {
+ this.headers[k.toLowerCase()] = v;
+ },
+ end(chunk) {
+ this.ended = true;
+ this.body = chunk;
+ },
+ };
+}
+
+const req = (link) => ({ url: `/api/news/image?url=${encodeURIComponent(link)}`, method: 'GET', headers: {} });
+
+describe('the preview-image resolver separates "no picture" from "no such article"', () => {
+ beforeEach(() => {
+ findArticle.mockReset();
+ extractOgImage.mockReset();
+ });
+
+ it('answers 204 with no body when the article carries no image', async () => {
+ findArticle.mockResolvedValue({ link: 'https://pub.example/a', image: null, source: 'Pub' });
+ extractOgImage.mockReturnValue(null);
+ const res = makeRes();
+ await handler(req('https://pub.example/a'), res);
+ expect(res.statusCode).toBe(204);
+ expect(res.body).toBeUndefined();
+ expect(res._json).toBeUndefined();
+ // Negative caching is the reason one imageless article costs one
+ // resolution rather than one per render.
+ expect(res.headers['cache-control']).toMatch(/max-age=\d+/);
+ });
+
+ it('still answers 404 for a link the aggregator never served', async () => {
+ findArticle.mockResolvedValue(null);
+ const res = makeRes();
+ await handler(req('https://not-ours.example/x'), res);
+ expect(res._json.status).toBe(404);
+ expect(res._json.body.error).toBe('unknown_article');
+ });
+
+ it('redirects a known image to the same-origin proxy, never to the publisher', async () => {
+ findArticle.mockResolvedValue({
+ link: 'https://pub.example/b',
+ image: 'https://cdn.pub.example/hero.jpg',
+ source: 'Pub',
+ });
+ const res = makeRes();
+ await handler(req('https://pub.example/b'), res);
+ expect(res.statusCode).toBe(302);
+ expect(res.headers.location.startsWith('/api/img?url=')).toBe(true);
+ expect(decodeURIComponent(res.headers.location)).toContain('https://cdn.pub.example/hero.jpg');
+ });
+
+ it('rejects a request with no url instead of guessing one', async () => {
+ const res = makeRes();
+ await handler({ url: '/api/news/image', method: 'GET', headers: {} }, res);
+ expect(res._json.status).toBe(400);
+ });
+});