CodingNic

Persist Reading Progress

Convert EPUB CFI to Book-Wide Progress

Persist Reading Progress 18 min read

Convert EPUB CFI to Book-Wide Progress

Convert EPUB CFI to Book-Wide Progress

Task

Use EPUB.js book locations to turn the current CFI into a book-wide percentage.

When the Reader receives a relocation event:

ts
rendition.on("relocated", (location) => {
  const cfi = location.start.cfi;
  const percentage = book.locations.percentageFromCfi(cfi);

  // Send cfi and percentage to the progress boundary.
});

Keep the CFI as the resume position and the percentage as the display/progress value.

Do not use chapter-local displayed.page / displayed.total for the overall book progress.

Test

Navigate across several chapters and inspect the stored or logged percentage.

The value should represent movement through the whole book.

Checkpoint

The Reader produces a stable EPUB CFI plus a book-wide percentage suitable for persistence.