toeverything/AFFiNE · error
Link preview response too large
Error message
Link preview response too large
What it means
readLinkPreviewResponse enforces a size cap (default 4 MiB) while streaming the response body. If the accumulated byte count exceeds maxBytes, it cancels the reader and throws 'Link preview response too large'. This protects the client from downloading unbounded payloads from the preview service.
Solutions
- Increase maxBytes when calling readLinkPreviewResponse if legitimate responses exceed 4 MiB
- Check the preview backend for runaway payload sizes (e.g. oversized transcripts or embedded data)
- Handle the error with try-catch and fall back to a basic link card without preview data
- If responses are legitimately large, stream/process them differently instead of buffering
Example fix
// before const data = await readLinkPreviewResponse(res); // after const data = await readLinkPreviewResponse(res, 16 * 1024 * 1024); // raise cap to 16 MiB
Defensive patterns
Strategy: try-catch
Validate before calling
const contentLength = Number(response.headers.get('content-length') ?? 0);
const maxBytes = 4 * 1024 * 1024;
if (contentLength > maxBytes) {
throw new Error('Preview response exceeds size limit');
} Type guard
null
Try / catch
try {
const data = await readLinkPreviewResponse(response);
} catch (e) {
if (e.message === 'Link preview response too large') {
// skip preview or retry with a higher maxBytes
} else throw e;
} Prevention
- Check content-length header against your maxBytes before reading
- Pass an explicit maxBytes sized for your largest legitimate payloads
- Be cautious lowering maxBytes below 4 MiB — typical responses with transcripts can be large
- Treat oversized responses from untrusted servers as a signal to blocklist them
When it happens
Trigger: The preview endpoint returns a JSON body larger than maxBytes (default 4*1024*1024 bytes); happens when a caller passes a smaller custom maxBytes and the response slightly exceeds it, or the backend misbehaves and streams an oversized document.
Common situations: A malicious or misconfigured preview server returning huge payloads; a caller lowering maxBytes below the typical response size; responses that embed very long transcripts (e.g. full YouTube transcripts) pushing past the limit.
Understand the failure class
Background: payload too large / request exceeds maximum size: why libraries cap bytes and how to fix oversize payloads — this error's family across 50 libraries.
Related errors
AI-assisted analysis of toeverything/AFFiNE@2af30773ae (2026-09-15).
Data as JSON: /api/errors/859fb5931fd23251.
Report an issue: GitHub.
Appendix: source
Thrown at blocksuite/affine/shared/src/services/link-preview-service/response.ts:103
export async function readLinkPreviewResponse(
response: Response,
maxBytes = 4 * 1024 * 1024
) {
if (!response.ok || !response.body)
throw new Error('Link preview unavailable');
const reader = response.body.getReader();
const decoder = new TextDecoder();
let bytes = 0;
let json = '';
try {
for (;;) {
const { done, value } = await reader.read();
if (done) break;
bytes += value.byteLength;
if (bytes > maxBytes) {
await reader.cancel();
throw new Error('Link preview response too large');
}
json += decoder.decode(value, { stream: true });
}
json += decoder.decode();
} finally {
reader.releaseLock();
}
const data = parseLinkPreviewResponse(JSON.parse(json));
if (!data) throw new Error('Invalid link preview response');
return data;
}
View on GitHub (pinned to 2af30773ae)