This guide walks you through building a minimal but fully functional e-book viewer. By the end you will have a page that opens an EPUB, tracks the reader’s position with theDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/johnfactotum/foliate-js/llms.txt
Use this file to discover all available pages before exploring further.
relocate event, and responds to prev() and next() navigation calls. The same setup works for all supported formats — MOBI, AZW3, FB2, CBZ, and PDF — because view.js detects the file type automatically.
foliate-js requires a web server. ES modules cannot be loaded from
file:// URLs. Use any static file server during development.Add foliate-js as a git submodule
There is no npm release yet. The recommended way to include foliate-js is as a git submodule, which lets you pin to a specific commit and pull updates on your own schedule.This clones the library into a
foliate-js/ directory at the root of your project. Commit the .gitmodules file and the submodule reference alongside your other code.Serve your project with a web server
Start any static file server from your project root. Here are two common options:Open
http://localhost:8080 (or whatever port your server uses) in your browser. Do not open HTML files directly via file:// URLs.Import view.js and register the custom element
In your JavaScript module, import The file must be in a
view.js. The import registers the <foliate-view> custom element as a side effect — you do not need to do anything else to enable it.<script type="module"> tag or imported from another module. Classic scripts do not support ES module syntax.Create the element and add it to the DOM
Create the
<foliate-view> element programmatically and append it to the document. Style it to fill the viewport or whatever container you want the reader to occupy.Open a book file
Call
view.open() with a File object (from an <input type="file">), a Blob, or a URL string. The method detects the format automatically and returns a promise that resolves when the book is ready.view.open() calls makeBook() internally, which inspects the file’s magic bytes to identify the format. Passing a URL causes it to fetch the file first.Listen to the relocate event
The Save
relocate event fires whenever the reader’s position changes — on page turns, after goTo() calls, or when the layout reflows. Its detail object contains the current CFI, fraction, TOC item, and more.cfi to persistent storage so you can restore the reader’s position on next load by passing it to view.init().Complete example
Below is a minimal but working reader page that ties all the steps together. It opens a file chosen by the user, tracks position changes, and provides prev/next buttons.Events reference
| Event | When it fires | Key detail properties |
|---|---|---|
relocate | Position changes | cfi, fraction, index, range, tocItem, pageItem |
load | A section finishes loading | doc, index |
link | User clicks an internal link | a, href |
external-link | User clicks an external link | a, href_ |
draw-annotation | An annotation needs to be rendered | draw, annotation, doc, range |
show-annotation | User clicks an annotation | value, index, range |
Navigation methods reference
| Method | Description |
|---|---|
view.open(book) | Open a File, Blob, URL string, or book object |
view.close() | Close the book and release resources |
view.init({ lastLocation, showTextStart }) | Initialize position from a saved CFI or start of text |
view.goTo(target) | Navigate to a CFI, href, or section index |
view.goToFraction(frac) | Navigate to a 0–1 fraction of the whole book |
view.prev() / view.next() | Previous or next page |
view.goLeft() / view.goRight() | Direction-aware navigation (handles RTL books) |