Skip to content

fix(content): read the Library export's pages in bounded calls, and wrap Excel's page text - #1365

Merged
gkhngyk merged 1 commit into
ansvisor:mainfrom
cihan-gyk:fix/library-export-slices
Oct 22, 2026
Merged

gkhngyk merged 1 commit into
ansvisor:mainfrom
cihan-gyk:fix/library-export-slices

Conversation

@cihan-gyk

Copy link
Copy Markdown
Collaborator

Summary

Follow-up to #1354's review: Library Export now reads drafts' pages in bounded calls, Excel wraps the page text, and getContentDraftPages has its own test.

  1. Bounded responses. getContentDraftPages returned every page of up to 500 drafts in one server-action response. A page averages ~30 KB on prod and can reach 500 KB.
    • It now takes at most DRAFT_PAGES_PER_CALL = 50 ids. The constant lives in lib/content/drafts.ts, since a 'use server' file exports only async functions.
    • It reads them in one query (.eq('brand_id', …).in('id', …)).
    • It stops at about 3 MB of pages (PAGES_RESPONSE_BUDGET, well inside a serverless response's 4.5 MB). It returns { pages, rest }, where rest holds the ids it left for the next call. It always returns at least one page, so a list keeps moving.
    • _library-export.tsx reads the list in turn: 50 ids a call, with each call's rest going first in the next. The single-row export reads its one page as before.
    • If one call fails, the existing "couldn't export" toast shows and no file is written.
  2. Excel wraps the page text. The Content cell (one block per line) is written as { value, wrap: true } when it has a line break, as opportunityWorkbook does. The 32,767-character cut still applies first. A full page makes Excel size its row up to the 409 pt maximum. That is the trade-off for readable lines.
  3. Tests.
    • New lib/actions/content-drafts-pages.test.ts:
      • one read scoped by brand_id;
      • the order given is kept, and a missing draft is left out;
      • more than 50 ids, a non-UUID id or a bad brand throws invalid;
      • the budget stops at 2 of 3 pages of 1.2 MB and returns the rest;
      • a single page over the budget still comes back.
    • draft-export.test.ts: the wrapped Content cell, and the cut on its value.
    • Library page.test.tsx: 120 drafts are read as 50 / 50 / 20, in order, with every draft in the CSV once. A call that leaves 20 ids for its size makes the next call start with those 20.

Deploy note: a browser tab still on the old client during the deploy may send up to 500 ids, get invalid and show the export-failed toast until it reloads.

Related issue

Follow-up to #1354 (review items 1–3). No issue.

Type of change

  • feat — New feature
  • fix — Bug fix
  • chore — Maintenance / dependencies
  • docs — Documentation only
  • refactor — Code change that neither fixes a bug nor adds a feature
  • test — Adding or updating tests

How to test

  1. Content › Library with drafts: Export CSV / Excel / Word for the list. Every listed draft is in the file once, in the list's order.
  2. Open the Excel file: the Content column shows each block on its own line.
  3. In the network panel, a list of more than 50 drafts sends several Export calls of at most 50 ids.

Checklist

  • Branch follows the naming convention (feature/, fix/, chore/, docs/) — see CONTRIBUTING.md
  • Commits follow Conventional Commits
  • yarn lint passes (run from web/) (0 errors; the 6 warnings are in untouched files, as on main)
  • yarn typecheck passes (run from web/)
  • yarn format:check passes (run from web/)
  • Changes are focused — one concern per PR

Also run: web yarn test (2,769 tests) and yarn build.

🤖 Generated with Claude Code

…rap Excel's page text

Follow-up to ansvisor#1354's review. getContentDraftPages took up to 500 ids and
sent every page back in one response. It now takes at most 50 a call
(DRAFT_PAGES_PER_CALL) and stops at about 3 MB of pages, handing back the
ids it left, always with at least one page; the Library reads a list in
turn, each call's rest first. Excel's Content cell wraps, as the
opportunity export's does. getContentDraftPages has its own test.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@cihan-gyk

Copy link
Copy Markdown
Collaborator Author

@gkhngyk the follow-up to your #1354 review (items 1–3).

  • Calls take at most 50 ids, read in turn.
  • A call also stops at about 3 MB of pages and hands back the ids it left, always returning at least one page. A pre-push review pointed out that 50 pages of up to 500 KB could still pass Vercel's 4.5 MB.
  • Excel's Content cell wraps.
  • getContentDraftPages has its own test.

@gkhngyk gkhngyk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. One small follow-up:

  1. The response budget counts characters, not bytes (web/src/lib/actions/content-drafts.ts:297).
    • pageSize adds up .length, which counts UTF-16 characters. The 4.5 MB response limit counts bytes.
    • A page in Cyrillic, Greek or Arabic takes about 2 bytes a character in UTF-8, and CJK takes 3. The serialized response also escapes every " in the HTML.
    • So 50 non-Latin pages can reach 3,000,000 characters, which is 6–9 MB, before the budget stops them.
    • Fix: count bytes with Buffer.byteLength(...) for the body, title and URL (the action runs on Node), or halve the budget. The budget test still passes with its ASCII bodies.

Checked and fine:

  • The read goes through the session client. content_drafts: member select limits it to the caller's brands, and .eq('brand_id', …) keeps it to the brand given.
  • 50 uuids add about 1.9 KB to the request URL, and the primary-key lookup is far inside the 8 s timeout.
  • The client's queue keeps the list's order: rest goes first, and the items are built in drafts order. Each call returns at least one page or moves past the missing ids, so the loop always ends.
  • If one call fails, the error reaches the existing toast and no file is written.
  • Excel's { value, wrap: true } cell is the same form opportunityWorkbook uses, and the cut is applied before the wrap.

@gkhngyk
gkhngyk merged commit bde9b0e into ansvisor:main Oct 22, 2026
6 checks passed
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