Skip to content

fix: memoize readability, instagram and tiktok per document, not per url - #894

Merged
Kikobeats merged 1 commit into
masterfrom
Kikobeats/readability-memo-by-document
Nov 23, 2026
Merged

Kikobeats merged 1 commit into
masterfrom
Kikobeats/readability-memo-by-document

Conversation

@Kikobeats

@Kikobeats Kikobeats commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

Problem

metascraper-readability, metascraper-instagram and metascraper-tiktok memoize a parse with memoizeOne.EqualityFirstArgument, so the cached result is keyed on the url alone:

  • readability(url, html, opts) (packages/metascraper-readability/src/index.js)
  • getDescription(url, $) (packages/metascraper-instagram/src/index.js)
  • getAuthorAndUsername(url, $) (packages/metascraper-tiktok/src/index.js)

The memo lives at module level, so it is shared by every call in the process. A second document at the same url (the same page fetched with other headers, through another proxy, or prerendered vs not) gets the first document's description, date or author. Found in the Microlink API, where concurrent requests for one url with different HTML returned the earlier document's description.

Fix

Use memoizeOne.EqualityUrlAndHtmlDom, the comparator metascraper-iframe already uses. The result is still reused across the rules of one extraction (same url, same document), and recomputed for a new document.

Tests

Each package gets a test that runs two different documents at the same url and expects each to be read on its own. All three fail before the fix (tiktok returned First Author for the second document) and pass after:

  • readability: 8 passed
  • instagram: 14 passed
  • tiktok: 7 passed

standard is clean on the three packages.

After release

The Microlink API carries this as a pnpm patch on metascraper-readability@5.58.7 (MR !2109). Once this is released, bumping metascraper* there lets the patch go.

🤖 Generated with Claude Code


Note

Medium Risk
Behavior change for repeated URLs with different HTML affects extraction correctness in concurrent or variant-page scenarios; scope is limited to three scraper packages with regression tests.

Overview
Fixes incorrect metadata when the same URL is scraped with different HTML (e.g. concurrent Microlink requests). Module-level memoization in metascraper-readability, metascraper-instagram, and metascraper-tiktok used memoizeOne.EqualityFirstArgument, so the cache key was only the URL and the first document’s parsed result was reused for later HTML.

All three now use memoizeOne.EqualityUrlAndHtmlDom (same pattern as metascraper-iframe): one parse is still shared across rules for a single extraction, but a new document triggers a fresh parse.

Each package adds a test that runs two distinct HTML payloads at the same URL and asserts metadata reflects the second document independently (dates, descriptions, authors).

Reviewed by Cursor Bugbot for commit 8f5ca45. Bugbot is set up for automated code reviews on this repo. Configure here.

Summary by CodeRabbit

  • Bug Fixes
    • Results for Instagram, TikTok, and article pages now reflect the HTML content provided for each request, even when the URL is unchanged.

readability(url, html), instagram's getDescription(url, $) and tiktok's
getAuthorAndUsername(url, $) were memoized with EqualityFirstArgument,
so a second document at the same url, such as the same page fetched
with other headers, another proxy or prerendered, got the first
document's answer. They now use EqualityUrlAndHtmlDom, like
metascraper-iframe, which still reuses the result across the rules of
one extraction.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Instagram, Readability, and TikTok memoization now compares the URL and HTML DOM. Tests check that different documents at the same URL produce results based on their own content.

Changes: Extractor memoization

Layer / File(s) Summary
HTML-aware memoization and regression coverage
packages/metascraper-instagram/src/index.js, packages/metascraper-instagram/test/index.js, packages/metascraper-readability/src/index.js, packages/metascraper-readability/test/index.js, packages/metascraper-tiktok/src/index.js, packages/metascraper-tiktok/test/index.js
The three extractors use URL-and-HTML-DOM equality for memoization. Tests process different HTML documents at the same URL and check that each result reflects its document.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Merge Risk: Minimal · up to 8f5ca

This change stops the Instagram, Readability and TikTok extractors from returning cached data from an earlier document when a later document shares the same URL. Each package has a test for that case, and no merge-blocking risk was found.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the three changed packages and the core change: memoization moves from per-URL to per-document. The changeset replaces memoizeOne.EqualityFirstArgument with memoizeOne.EqualityUrlAndHt…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the three changed packages and the core change: memoization moves from per-URL to per-document. The changeset replaces memoizeOne.EqualityFirstArgument with memoizeOne.EqualityUrlAndHtmlDom in readability, instagram and tiktok, and adds tests that use two different documents at the same URL. The title matches the main change. Wording is specific, and the 72-character length is within a normal title range.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: a21a7598-9654-46d1-a726-5ae6a653b04e
Commits

Reviewing files that changed from the base of the PR and between 26bbda7 and 8f5ca45.

Files selected for processing: 6

  • packages/metascraper-instagram/src/index.js
  • packages/metascraper-instagram/test/index.js
  • packages/metascraper-readability/src/index.js
  • packages/metascraper-readability/test/index.js
  • packages/metascraper-tiktok/src/index.js
  • packages/metascraper-tiktok/test/index.js

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

@coveralls

Copy link
Copy Markdown
Collaborator

Coverage Status

Coverage is 95.213% — Kikobeats/readability-memo-by-document into master. No base build found for master.

@Kikobeats
Kikobeats merged commit 03afa46 into master Nov 23, 2026
67 checks passed
@Kikobeats
Kikobeats deleted the Kikobeats/readability-memo-by-document branch November 23, 2026 21:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants