CodingNic

Build the EPUB Reader

Handle Reader Lifecycle and Errors

Build the EPUB Reader 20 min read

Handle Reader Lifecycle and Errors

Handle Reader Lifecycle and Errors

Task

Make the Reader safe to mount, unmount, switch books, and recover from failures.

When the component unmounts:

ts
return () => {
  renditionRef.current?.destroy();
  bookRef.current?.destroy();
  renditionRef.current = null;
  bookRef.current = null;
};

Guard asynchronous setup so a late promise cannot update an unmounted component.

Show a Reader error state when:

  • the signed URL cannot be created
  • the EPUB fails to open
  • the rendition cannot mount

Keep the error state inside the prepared Reader layout.

Test

Verify:

  • open a valid EPUB
  • switch books quickly
  • leave the Reader and return
  • use an invalid or missing Book ID
  • simulate an EPUB load failure

No stale Reader instance should remain mounted.

Checkpoint

The Reader is a real EPUB.js integration with navigation, Contents, book-wide locations, cleanup, and error handling.

Module Complete

Readly can now open and read real EPUBs. Module 7 will persist the CFI and restore the exact reading position when the user returns.